【问题标题】:count leading zeros (clz) or number of leading zeros (nlz) in Java在 Java 中计算前导零 (clz) 或前导零的数量 (nlz)
【发布时间】:2014-02-16 12:27:06
【问题描述】:

我需要二进制的int 3200100000 或二进制的int 127 0111 1111。 变体 Integer.toBinaryString 仅从 1 返回结果。 如果我以这种方式构建 for 循环:

for (int i= 32; i <= 127; i + +) {
System.out.println (i); 
System.out.println (Integer.toBinaryString (i));
}

从二进制数中,我需要前导零的数量(计算前导零 (clz) 或前导零的数量 (nlz))我的意思是 0 的确切数量,例如:在 00100000 -> 2 和 0111 1111 - > 1

【问题讨论】:

  • 考虑格式化输出以放置前导零。

标签: java binary converter


【解决方案1】:

怎么样

int lz = Integer.numberOfLeadingZeros(i & 0xFF) - 24;
int tz = Integer.numberOfLeadingZeros(i | 0x100); // max is 8.

【讨论】:

    【解决方案2】:

    按如下方式计算前导零的数量:

    int lz = 8;
    while (i)
    {
        lz--;
        i >>>= 1;
    }
    

    当然,这里假设数字不超过 255,否则,你会得到否定的结果。

    【讨论】:

    • 这种方法非常慢,因为它会创建一个临时的 String 对象。推荐使用整数方法。
    • @antiguru:差不多两年后看到这个,我决定我必须改变它。这太可怕了。
    【解决方案3】:

    有效的解决方案是 int ans = 8-(log2(x)+1)

    你可以计算log2(x)= logy (x) / logy (2)

    【讨论】:

    • 这个解决方案几乎等于@Martijn Courteaux 给出的解决方案,log2(x) 给出了没有前导零的 binaryString(x) 的长度。所以 8 - lengthOfString_WithoutLeadingZeroes 给出了你的答案
    • 什么意思,32 的正确解是 3,它的工作正常!
    • 好吧,我没注意到二进制计数是以 pow(2,0) 开头的,所以答案要减一,而且答案应该是严格的整数,不要四舍五入。跨度>
    • 多么棒的答案!如果可以的话,我会多次投票。谢谢!
    【解决方案4】:
    public class UtilsInt {
    
        int leadingZerosInt(int i) {
            return leadingZeros(i,Integer.SIZE);
        }
    
        /**
         * use recursion to find occurence of first set bit
         * rotate right by one bit & adjust complement
         * check if rotate value is not zero if so stop counting/recursion
         * @param i - integer to check 
         * @param maxBitsCount - size of type (in this case int)
         *                       if we want to check only for:
         *                         positive values we can set this to Integer.SIZE / 2   
         *                         (as int is signed  in java - positive values are in L16 bits)
         */
         private synchronized int leadingZeros(int i, int maxBitsCount) {
             try {
                logger.debug("checking if bit: "+ maxBitsCount 
                                    + " is set | " + UtilsInt.intToString(i,8));
                return (i >>>= 1) != 0 ? leadingZeros(i, --maxBitsCount) : maxBitsCount;
             } finally {
                 if(i==0) logger.debug("bits in this integer from: " + --maxBitsCount 
                                   + " up to last are not set (i'm counting from msb->lsb)");
             }
        }
    }
    

    测试语句:

    int leadingZeros = new UtilsInt.leadingZerosInt(255); // 8
    

    测试输出:

    checking if bit: 32 is set |00000000 00000000 00000000 11111111
    checking if bit: 31 is set |00000000 00000000 00000000 01111111
    checking if bit: 30 is set |00000000 00000000 00000000 00111111
    checking if bit: 29 is set |00000000 00000000 00000000 00011111
    checking if bit: 28 is set |00000000 00000000 00000000 00001111
    checking if bit: 27 is set |00000000 00000000 00000000 00000111
    checking if bit: 26 is set |00000000 00000000 00000000 00000011
    checking if bit: 25 is set |00000000 00000000 00000000 00000001
    bits in this integer from: 24 up to last are not set (i'm counting from msb->lsb)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 2022-01-21
      • 2023-03-21
      • 1970-01-01
      • 2015-11-22
      相关资源
      最近更新 更多