JavaScript Math.clz32()
Syntax & Examples


Math.clz32() static-method

The Math.clz32() method in JavaScript returns the number of leading zero bits in the 32-bit binary representation of a number. It is a static method of Math, meaning it is always used as Math.clz32() and not as a method of a Math object created (Math is not a constructor).


Syntax of Math.clz32()

The syntax of Math.clz32() static-method is:

Math.clz32(x)

This clz32() static-method of Math returns the number of leading zero bits of the 32-bit integer x.

Parameters

ParameterOptional/RequiredDescription
xrequiredA number for which to count the leading zero bits in its 32-bit binary representation.

Return Type

Math.clz32() returns value of type Number.



✐ Examples

1 Using Math.clz32() with a positive number

In this example, we use the Math.clz32() method to find the number of leading zero bits in the 32-bit binary representation of 1.

For example,

  1. We call Math.clz32() with 1 as the argument.
  2. The method returns the number of leading zero bits in the 32-bit binary representation of 1.
  3. We log the result to the console using console.log().

JavaScript Program

console.log(Math.clz32(1));

Output

31

2 Using Math.clz32() with zero

In this example, we use the Math.clz32() method to find the number of leading zero bits in the 32-bit binary representation of 0.

For example,

  1. We call Math.clz32() with 0 as the argument.
  2. The method returns the number of leading zero bits in the 32-bit binary representation of 0.
  3. We log the result to the console using console.log().

JavaScript Program

console.log(Math.clz32(0));

Output

32

3 Using Math.clz32() with a large number

In this example, we use the Math.clz32() method to find the number of leading zero bits in the 32-bit binary representation of 1000.

For example,

  1. We call Math.clz32() with 1000 as the argument.
  2. The method returns the number of leading zero bits in the 32-bit binary representation of 1000.
  3. We log the result to the console using console.log().

JavaScript Program

console.log(Math.clz32(1000));

Output

22

Summary

In this JavaScript tutorial, we learned about clz32() static-method of Math: the syntax and few working examples with output and detailed explanation for each example.