JavaScript Math.clz32() Method

Definition
Math.clz32(x) counts the leading zero bits when a number is written as a 32-bit unsigned integer. The name means count leading zeros, 32-bit.
Math is built into JavaScript. Call the method directly. It is not a constructor, so never write new Math().
Syntax
Call Math.clz32() like this. Start with this line.
| Call | What it does |
|---|
Math.clz32(x) | Returns the number of leading zero bits in the 32-bit form of x. |
Built into JavaScript. Call Math.clz32() on the Math object — no import required.
Library declarations
interface Math {
clz32(x: number): number;
}
Parameters
| Parameter | Description |
|---|
| x | Required. A number. It is converted to an unsigned 32-bit integer first. |
Return Value
| Return Value | Description |
|---|
| number | A whole number from 0 to 32: the count of leading zero bits. |
JavaScript Examples
Example 1 — Basic Use
A bigger number has fewer leading zeros. Binary literals start with 0b.
console.log(Math.clz32(1)); // 31
console.log(Math.clz32(0b1000)); // 28
console.log(Math.clz32(255)); // 24
console.log(Math.clz32(0)); // 32
How It Works
- One.
1 is a single bit at the end, so 31 zeros come first. - Binary literal.
0b1000 is 8. It uses 4 bits, so 28 leading zeros. - 255. It needs 8 bits (
11111111), leaving 24 zeros. - Zero. No 1 bit at all, so the answer is
32.
Example 2 — Bit Length of a Number
Subtract the leading zeros from 32 to see how many bits a number needs.
function bitLength(n) {
return 32 - Math.clz32(n);
}
console.log(bitLength(1)); // 1
console.log(bitLength(255)); // 8
console.log(bitLength(256)); // 9
How It Works
- 32 bits total. A 32-bit number has 32 bit slots.
- Remove zeros. Subtract the leading zeros to count the used bits.
- Edge.
255 needs 8 bits. 256 needs one more bit, so 9.
Example 3 — Special Values and Floor of log2
Decimals are cut, NaN and Infinity act like 0. You can also get the floor of log2 quickly.
console.log(Math.clz32(3.9)); // 30
console.log(Math.clz32(NaN)); // 32
console.log(Math.clz32(Infinity)); // 32
console.log(Math.clz32(-1)); // 0
console.log(31 - Math.clz32(1000)); // 9
console.log(Math.floor(Math.log2(1000))); // 9
How It Works
- Decimals.
3.9 becomes 3 (binary 11), so 30 zeros. - NaN and Infinity. Both become
0, so the answer is 32. - Negative.
-1 is all ones in 32 bits, so there are 0 zeros. - Fast log2.
31 - Math.clz32(n) equals Math.floor(Math.log2(n)) for positive integers.
Compatibility
Browser & Runtime Support
Math.clz32() is part of ES2015 and is Baseline Widely available in modern browsers and Node.js.
✓ Baseline · ES2015
Math.clz32()
Supported in Chrome, Firefox, Safari, Edge, and Node.js for years. Safe for modern apps without a polyfill.
98% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full supportMath.clz32 Excellent
Bottom line: Use Math.clz32() in new code. Only very old browsers, like Internet Explorer, lack it.
Frequently Asked Questions
Math.clz32(x) counts how many zero bits come before the first 1 bit in the 32-bit form of x. Math.clz32(1) is 31.
It stands for "count leading zeros, 32-bit".
It returns 32, because all 32 bits are zeros.
Use 32 - Math.clz32(n). For example, 255 needs 32 - 24 = 8 bits.
It was added in ES2015 and works in all modern browsers and Node.js.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful