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.

Example

Input

Input any number: 22

Output

Trailing zeros: 1

Required knowledge

Bitwise operators, Data types, Variables and Expressions, Basic input/output, If else, For loop

Logic to count trailing zeros in a binary number

Number of trailing zeros in a binary number is equal to first set bit position.

Trailing zeros in binary number

Step by step descriptive logic to count trailing zeros in a binary number.

  1. Input number from user. Store it in some variable say, num.
  2. Find total bits required to store an integer in memory say, INT_SIZE = sizeof(int) * 8.

    Read more - How to find size of a data type using sizeof() operator.

  3. Initialize a variable to store trailing zeros count, say count = 0;.
  4. Run a loop from 0 to INT_SIZE. The loop structure should look like for(i=0; i<INT_SIZE; i++).
  5. Inside the loop if ith bit is set then terminate from loop; otherwise increment count by 1.

Program to count trailing zeros in a binary number

/**
 * 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 num, count, i;

    /* Input number from user */
    printf("Enter any number: ");
    scanf("%d", &num);

    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((num >> i ) & 1)
        {
            /* Terminate from loop */
            break;
        }

        /* Increment trailing zeros count */
        count++;
    }

    printf("Total number of trailing zeros in %d is %d.", num, count);

    return 0;
}

You can also short the above program using while loop.

Program to count trailing zeros in a binary number using while loop

/**
 * C program to count trailing zeros in a binary number using bitwise operator
 */

#include <stdio.h>

int main()
{
    int num, count=0;

    /* Input number from user */
    printf("Enter any number: ");
    scanf("%d", &num);

    while(!(num & 1))
    {
        count++;
        num >>= 1;
    }

    printf("Total number of trailing zeros = %d.", count);

    return 0;
}

Important note: The statement num >>= 1; is equivalent to num = num >> 1;.

Read more about - Shorthand assignment operators in C.

while(!(num & 1)) is equivalent to while((num & 1) == 0).

Learn more - Program to count leading zeros in a binary number

Output

Enter any number: 48
Total number of trailing zeros in 48 is 4.

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>