top button
Flag Notify
Site Registration

WAP in C to count trailing zeros using bitwise operator?

+3 votes
483 views
WAP in C to count trailing zeros using bitwise operator?
posted Jan 30, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Can you provide the input and their expected output.
What I understand is, if you have a number assumes 14000 and the expected ans will be 3. i.e three zero's.
Correct me if I am wrong.

1 Answer

+1 vote

Try this program :

#include<bits/stdc++.h>
using namespace std;
void PrintBinaryOfNumber_techQH(int num)
{
   int mask = 0x4000;
   if ((num & 0x8000) == 0)
      printf("0");
   else
      printf("1");
   while (mask != 0) {
      if ((num & mask) == 0)
         printf("0");
      else
         printf("1");
      mask = mask >> 1;
   }
}
int main()
{
   int i, count = 0;
   unsigned int num;
   printf("\nEnter the number:");
   scanf("%d", &num);
   printf("\nDecimal number in binary format :");
   PrintBinaryOfNumber_techQH(num);
   while (num != 0) {
      if (num & 1 == 1) {
         break;
      } 
else 
     {
         count++;
         num = num >> 1;
      }
   }
   printf("\nTrailing Zeros : %d", count);
return 0;
}
answer Apr 28, 2016 by Shivam Kumar Pandey
...