Given an integer N, the task is to find the number of trailing zeroes in the binary representation of the given number.
Input: N = 12
The binary representation of the number 13 is “1100”.
Therefore, there are two trailing zeros in the 12.
Input: N = -56
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:
Time Complexity: O(log(N))
Auxiliary Space: O(1)
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.