
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C Program to count trailing and leading zeros in a binary number
To begin with, let us understand what are trailing zeros in a binary number.
Trailing zeros
The position of zeros after first one from the least significant bit (LSB) is called as trailing zeros in binary number.
Example
104 is decimal number
Binary number of 104 is: (MSB) 1101000(LSB)
Here,
- MSB refers to Most Significant Bit.
- LSB refers to Least Significant Bit.
- From LSB after the first bit set, there are three zero.
- The number of trailing zeros is three.
Example
Following is the program to count number of trailing zeros for a given number −
#include<stdio.h> #include<stdlib.h> int main(){ int number, i, trail = 0, size; printf("Enter a number
"); scanf("%d",&number); size = sizeof(number) * 8; for(i = 0; i < size; i++){ if((number >> i) & 1) { break; } trail++; } printf("Number of trailing ZERO is = %d", trail); return 0; }
Output
When the above program is executed, it produces the following result −
Enter a number 24 Number of trailing ZERO is = 3
Leading zeros
If the position of zero before bit is set to one, they are termed as leading zeros.
Example
94 is decimal number.
Binary number of 94 is: (MSB) .....001011110(LSB)
Number of Leading ZERO is = 25
Program
Given below is the program to count number of leading zeros for a given number.
#include<stdio.h> #include<stdlib.h> int main(){ int number, i, lead = 0, Msb,size; printf("Enter a number
"); scanf("%d",&number); size = sizeof(number) * 8; Msb=1<<(size-1); for(i = 0; i < size; i++){ if((number << i) & Msb) { break; } lead++; } printf("Number of Leading ZERO is = %d", lead); return 0; }
Output
When the above program is executed, it produces the following result −
Enter a number 94 Number of Leading ZERO is = 25
- Related Articles
- Count number of trailing zeros in Binary representation of a number using Bitset in C++
- Count trailing zeros in factorial of a number in C++
- Number of leading zeros in binary representation of a given number in C++
- Count number of trailing zeros in product of array in C++
- Java Program to add leading zeros to a number
- C/C++ Program to Count trailing zeroes in factorial of a number?
- Java Program to remove leading and trailing whitespace from a string
- Java Program to Remove leading zeros
- Golang program to remove leading zeros
- Trim (Remove leading and trailing spaces) a string in C#
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*.. in C++
- Python Program to Count trailing zeroes in factorial of a number
- Java Program to Count trailing zeroes in factorial of a number
- Java Program to remove the leading and trailing quotes from a string
- How to remove leading zeros from a number in JavaScript?

Advertisements