Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program to count trailing and leading zeros in a binary number
In C programming, counting trailing and leading zeros in a binary representation of a number is a common bit manipulation task. Trailing zeros are the consecutive zeros from the least significant bit (LSB) until the first set bit, while leading zeros are the consecutive zeros from the most significant bit (MSB) until the first set bit.
Syntax
/* For trailing zeros */ int countTrailingZeros(int number); /* For leading zeros */ int countLeadingZeros(int number);
Trailing Zeros
Trailing zeros are the zeros that appear after the rightmost set bit (1) when counting from the LSB. For example, the decimal number 104 has binary representation 1101000, which has 3 trailing zeros.
Example: Count Trailing Zeros
#include <stdio.h>
int main() {
int number = 104;
int trailingZeros = 0;
int size = sizeof(number) * 8;
printf("Decimal number: %d<br>", number);
printf("Binary representation: ");
/* Print binary representation */
for (int i = size - 1; i >= 0; i--) {
printf("%d", (number >> i) & 1);
}
printf("<br>");
/* Count trailing zeros */
for (int i = 0; i < size; i++) {
if ((number >> i) & 1) {
break;
}
trailingZeros++;
}
printf("Number of trailing zeros: %d<br>", trailingZeros);
return 0;
}
Decimal number: 104 Binary representation: 00000000000000000000000001101000 Number of trailing zeros: 3
Leading Zeros
Leading zeros are the zeros that appear before the leftmost set bit (1) when counting from the MSB. For a 32-bit integer, if the first set bit is at position n from the left, then there are n leading zeros.
Example: Count Leading Zeros
#include <stdio.h>
int main() {
int number = 94;
int leadingZeros = 0;
int size = sizeof(number) * 8;
int msb = 1 << (size - 1);
printf("Decimal number: %d<br>", number);
printf("Binary representation: ");
/* Print binary representation */
for (int i = size - 1; i >= 0; i--) {
printf("%d", (number >> i) & 1);
}
printf("<br>");
/* Count leading zeros */
for (int i = 0; i < size; i++) {
if ((number << i) & msb) {
break;
}
leadingZeros++;
}
printf("Number of leading zeros: %d<br>", leadingZeros);
return 0;
}
Decimal number: 94 Binary representation: 00000000000000000000000001011110 Number of leading zeros: 25
Combined Example
Here's a program that counts both trailing and leading zeros for any given number −
#include <stdio.h>
void countZeros(int number) {
int size = sizeof(number) * 8;
int trailingZeros = 0, leadingZeros = 0;
int msb = 1 << (size - 1);
/* Count trailing zeros */
for (int i = 0; i < size; i++) {
if ((number >> i) & 1) break;
trailingZeros++;
}
/* Count leading zeros */
for (int i = 0; i < size; i++) {
if ((number << i) & msb) break;
leadingZeros++;
}
printf("Number: %d<br>", number);
printf("Trailing zeros: %d<br>", trailingZeros);
printf("Leading zeros: %d<br>", leadingZeros);
printf("-------------------<br>");
}
int main() {
countZeros(104);
countZeros(94);
countZeros(8);
return 0;
}
Number: 104 Trailing zeros: 3 Leading zeros: 24 ------------------- Number: 94 Trailing zeros: 1 Leading zeros: 25 ------------------- Number: 8 Trailing zeros: 3 Leading zeros: 28 -------------------
Conclusion
Counting trailing and leading zeros involves bit manipulation techniques using shift operations and bitwise AND. These methods efficiently determine the position of the first set bit from either end of the binary representation.
