JavaScript Math clz32() Method
Last Updated :
12 Jul, 2024
JavaScript Math.clz32() is an inbuilt method in JavaScript that stands for “Count Leading Zeroes 32”. This method is used for getting the number of leading zero bits present in the 32-bit representation of a number.
Syntax:
Math.clz32(p)
Parameters: this method accepts a single parameter p, which is a number for which the number of leading zero bits present in its 32-bit representation is going to find out.
Returns: It returns the number of leading zero bits present in a 32-bit representation of the number.
Examples:
Input : Math.clz32(10)
Output : 28
Explanation: Here the number 10 can be represented in 32-bit as shown below-
00000000000000000000000000001010
From the above representation, we see that there is a total of 28 zero bits which are leading 1010 i.e., 4 bits of decimal number 10. That is why here output becomes 28 as leading zero bits are 28.
Input : Math.clz32(64)
Output :25
Let’s see the JavaScript code on Math.clz32() method.
Example 1: In this example, we will see the basic use of the Math.clz32() method.
JavaScript
// Here different number is being taken
// as a parameter for Math.clz32() method.
console.log(Math.clz32(1));
console.log(Math.clz32(8));
console.log(Math.clz32(32));
console.log(Math.clz32(64));
console.log(Math.clz32(772));
Example 2: Errors and Exceptions, it is an error case because a complex number cannot be converted into a 32-bit binary representation only integers value can be converted.
JavaScript
// Complex number can not be converted into
// 32-bit binary representation.
console.log(Math.clz32(1 + 2i));
Output:
Error: Invalid or unexpected token
Example 3: It is an exceptional case that it can be considered as a string parameter gives an internal zero then it becomes possible otherwise it should return an error.
JavaScript
// Any string behave exceptionally and give leading
// 32 zero bit in its 32-bit binary representation
// still any string can not be converted into
// 32-bit binary representation.
console.log(Math.clz32("geeksforgeeks"));
console.log(Math.clz32("gfg"));
Application:
Here Math.clz32() method has many applications whenever we need to get the number of leading zero bits present in a 32-bit representation of a number that time we take the help of this method in JavaScript.
Example: In this example, we will see the basic use of the Math.clz32() method in Javascript.
JavaScript
// Here different numbers are being taken as
// parameter from 0 to 9 for Math.clz32() method.
for (i = 0; i < 10; i++) {
console.log(Math.clz32(i));
}
Output32
31
30
30
29
29
29
29
28
28
We have a complete list of Javascript Math methods, to check those please go through this JavaScript Math Object Complete Reference article.
Supported Browsers: The browsers supported by JavaScript Math.clz32() method are listed below:
- Google Chrome 38 and above
- Firefox 12 and above
- Opera 25 and above
- Safari 7 and above
Similar Reads
JavaScript Math E Property
The Math.E is a property in JavaScript that simply returns the value of Euler's Number, which is nothing but just has a base of the natural logarithm. Its value is approximately around of 2.71828. Syntax:Math.E;Math.E â 2.718Return Values:It simply returns the value of the base of the natural logari
2 min read
JavaScript Math LN2 Property
The Math.LN2 is a property in JavaScript that is simply used to find the value of a natural log of 2. The natural log is of base e which is represented as ln. So, the natural log of 2 is represented as ln(2) whose value is approximately 0.693. Syntax: Math.LN2; Parameters: This method does not accep
2 min read
JavaScript Math LN10 Property
The Math.LN10 is a property in JavaScript that is simply used to find the value of a natural log of 10. The natural log is of base e which is represented as ln. So, the natural log of 10 is represented as ln(10) whose value is approximately 2.302 Syntax: Math.LN10; Return Values: It simply returns t
3 min read
JavaScript Math LOG2E Property
The Javascript Math.LOG2E is a property in JavaScript that is simply used to find the value of base 2 logarithms of e, where e is an irrational and transcendental number approximately equal to 1.442. Syntax: Math.LOG2E;Return Values: It simply returns the value of the base 2 logarithms of e. Example
3 min read
JavaScript Math LOG10E Property
The Javascript Math.LOG10E is a property in JavaScript that is simply used to find the value of base 10 logarithms of e, where e is an irrational and transcendental number approximately equal to 0.434 Syntax: Math.LOG10E; Return Values: It simply returns the value of the base 10 logarithms of e. Exa
3 min read
JavaScript Math PI Property
The Math.PI is a property in JavaScript that is simply used to find the value of Pi i.e, in symbolic form Î which is nothing but it is the ratio of the circumference of a circle to its diameter, whose value is approximately 3.141. It is mainly used in mathematics problems. Syntax:Math.PIReturn value
3 min read
JavaScript Math SQRT1_2 Property
The Javascript Math.SQRT1_2 is a property in JavaScript that is simply used to find the value of the square root of 1/2, whose value is approximately 0.707106. That is, â (1/2) = 0.707106Syntax: Math.SQRT1_2;Return Values: It simply returns the value of the square root of 1/2, whose value is approxi
3 min read
JavaScript Math SQRT2 Property
The Math.SQRT2 is a property in JavaScript which is simply used to find the value of the square root of 2, whose value is approximately 1.4142. That is, â 2 = 1.4142 Syntax: Math.SQRT2; Return Value: It simply returns the value of the square root of 2, whose value is approximately 1.4142. Below is a
2 min read
JavaScript Math abs() Method
Javascript Math.abs() method is used to return the absolute value of a number. It takes a number as its parameter and returns its absolute value. Syntax:Math.abs(value)Parameters:This method accepts a single parameter as mentioned above and described below: value: The number whose absolute value is
2 min read
JavaScript Math acos() Method
The Javascript Math.acos( ) method is used to return the arccosine of a number in radians. The Math.acos() method returns a numeric value between 0 and pi radians. The Math acos() is a static method of Math, therefore, it is always used as Math.acos(), rather than as a method of a Math object create
3 min read