Count number of trailing zeros in Binary representation of a number using Bitset
Given a number. The task is to count the number of Trailing Zero in Binary representation of a number using bitset.
Examples:
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
Approach: We simply set the number in the bitset and then we iterate from 0 index of bitset, as soon as we get 1 we will break the loop because there is no trailing zero after that.
Below is the implementation of above approach:
C++
// C++ program to count number of trailing zeros// in Binary representation of a number// using Bitset#include <bits/stdc++.h>using namespace std;// Function to count number of trailing zeros in// Binary representation of a number// using Bitsetint 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 Codeint main(){ int n = 4; int ans = CountTrailingZeros(n); cout << ans << "\n"; return 0;} |
chevron_right
filter_none
Java
// Java program to count number of trailing zeros// in Binary representation of a number// using Bitsetimport 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 |
chevron_right
filter_none
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 numberdef 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 Coden = 4ans = CountTrailingZeros(n)print(ans)# This code is contributed # by Mohit Kumar |
chevron_right
filter_none
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 |
chevron_right
filter_none
Output:
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

