How to use the Leading zeros Calculator tool
- Enter the word in the Binary digits box, padded with zeros to the width you care about, such as 8, 16 or 32 bits.
- Select Convert to count, or Example to try the sample word 01001101.
- Read the number of leading zero bits in the Result box.
- Select Copy to copy the count, or Clear to empty the form.
What count leading zeros means
Count leading zeros, usually shortened to CLZ, is the number of consecutive 0 bits at the most significant end of a word, before the first 1 appears. It is a property of a value at a given width. The number 77 has one leading zero as the 8-bit word 01001101, nine as a 16-bit word and 25 as a 32-bit word.
This tool has no separate width setting. It scans the digits from the left exactly as entered and stops at the first 1, so the width is simply the length of your input. Type every leading zero that the real register would hold.
Worked example at three widths
The same value gives a different count each time the word grows, because every extra digit on the left is one more leading zero. A word that starts with 1 always returns 0.
01001101 → 1
0000000001001101 → 9
00000000000000000000000001001101 → 25
1001101 → 0Zero input and other edge cases
When the word contains no 1 at all, the calculator returns the full length: 0000 gives 4 and sixteen zeros give 16. That matches the x86 LZCNT instruction, the ARM CLZ instruction, C++20 std::countl_zero and Rust leading_zeros, which all return the operand width for zero. It differs from the GCC and Clang builtin __builtin_clz, whose result for zero is undefined.
- A 0b prefix is removed before counting and is not part of the width.
- Spaces and line breaks between groups are ignored, so 0000 0000 0100 1101 counts as 16 bits.
- After cleanup the input must be 1 to 65,536 characters of 0 and 1. Other characters trigger the error Enter between 1 and 65,536 binary digits.
Where leading zero counts are used
CLZ is the standard building block for an integer base-2 logarithm: for a nonzero w-bit value, floor(log2(x)) is w - 1 - clz(x). Floating-point code uses it to normalize a significand, shifting left by the leading zero count so the top bit is set. Variable-length integer encoders use it to decide how many bytes a value needs, and software division routines use it to align the divisor with the dividend.
JavaScript exposes the 32-bit form as Math.clz32, Java as Integer.numberOfLeadingZeros and C23 as stdc_leading_zeros.
Frequently asked questions
How do I count leading zeros for a 32-bit integer?
Write the value as exactly 32 binary digits, adding zeros on the left, and paste it in. The tool counts from the first digit you typed, so 00000000000000000000000001001101 returns 25. Without the padding the same value returns a smaller count.
What is the leading zero count of zero?
This calculator returns the word length, for example 8 for 00000000. Many CPU instructions and library functions do the same. Be careful in C: __builtin_clz(0) is undefined, so guard that case in your own code.
How is CLZ related to the highest set bit?
For a nonzero word they add up to the width minus one. In 01001101 there is 1 leading zero and the highest set bit is at index 6, and 1 + 6 = 7 = 8 - 1. Use the Last set bit Calculator on this site to get the index directly.
Does the calculator accept hex or decimal numbers?
No, only binary digits with an optional 0b prefix. A leading zero count depends on a width that hex and decimal notation do not carry, so convert to binary and pad to the register size first.
What is the difference between leading and trailing zeros?
Leading zeros are at the left, most significant end and depend on the word width. Trailing zeros are at the right, least significant end and tell you how many times the value divides by two. 00101000 has 2 leading zeros and 3 trailing zeros.