Count number of trailing zeros in Binary representation of a number using Bitset Last Updated : 07 May, 2025 Comments Improve Suggest changes Like Article Like Report Given a number. The task is to count the number of Trailing Zero in Binary representation of a number using bitset.Examples: Input : N = 16Output : 4Binary representation of N is 10000. Therefore,number of zeroes at the end is 4.Input : N = 8Output : 3Approach: We simply set the number in the bitset and then we iterate from 0 indexes 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 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 Python # 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 JavaScript <script> // JavaScript program to count number of trailing zeros // in Binary representation of a number // using Bitset // Function to count number of trailing zeros in // Binary representation of a number // using Bitset function CountTrailingZeros(n) { let bit = n.toString(2); let bit1=bit.split(""); bit1=bit1.reverse(); let zero = 0; for (let i = 0; i < 64; i++) { if (bit1[i] == '0') zero++; // if '1' comes then break else break; } return zero; } // Driver Code let n = 4; let ans = CountTrailingZeros(n); document.write(ans); // This code is contributed by rag2127 </script> Output2Time Complexity: O(1)Auxiliary Space: O(1)Approach: Bitwise OperationTo count the number of trailing zeroes in the binary representation of a number using bitwise operations.We can repeatedly right-shift the number by 1 until the least significant bit (LSB) becomes 1. The number of right shifts performed will give us the count of trailing zeroes.Below is the implementation of above approach: C++ #include <iostream> using namespace std; int count_trailing_zeroes(int n) { int count = 0; while ((n & 1) == 0) { count += 1; n >>= 1; } return count; } int main() { int n1 = 16; cout << count_trailing_zeroes(n1) << endl; return 0; } Java import java.util.Scanner; public class GFG { // Function to count trailing zeroes in a number static int countTrailingZeroes(int n) { int count = 0; while ((n & 1) == 0) { count += 1; n >>= 1; } return count; } // Driver Code public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter an integer: "); int n1 = 16; System.out.println("Number of trailing zeroes: " + countTrailingZeroes(n1)); } } Python # Python program to count # number of trailing zeros in # Binary representation of a number def count_trailing_zeroes(n): count = 0 while n & 1 == 0: count += 1 n >>= 1 return count # Driver Code n1 = 16 print(count_trailing_zeroes(n1)) C# using System; namespace CountTrailingZeroes { class Program { // Function to count trailing zeroes in a number static int CountTrailingZeroes(int n) { int count = 0; while ((n & 1) == 0) { count += 1; n >>= 1; } return count; } static void Main(string[] args) { int n1 = 16; Console.WriteLine(CountTrailingZeroes(n1)); } } } JavaScript function countTrailingZeroes(n) { let count = 0; while ((n & 1) === 0) { count += 1; n >>= 1; } return count; } // Example usage const n1 = 16; console.log(countTrailingZeroes(n1)); Output4Time Complexity: O(log N), where N is the value of the input number n.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Count number of trailing zeros in Binary representation of a number using Bitset S souradeep Follow Improve Article Tags : Bit Magic Mathematical DSA number-theory Practice Tags : Bit MagicMathematicalnumber-theory Similar Reads Count trailing zeroes present in binary representation of a given number using XOR Given an integer N, the task is to find the number of trailing zeroes in the binary representation of the given number. Examples: Input: N = 12Output: 2Explanation:The binary representation of the number 13 is "1100".Therefore, there are two trailing zeros in the 12. Input: N = -56Output: 3Explanati 4 min read Number of leading zeros in binary representation of a given number Given a positive integer N, the task is to find the number of leading zeros in its binary representation.A leading zero is any 0 digit that comes before the first nonzero digit in a number's binary form. Examples: Input : N = 16Output : 27Explanation: As Binary(16) = (0000000000000000000000000001000 10 min read Count number of 0s in base K representation of a number Given a number N, the task is to find the number of zero's in base K representation of the given number, where K > 1. Examples: Input: N = 10, K = 3Output: 1Explanation: Base 3 representation of 10 is 101. Hence the number of 0's in 101 is 1. Input: N = 8, K = 2Output: 3Explanation: Base 2 repres 5 min read Comparing leading zeros in binary representations of two numbers Given two integer numbers x and y. Compare and print which one of them has more leading zeros using Bitwise operation. If both the no. have the same no. of leading zeros, print "Equal".Note:- A leading zero is any 0 digit that comes before the first nonzero digit in the binary notation of the number 8 min read Count number of set bits in a range using bitset Given a large binary number.The task is to count the number of 1's in a given range from L to R (1 based indexing).Examples: Input : s = "101101011010100000111", L = 6, R = 15 Output : 5 s [L : R] = "1011010100" There is only 5 set bits.Input : s = "10110", L = 2, R = 5 Output : 2 Approach: Convert 5 min read Count of numbers in range [L, R] with LSB as 0 in their Binary representation Given two integers L and R. The task is to find the count of all numbers in the range [L, R] whose Least Significant Bit in binary representation is 0. Examples: Input: L = 10, R = 20 Output: 6 Input: L = 7, R = 11 Output: 2 Naive approach: The simplest approach is to solve this problem is to check 5 min read Count of N-bit binary numbers without leading zeros Given an integer N, the task is to find the count of N-bit binary numbers without leading zeros.Examples: Input: N = 2 Output: 2 10 and 11 are the only possible binary numbers.Input: N = 4 Output: 8 Approach: Since the numbers cannot have leading zeros so the left-most bit has to be set to 1. Now fo 2 min read Binary representation of a given number Given an integer n, the task is to print the binary representation of the number. Note: The given number will be maximum of 32 bits, so append 0's to the left if the result string is smaller than 30 length.Examples: Input: n = 2Output: 00000000000000000000000000000010Input: n = 0Output: 000000000000 6 min read Check if binary representations of 0 to N are present as substrings in given binary string Give binary string str and an integer N, the task is to check if the substrings of the string contain all binary representations of non-negative integers less than or equal to the given integer N. Examples: Input: str = â0110", N = 3 Output: True Explanation: Since substrings â0", â1", â10", and â11 9 min read Count unique numbers that can be generated from N by adding one and removing trailing zeros Given a number N. Add one to the number in the first step and if the number has trailing zeros, remove all the trailing zeros in the second step. Continue the process for the next generated number. The task is to count the number of unique numbers that can be generated from these operations. Example 4 min read Like