📌  相关文章
📜  使用Bitset计算数字的二进制表示形式中的尾随零数

📅  最后修改于: 2021-04-27 06:09:57             🧑  作者: Mango

给出一个数字。任务是使用位集对数字的二进制表示形式中的尾随零进行计数。
例子:

Input : N = 16
Output : 3
Binary representation of N is 1000. Therefore,
number of zeroes at the end is 3.

Input : N = 8
Output : 2

方法:我们只需在位集中设置数字,然后从位集的0索引进行迭代,一旦我们得到1,我们将中断循环,因为在此之后没有尾随零。
下面是上述方法的实现:

C++
// C++ program to count number of trailing zeros
// in Binary representation of a number
// using Bitset
 
#include 
using namespace std;
 
// Function to count number of trailing zeros in
// Binary representation of a number
// using Bitset
int CountTrailingZeros(int n)
{
    // declare bitset of 64 bits
    bitset<64> bit;
 
    // set bitset with the value
    bit |= n;
 
    int zero = 0;
 
    for (int i = 0; i < 64; i++) {
        if (bit[i] == 0)
            zero++;
        // if '1' comes then break
        else
            break;
    }
 
    return zero;
}
 
// Driver Code
int main()
{
    int n = 4;
 
    int ans = CountTrailingZeros(n);
 
    cout << ans << "\n";
 
    return 0;
}


Java
// Java program to count number of trailing zeros
// in Binary representation of a number
// using Bitset
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
      
    // Function to count number of trailing zeros in
    // Binary representation of a number
    // using Bitset
    static int CountTrailingZeros(int n)
    {
         
        String bit = Integer.toBinaryString(n);
        StringBuilder bit1 = new StringBuilder();
        bit1.append(bit);
        bit1=bit1.reverse();
        int zero = 0;
      
        for (int i = 0; i < 64; i++) {
            if (bit1.charAt(i) == '0')
                zero++;
            // if '1' comes then break
            else
                break;
        }
      
        return zero;
    }
      
    // Driver Code
    public static void main(String []args)
    {
        int n = 4;
      
        int ans = CountTrailingZeros(n);
      
        System.out.println(ans);
    }
}
 
// This code is contributed by chitranayal


Python3
# Python3 program to count
# number of trailing zeros in
# Binary representation of a number
 
# Function to count number of
# trailing zeros in Binary
# representation of a number
def CountTrailingZeros(n):
     
    # declare bitset of 64 bits
    bit = bin(n)[2:]
    bit = bit[::-1]
 
    zero = 0;
 
    for i in range(len(bit)):
        if (bit[i] == '0'):
            zero += 1
             
        # if '1' comes then break
        else:
            break
 
    return zero
 
# Driver Code
n = 4
 
ans = CountTrailingZeros(n)
 
print(ans)
 
# This code is contributed
# by Mohit Kumar


C#
// C# program to count number of trailing zeros
// in Binary representation of a number
// using Bitset
using System;
class GFG
{
 
  // Function to count number of trailing zeros in
  // Binary representation of a number
  // using Bitset
  static int CountTrailingZeros(int n)
  {
    string bit=Convert.ToString(n, 2);
    char[] charArray = bit.ToCharArray();
    Array.Reverse( charArray );
    string bit1 = new string( charArray );
    int zero = 0;
    for (int i = 0; i < 64; i++)
    {
      if (bit1[i] == '0')
      {
        zero++;
      }
 
      // if '1' comes then break
      else
      {
        break;
      }
    }
    return zero;
  }
 
  // Driver Code
  static public void Main ()
  {
    int n = 4;
    int ans = CountTrailingZeros(n);
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by avanitrachhadiya2155


输出:
2