Open in App
Not now

# Count trailing zeroes present in binary representation of a given number using XOR

• Difficulty Level : Medium
• Last Updated : 09 Jun, 2021

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 = 12
Output: 2
Explanation:
The binary representation of the number 13 is “1100”.
Therefore, there are two trailing zeros in the 12.

Input: N = -56
Output: 3
Explanation:
The binary representation of the number -56 is “001000”.
Therefore, there are 3 trailing zeros present in -56.

Approach: Follow the steps to solve the problem

• The idea is to use the observation that after calculating XOR of N with N – 1, all the set bit of N left to the rightmost set bit, i.e LSB set bit disappears and the rightmost set bit of N becomes the leftmost set bit of N ^ (N – 1).
• Print the count of bits of a number (N ^ (N – 1)) decremented by 1.

Below is the implementation of the above approach:

## C++

 `// C++ implementation` `// of the above approach`   `#include ` `using` `namespace` `std;`   `// Function to print count of` `// trailing zeroes present in` `// binary representation of N` `int` `countTrailingZeroes(``int` `N)` `{` `    ``// Count set bits in (N ^ (N - 1))` `    ``int` `res = log2(N ^ (N - 1));`   `    ``// If res < 0, return 0` `    ``return` `res >= 0 ? res : 0;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `N = 12;`   `    ``// Function call to print the count` `    ``// of trailing zeroes in the binary` `    ``// representation of N` `    ``cout << countTrailingZeroes(N);`   `    ``return` `0;` `}`

## Java

 `// Java implementation` `// of the above approach` `import` `java.io.*;`   `class` `GFG {`   `    ``// Function to print count of` `    ``// trailing zeroes present in` `    ``// binary representation of N` `    ``public` `static` `int` `countTrailingZeroes(``int` `N)` `    ``{` `        ``// Stores XOR of N and (N-1)` `        ``int` `res = N ^ (N - ``1``);`   `        ``// Return count of set bits in res` `        ``return` `(``int``)(Math.log(temp)` `                     ``/ Math.log(``2``));` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `N = ``12``;`   `        ``// Function call to print the count` `        ``// of trailing zeroes in the binary` `        ``// representation of N` `        ``System.out.println(` `            ``countTrailingZeroes(N));` `    ``}` `}`

## Python3

 `# Python3 implementation` `# of the above approach` `from` `math ``import` `log2`   `# Function to print count of` `# trailing zeroes present in` `# binary representation of N` `def` `countTrailingZeroes(N):` `  `  `    ``# Count set bits in (N ^ (N - 1))` `    ``res ``=` `int``(log2(N ^ (N ``-` `1``)))`   `    ``# If res < 0, return 0` `    ``return` `res ``if` `res >``=` `0` `else` `0`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    ``N ``=` `12`   `    ``# Function call to print the count` `    ``# of trailing zeroes in the binary` `    ``# representation of N` `    ``print` `(countTrailingZeroes(N))`   `    ``# This code is contributed by mohit kumar 29.`

## C#

 `// C# implementation` `// of the above approach` `using` `System;` `public` `class` `GFG{`   `  ``// Function to print count of` `  ``// trailing zeroes present in` `  ``// binary representation of N` `  ``public` `static` `int` `countTrailingZeroes(``int` `N)` `  ``{` `    ``// Stores XOR of N and (N-1)` `    ``int` `res = (``int``)Math.Log(N ^ (N - 1), 2.0);`   `    ``// Return count of set bits in res` `    ``if``(res >= 0)` `      ``return` `res;` `    ``else` `      ``return` `0;` `  ``}`   `  ``// Driver Code` `  ``static` `public` `void` `Main ()` `  ``{`   `    ``int` `N = 12;`   `    ``// Function call to print the count` `    ``// of trailing zeroes in the binary` `    ``// representation of N` `    ``Console.WriteLine(` `      ``countTrailingZeroes(N));` `  ``}` `}`   `// This code is contributed by Dharanendra L V.`

## Javascript

 ``

Output:

`2`

Time Complexity: O(log(N))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles