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

檢查整數中是否只設置了一個位(無論其位置如何

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

問題描述

我使用 64 位整數中的位存儲標志.
我想知道在 64 位整數中的位置是否設置了一個位(即我不關心任何特定位的位置).

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 計算位數(肖恩·埃隆·安德森(Sean Eron Anderson)),但我想知道僅檢測是否設置了一個位的最有效方法是什么...

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...

我發現了一些其他相關的問題:

I found some other related questions:

  • (8051) 檢查是否設置了單個位
  • 檢測整數內的單個一位流

還有一些維基百科頁面:

and also some Wikipedia pages:

  • 查找第一個
  • 位操作
  • 漢明權重

注意:我的應用程序是用 java 編寫的,但我對使用其他語言的優化感到好奇...

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

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

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.

推薦答案

如果您只是想檢查是否設置了一個位,那么您實際上是在檢查該數字是否是 2 的冪.要做到這一點,您可以做:

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 的冪,因此如果這很重要,您應該檢查不是 0 的數字.那么:

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) ...

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

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

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 国产日韩一区二区三免费高清 | 精品二区 | 久久精品久久久久久 | 久久人人国产 | 欧美乱码精品一区二区三区 | 亚洲va欧美va人人爽午夜 | 99久久精品国产一区二区三区 | 在线免费观看日本视频 | 欧美全黄 | 密色视频 | 国产一区二区三区网站 | 国产特级毛片aaaaaa喷潮 | 国产大片黄色 | 人人做人人澡人人爽欧美 | 亚洲播放一区 | 亚洲h在线观看 | 免费观看黄色一级片 | 欧美精品一区三区 | 久久久久久电影 | 91色视频在线 | 中文字幕在线免费观看 | 国产日韩欧美一区二区 | a黄毛片 | 97人澡人人添人人爽欧美 | 欧美精三区欧美精三区 | 日韩中文字幕一区二区三区 | 国产一二区视频 | 亚洲精品一区二区三区中文字幕 | 国内久久| 一区二区高清在线观看 | 亚洲精品天堂 | 欧美日韩一区不卡 | 国产一区二区三区四区五区加勒比 | 午夜天堂精品久久久久 | 亚洲精选一区二区 | 一区二区三区在线看 | 四虎最新视频 | 成人日韩av| 91久久精品日日躁夜夜躁国产 | 四虎影视1304t | 91麻豆精品国产91久久久资源速度 |