📜  使用查找表计算尾随零位

📅  最后修改于: 2021-05-25 03:05:30             🧑  作者: Mango

给定一个整数,计算尾随零的数量。例如,对于n = 12,其二进制表示为1100,尾随零位的数目为2。

例子 :

Input : 8
Output : 3
Binary of 8 is 1000, so there are theree
trailing zero bits.

Input : 18
Output : 1
Binary of 18 is 10010, so there is one
trailing zero bit.

一种简单的解决方案是遍历LSB(最低有效位)中的位并在位为0时递增计数。

C++
// Simple C++ code for counting trailing zeros
// in binary representation of a number
#include
using namespace std;
 
int countTrailingZero(int x)
{
  int count = 0;
  while ((x & 1) == 0)
  {
      x = x >> 1;
      count++;
  }
  return count;
}
 
// Driver Code
int main()
{
    cout << countTrailingZero(11) << endl;
    return 0;
}


Java
// Simple Java code for counting
// trailing zeros in binary
// representation of a number
import java.io.*;
 
class GFG
{
    public static int countTrailingZero(int x)
    {
        int count = 0;
         
        while ((x & 1) == 0)
        {
            x = x >> 1;
            count++;
        }
        return count;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
         
        System.out.println(countTrailingZero(11));
    }
}
 
// This code is contributed by ajit


Python3
# Python 3 code for counting trailing zeros
# in binary representation of a number
def countTrailingZero(x):
    count = 0
    while ((x & 1) == 0):
        x = x >> 1
        count += 1
     
    return count
 
# Driver Code
if __name__ == '__main__':
    print(countTrailingZero(11))
     
# This code is contributed by
# Sanjit_Prasad


C#
// Simple C# code for counting
// trailing zeros in binary
// representation of a number
using System;
 
class GFG
{
    public static int countTrailingZero(int x)
    {
        int count = 0;
         
        while ((x & 1) == 0)
        {
            x = x >> 1;
            count++;
        }
        return count;
    }
     
    // Driver Code
    static public void Main ()
    {
        Console.WriteLine(countTrailingZero(11));
    }
}
 
// This code is contributed by aj_36


PHP
> 1;
        $count++;
    }
    return $count;
}
 
    // Driver Code
    echo countTrailingZero(11),"\n";
     
// This code is contributed by ajit
?>


Javascript


C++
// C++ code for counting trailing zeros
// in binary representation of a number
#include
using namespace std;
 
int countTrailingZero(int x)
{
     // Map a bit value mod 37 to its position
     static const int lookup[] = {32, 0, 1,
     26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11,
     0, 13, 4, 7, 17, 0, 25, 22, 31, 15, 29,
     10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19,
     18};
 
     // Only difference between (x and -x) is
     // the value of signed magnitude(leftmostbit)
     // negative numbers signed bit is 1
     return lookup[(-x & x) % 37];
}
 
// Driver Code
int main()
{
    cout << countTrailingZero(48) << endl;
    return 0;
}


Java
// Java code for counting
// trailing zeros in binary
// representation of a number
import java.io.*;
 
class GFG
{
static int countTrailingZero(int x)
{
     
    // Map a bit value mod
    // 37 to its position
    int lookup[] = {32, 0, 1, 26, 2, 23, 
                    27, 0, 3, 16, 24, 30,
                    28, 11, 0, 13, 4, 7,
                    17, 0, 25, 22, 31, 15,
                    29, 10, 12, 6, 0, 21,
                    14, 9, 5, 20, 8, 19, 18};
 
    // Only difference between
    // (x and -x) is the value
    // of signed magnitude
    // (leftmostbit) negative
    // numbers signed bit is 1
    return lookup[(-x & x) % 37];
}
 
// Driver Code
public static void main (String[] args)
{
    System.out.println(countTrailingZero(48));
}
}
 
// This code is contributed
// by ajit


Python3
# Python3 code for counting trailing zeros
# in binary representation of a number
 
def countTrailingZero(x):
 
    # Map a bit value mod 37 to its position
    lookup = [32, 0, 1, 26, 2, 23, 27, 0,
              3, 16, 24, 30, 28, 11, 0, 13,
              4, 7, 17, 0, 25, 22, 31, 15,
              29, 10, 12, 6, 0, 21, 14, 9,
              5, 20, 8, 19, 18]
 
    # Only difference between (x and -x) is
    # the value of signed magnitude(leftmostbit)
    # negative numbers signed bit is 1
    return lookup[(-x & x) % 37]
 
# Driver Code
if __name__ == "__main__":
 
    print(countTrailingZero(48))
 
# This code is contributed
# by Rituraj Jain


C#
// C# code for counting
// trailing zeros in binary
// representation of a number
using System;
 
class GFG
{
static int countTrailingZero(int x)
{
     
    // Map a bit value mod
    // 37 to its position
    int []lookup = {32, 0, 1, 26, 2, 23,
                    27, 0, 3, 16, 24, 30,
                    28, 11, 0, 13, 4, 7,
                    17, 0, 25, 22, 31, 15,
                    29, 10, 12, 6, 0, 21,
                    14, 9, 5, 20, 8, 19, 18};
 
    // Only difference between
    // (x and -x) is the value
    // of signed magnitude
    // (leftmostbit) negative
    // numbers signed bit is 1
    return lookup[(-x & x) % 37];
}
 
// Driver Code
static public void Main ()
{
    Console.WriteLine(countTrailingZero(48));
}
}
 
// This code is contributed
// by m_kit


PHP


Javascript


输出 :

0

时间复杂度: O(Log n)查找表解决方案基于以下概念:

  1. 该解决方案假定负数以2的补码形式存储,这对于大多数设备而言都是正确的。如果数字以2的补码形式表示,则(x&-x)[x的x和-x的按位与]生成仅具有最后一个置位的数字。
  2. 一旦获得仅设置一位的数字,就可以使用查找表找到其位置。它利用了以下事实:前32位位置值相对于37来说是质数,因此对37进行模数除法可得到每个值的从0到36的唯一数字。然后,可以使用小的查找表将这些数字映射为零。

C++

// C++ code for counting trailing zeros
// in binary representation of a number
#include
using namespace std;
 
int countTrailingZero(int x)
{
     // Map a bit value mod 37 to its position
     static const int lookup[] = {32, 0, 1,
     26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11,
     0, 13, 4, 7, 17, 0, 25, 22, 31, 15, 29,
     10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19,
     18};
 
     // Only difference between (x and -x) is
     // the value of signed magnitude(leftmostbit)
     // negative numbers signed bit is 1
     return lookup[(-x & x) % 37];
}
 
// Driver Code
int main()
{
    cout << countTrailingZero(48) << endl;
    return 0;
}

Java

// Java code for counting
// trailing zeros in binary
// representation of a number
import java.io.*;
 
class GFG
{
static int countTrailingZero(int x)
{
     
    // Map a bit value mod
    // 37 to its position
    int lookup[] = {32, 0, 1, 26, 2, 23, 
                    27, 0, 3, 16, 24, 30,
                    28, 11, 0, 13, 4, 7,
                    17, 0, 25, 22, 31, 15,
                    29, 10, 12, 6, 0, 21,
                    14, 9, 5, 20, 8, 19, 18};
 
    // Only difference between
    // (x and -x) is the value
    // of signed magnitude
    // (leftmostbit) negative
    // numbers signed bit is 1
    return lookup[(-x & x) % 37];
}
 
// Driver Code
public static void main (String[] args)
{
    System.out.println(countTrailingZero(48));
}
}
 
// This code is contributed
// by ajit

Python3

# Python3 code for counting trailing zeros
# in binary representation of a number
 
def countTrailingZero(x):
 
    # Map a bit value mod 37 to its position
    lookup = [32, 0, 1, 26, 2, 23, 27, 0,
              3, 16, 24, 30, 28, 11, 0, 13,
              4, 7, 17, 0, 25, 22, 31, 15,
              29, 10, 12, 6, 0, 21, 14, 9,
              5, 20, 8, 19, 18]
 
    # Only difference between (x and -x) is
    # the value of signed magnitude(leftmostbit)
    # negative numbers signed bit is 1
    return lookup[(-x & x) % 37]
 
# Driver Code
if __name__ == "__main__":
 
    print(countTrailingZero(48))
 
# This code is contributed
# by Rituraj Jain

C#

// C# code for counting
// trailing zeros in binary
// representation of a number
using System;
 
class GFG
{
static int countTrailingZero(int x)
{
     
    // Map a bit value mod
    // 37 to its position
    int []lookup = {32, 0, 1, 26, 2, 23,
                    27, 0, 3, 16, 24, 30,
                    28, 11, 0, 13, 4, 7,
                    17, 0, 25, 22, 31, 15,
                    29, 10, 12, 6, 0, 21,
                    14, 9, 5, 20, 8, 19, 18};
 
    // Only difference between
    // (x and -x) is the value
    // of signed magnitude
    // (leftmostbit) negative
    // numbers signed bit is 1
    return lookup[(-x & x) % 37];
}
 
// Driver Code
static public void Main ()
{
    Console.WriteLine(countTrailingZero(48));
}
}
 
// This code is contributed
// by m_kit

的PHP


Java脚本


输出 :

4

时间复杂度: O(1)
来源 :
https://graphics.stanford.edu/~seander/bithacks.html