# 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 = 16Output :3 Binary representation of N is 1000. Therefore, number of zeroes at the end is 3.Input: N = 8Output: 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 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; ` `} ` |

*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 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 ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*..
- Count number of trailing zeros in product of array
- Number of leading zeros in binary representation of a given number
- Smallest number divisible by n and has at-least k trailing zeros
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Number of trailing zeroes in base B representation of N!
- Count unique numbers that can be generated from N by adding one and removing trailing zeros
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Count trailing zeroes in factorial of a number
- Count number of set bits in a range using bitset
- Count number of common elements between two arrays by using Bitset and Bitwise operation
- Binary representation of a given number
- Binary representation of previous number
- Next greater number than N with exactly one bit different in binary representation of N
- Largest number with binary representation is m 1's and m-1 0's

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.