Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Math.clz32() Function

Improve Article
Save Article
  • Last Updated : 21 Dec, 2022
Improve Article
Save Article

JavaScript Math.clz32() is an inbuilt function in JavaScript that stands for “Count Leading Zeroes 32”. This function 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 function 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 below 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() function.  

Example 1: In this example, we will see the basic use of the Math.clz32() function.  

Javascript




<script>
    // Here different number is being taken 
    // as a parameter for Math.clz32() function.
    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));
</script>


Output: 

31
28
26
25
22

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




<script>
  // Complex number can not be converted into
  // 32-bit binary representation.
  console.log(Math.clz32(1+2i));
</script>


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




<script>
    // 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"));
</script>


Output: 

32
32

Application: Here Math.clz32() function have 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 function in JavaScript.

Example: In this example, we will see the basic use of Math.clz32() function in Javascript.

Javascript




<script>
  // Here different numbers are being taken as
  // parameter from 0 to 9 for Math.clz32() function.
  for (i = 0; i < 10; i++)
  
      console.log(Math.clz32(i)); 
  }
</script>


Output: 

32
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() function are listed below: 

  • Google Chrome 38 and above
  • Firefox 12 and above
  • Opera 25 and above
  • Safari 7 and above

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!