JavaScript | Math.clz32() function

Math.clz32() is an inbuilt function in JavaScript which stands for “Count Leading Zeroes 32”.
This function is used for getting the number of leading zero bits present in 32-bit representation of a number.
Syntax:

Math.clz32(p)

Parameter:

  • Here the parameter p is a number for which the number of leading zero bits present in its 32-bit representation is going to find out.
  • Return value:

  • It returns the number of leading zero bits present in 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 above representation, we see that there are total 28 zero bits which are leading 1010 i.e, 4 bit 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 JavaScript code on Math.clz32() function.
Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Here different number is being taken as 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));

chevron_right


Output:

> 31
> 28
> 26
> 25
> 22

Errors and Exceptions:

  1. It is an error case because a complex number cannot be converted into 32-bit binary representation only integers value can be converted.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Error: Invalid or unexpected token
  2. It is an exceptional case that it can be considered as string parameter gives internally zero then it become possible otherwise it should return an error.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    > 32
    > 32

Application:

  • Here Math.clz32() function have many applications as whenever we need to get the number of leading zero bits present in 32-bit representation of a number that time we take the help of this function in JavaScript.
    Code #1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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)); + "<br>";
    }

    chevron_right

    
    

    Output:

    > 32
    > 31
    > 30
    > 30
    > 29
    > 29
    > 29
    > 29
    > 28
    > 28
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.