JavaScript Tutorial JavaScript References

JavaScript - Math.clz32() Method



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

Syntax

Math.clz32(x)

Parameters

x Specify a number.

Return Value

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

Example:

The example below shows the usage of Math.clz32() method.

var txt;

//00000000000000000000000000000001
//expected output = 31
txt = "Math.clz32(1) = " + Math.clz32(1) + "<br>";

//00000000000000000000000000000100
//expected output = 29
txt = txt + "Math.clz32(4) = " + Math.clz32(4) + "<br>";

//00000000000000000000001111101000
//expected output = 22
txt = txt + "Math.clz32(1000) = " + Math.clz32(1000) + "<br>";

The output (value of txt) after running above script will be:

Math.clz32(1) = 31
Math.clz32(4) = 29
Math.clz32(1000) = 22

❮ JavaScript - Math Object