C Programming Code Examples C > Bitwise Operators Code Examples C program to count trailing zeros in a binary number C program to count trailing zeros in a binary number Write a C program to input any number from user and count number of trailing zeros in the given number using bitwise operator. How to find total number of trailing zeros in any given number using bitwise operator in C programming. Input number from user. Store it in some variable say, j. Find total bits required to store an integer in memory say, INT_SIZE = sizeof(int) * 8. Initialize a variable to store trailing zeros count, say count = 0;. Run a loop from 0 to INT_SIZE. The loop structure should look like for(i=0; i<INT_SIZE; i++). Inside the loop if ith bit is set then terminate from loop; otherwise increment count by 1. /* C program to count trailing zeros in a binary number using bitwise operator */ #include <stdio.h> #define INT_SIZE sizeof(int) * 8 /* Bits required to represent an integer */ int main() { int j, count, i; /* Input number from user */ printf("Enter any number: "); scanf("%d", &j); count = 0; /* Iterate over each bit of the number */ for(i=0; i<INT_SIZE; i++) { /* If set bit is found the terminate from loop*/ if((j >> i ) & 1) { /* Terminate from loop */ break; } /* Increment trailing zeros count */ count++; } printf("Total number of trailing zeros in %d is %d.", j, count); return 0; }