JavaScript Math clz32()

The JavaScript Math.clz32() function returns the number of leading zero bits in the 32-bit binary representation of a number.

Here, clz32 is short for CountLeadingZeroes32.

The syntax of the Math.clz32() function is:

Math.clz32(x)

clz32(), being a static method, is called using the Math class name.


Math.clz32() Parameters

The Math.clz32() function takes in:

  • x - A number

Return value from Math.clz32()

  • Returns the number of leading zero bits in the 32-bit binary representation of the number.

Example: Using Math.clz32()

// 00000000000000000000000000000001
var value = Math.clz32(1);
console.log(value); // 31

// 00000000000000000000001111101000
var value = Math.clz32(1000);
console.log(value); // 22

// 00000000000000000000000000000100
var value = Math.clz32(4);
console.log(value); // 29

var value = Math.clz32(true);
console.log(value); // 31

var value = Math.clz32(0);
console.log(value); // 32

Output

31
22
29
31
32