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

計數 int 中使用的位

Count bits used in int(計數 int 中使用的位)
本文介紹了計數 int 中使用的位的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如果你有二進制數 10110,我怎樣才能讓它返回 5?例如一個數字,它告訴我們使用了多少位?下面列出了一些類似的例子:

If you have the binary number 10110 how can I get it to return 5? e.g a number that tells how many bits are used? There are some likewise examples listed below:

  • 101 應該返回 3
  • 000000011 應該返回 2
  • 11100 應該返回 5
  • 101010101 應該返回 9

如何在 Java 中以最簡單的方式獲得它?我想出了以下方法,但我可以做得更快嗎:

How can this be obtained the easiest way in Java? I have come up with the following method but can i be done faster:

public static int getBitLength(int value)
{
    if (value == 0)
    {
        return 0;
    }
    int l = 1;
    if (value >>> 16 > 0) { value >>= 16; l += 16; }
    if (value >>> 8 > 0) { value >>= 8; l += 8; }
    if (value >>> 4 > 0) { value >>= 4; l += 4; }
    if (value >>> 2 > 0) { value >>= 2; l += 2; }
    if (value >>> 1 > 0) { value >>= 1; l += 1; }
    return l;
}

推薦答案

最簡單?

32 - Integer.numberOfLeadingZeros(value)

如果您正在尋找算法,Java API 的實現者同意您的分而治之的位移方法:

If you are looking for algorithms, the implementors of the Java API agree with your divide-and-conquer bitshifting approach:

public static int numberOfLeadingZeros(int i) {
    if (i == 0)
        return 32;
    int n = 1;
    if (i >>> 16 == 0) { n += 16; i <<= 16; }
    if (i >>> 24 == 0) { n +=  8; i <<=  8; }
    if (i >>> 28 == 0) { n +=  4; i <<=  4; }
    if (i >>> 30 == 0) { n +=  2; i <<=  2; }
    n -= i >>> 31;
    return n;
}

編輯:提醒那些相信浮點計算準確性的人,運行以下測試工具:

Edit: As a reminder to those who trust in the accuracy of floating point calculations, run the following test harness:

public static void main(String[] args) {
    for (int i = 0; i < 64; i++) {
        long x = 1L << i;
        check(x);
        check(x-1);
    }
}

static void check(long x) {
    int correct = 64 - Long.numberOfLeadingZeros(x);
    int floated = (int) (1 + Math.floor(Math.log(x) / Math.log(2)));
    if (floated != correct) {
        System.out.println(Long.toString(x, 16) + " " + correct + " " + floated);
    }
}

第一個檢測到的偏差是:

The first detected deviation is:

ffffffffffff 48 49

這篇關于計數 int 中使用的位的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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(使用線程逐塊處理文件)
主站蜘蛛池模板: 一区二区视频在线 | 日本一区二区三区四区 | 免费亚洲视频 | 一区二区免费 | 欧美a v在线 | 羞羞色网站| 亚洲另类春色偷拍在线观看 | 国产精品无码专区在线观看 | 亚洲一区二区三区 | 国产精品久久久久久久午夜 | 亚洲精品久久久久久久久久久 | 高清国产一区二区 | 国产第一页在线播放 | 色屁屁在线观看 | 日本精品一区二区三区在线观看视频 | 欧美国产91| 四色成人av永久网址 | 久久久激情视频 | 91精品久久久久久久久久入口 | 免费看淫片 | 美女一级a毛片免费观看97 | 色一情一乱一伦一区二区三区 | 亚洲国产成人精品女人久久久 | 亚洲人成一区二区三区性色 | 第四色影音先锋 | 亚洲一区二区三区免费观看 | 亚洲综合一区二区三区 | 久久久性 | 久久精品一区二区三区四区 | 国产精品中文 | 福利网址| 久久国产视频网站 | www.9191 | 精品粉嫩超白一线天av | 青青久久av北条麻妃海外网 | 在线成人免费观看 | 国产精品国产三级国产aⅴ中文 | 日韩www| 欧美一二三区 | 日韩视频中文字幕 | 日韩2020狼一二三 |