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
Parameter | Optional/Required | Description |
---|---|---|
x | required | A 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,
- We call
Math.clz32()
with 1 as the argument. - The method returns the number of leading zero bits in the 32-bit binary representation of 1.
- 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,
- We call
Math.clz32()
with 0 as the argument. - The method returns the number of leading zero bits in the 32-bit binary representation of 0.
- 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,
- We call
Math.clz32()
with 1000 as the argument. - The method returns the number of leading zero bits in the 32-bit binary representation of 1000.
- 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.