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 : 4
Binary representation of N is 10000. Therefore,
number of zeroes at the end is 4.
Input : N = 8
Output : 3
Approach: 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++
#include <bits/stdc++.h>
using namespace std;
int CountTrailingZeros( int n)
{
bitset<64> bit;
bit |= n;
int zero = 0;
for ( int i = 0; i < 64; i++) {
if (bit[i] == 0)
zero++;
else
break ;
}
return zero;
}
int main()
{
int n = 4;
int ans = CountTrailingZeros(n);
cout << ans << "\n" ;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
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++;
else
break ;
}
return zero;
}
public static void main(String []args)
{
int n = 4 ;
int ans = CountTrailingZeros(n);
System.out.println(ans);
}
}
|
Python3
def CountTrailingZeros(n):
bit = bin (n)[ 2 :]
bit = bit[:: - 1 ]
zero = 0 ;
for i in range ( len (bit)):
if (bit[i] = = '0' ):
zero + = 1
else :
break
return zero
n = 4
ans = CountTrailingZeros(n)
print (ans)
|
C#
using System;
class GFG
{
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++;
}
else
{
break ;
}
}
return zero;
}
static public void Main ()
{
int n = 4;
int ans = CountTrailingZeros(n);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
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++;
else
break ;
}
return zero;
}
let n = 4;
let ans = CountTrailingZeros(n);
document.write(ans);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach: Bitwise Operation
- To 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 {
static int countTrailingZeroes( int n) {
int count = 0 ;
while ((n & 1 ) == 0 ) {
count += 1 ;
n >>= 1 ;
}
return count;
}
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
def count_trailing_zeroes(n):
count = 0
while n & 1 = = 0 :
count + = 1
n >> = 1
return count
n1 = 16
print (count_trailing_zeroes(n1))
|
C#
using System;
namespace CountTrailingZeroes
{
class Program
{
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;
}
const n1 = 16;
console.log(countTrailingZeroes(n1));
|
Time Complexity: O(log N), where N is the value of the input number n.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
26 Oct, 2023
Like Article
Save Article