久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

檢查整數(shù)中是否只設(shè)置了一個(gè)位(無(wú)論其位置如何

Check if only one single bit is set within an integer (whatever its position)(檢查整數(shù)中是否只設(shè)置了一個(gè)位(無(wú)論其位置如何))
本文介紹了檢查整數(shù)中是否只設(shè)置了一個(gè)位(無(wú)論其位置如何)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我使用 64 位整數(shù)中的位存儲(chǔ)標(biāo)志.
我想知道在 64 位整數(shù)中的位置是否設(shè)置了一個(gè)位(即我不關(guān)心任何特定位的位置).

I store flags using bits within a 64-bits integer.
I want to know if there is a single bit set whatever the position within the 64-bits integer (e.i. I do not care about the position of any specific bit).

boolean isOneSingleBitSet (long integer64)
{
   return ....;
}

我可以使用 Bit Twiddling Hacks 計(jì)算位數(shù)(肖恩·埃隆·安德森(Sean Eron Anderson)),但我想知道僅檢測(cè)是否設(shè)置了一個(gè)位的最有效方法是什么...

I could count number of bits using the Bit Twiddling Hacks (by Sean Eron Anderson), but I am wondering what is the most efficient way to just detect whether one single bit is set...

我發(fā)現(xiàn)了一些其他相關(guān)的問(wèn)題:

I found some other related questions:

  • (8051) 檢查是否設(shè)置了單個(gè)位
  • 檢測(cè)整數(shù)內(nèi)的單個(gè)一位流

還有一些維基百科頁(yè)面:

and also some Wikipedia pages:

  • 查找第一個(gè)
  • 位操作
  • 漢明權(quán)重

注意:我的應(yīng)用程序是用 java 編寫的,但我對(duì)使用其他語(yǔ)言的優(yōu)化感到好奇...

NB: my application is in java, but I am curious about optimizations using other languages...

編輯:L?u V?nh Phúc 指出我的問(wèn)題中的第一個(gè)鏈接已經(jīng)得到了答案:請(qǐng)參閱確定整數(shù)是否為 2 的冪部分em>Bit Twiddling Hacks(作者 Sean Eron Anderson).我沒(méi)有意識(shí)到一位二的冪是一樣的.

EDIT: L?u V?nh Phúc pointed out that my first link within my question already got the answer: see section Determining if an integer is a power of 2 in the Bit Twiddling Hacks (by Sean Eron Anderson). I did not realized that one single bit was the same as power of two.

推薦答案

如果您只是想檢查是否設(shè)置了一個(gè)位,那么您實(shí)際上是在檢查該數(shù)字是否是 2 的冪.要做到這一點(diǎn),您可以做:

If you just literally want to check if one single bit is set, then you are essentially checking if the number is a power of 2. To do this you can do:

if ((number & (number-1)) == 0) ...

這也將 0 視為 2 的冪,因此如果這很重要,您應(yīng)該檢查不是 0 的數(shù)字.那么:

This will also count 0 as a power of 2, so you should check for the number not being 0 if that is important. So then:

if (number != 0 && (number & (number-1)) == 0) ...

這篇關(guān)于檢查整數(shù)中是否只設(shè)置了一個(gè)位(無(wú)論其位置如何)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 精品亚洲一区二区三区四区五区高 | 一区在线视频 | 中文字幕一区二区三区精彩视频 | 8x国产精品视频一区二区 | 欧美日韩一区精品 | 免费特黄视频 | 毛片99| 91亚洲一区| 亚洲麻豆 | 超碰在线人 | 亚洲午夜小视频 | 欧美日韩国产中文 | 免费观看毛片 | 亚洲综合网站 | 91久久国产综合久久 | 欧美日韩在线精品 | 国产精品久久久久久高潮 | 色吧色综合 | 欧美一区二区免费在线 | 国产精品视频一二三区 | 在线免费观看a级片 | 国产三级电影网站 | 欧美日韩视频在线第一区 | 日韩免| 日韩欧美一区二区三区免费看 | 亚洲国产二区 | 国产免费自拍 | 精品国产乱码久久久久久图片 | 国产欧美精品一区二区色综合朱莉 | 三级免费 | 精品福利一区二区三区 | 国产激情视频在线免费观看 | 亚洲精品久久久久中文字幕二区 | 久草视频在线播放 | 激情国产| 综合网伊人 | 国产精品久久久久久吹潮 | 国产线视频精品免费观看视频 | 国产成人麻豆免费观看 | 成人亚洲片 | 99热精品在线 |