totn JavaScript

JavaScript: Math clz32() function

This JavaScript tutorial explains how to use the math function called clz32() with syntax and examples.

Description

In JavaScript, clz32() is a function (acronym for "Count Leading Zeros 32") that is used to return the number of leading zeros in a 32-bit binary representation of a number. Because the clz32() function is a static function of the Math object, it must be invoked through the placeholder object called Math.

Syntax

In JavaScript, the syntax for the clz32() function is:

Math.clz32(number);

Parameters or Arguments

number
The number which will be converted to a 32-bit binary number and then its leading zero bits will be counted.

Returns

The clz32() function returns the number of leading zeros in a number represented as 32-bit binary.

Note

  • Math is a placeholder object that contains mathematical functions and constants of which clz32() is one of these functions.

Example

Let's take a look at an example of how to use the clz32() function in JavaScript.

For example:

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

In this example, we have invoked the clz32() function using the Math class.

We have written the output of the clz32() function to the web browser console log, for demonstration purposes, to show what the clz32() function returns.

The following will be output to the web browser console log:

31
30
29

In this example, the first output to the console log returned 31 since there are 31 leadings zeros in the 32-bit binary representation of the number 1 which is 00000000000000000000000000000001.

The second output to the console log returned 30 since there are 30 leadings zeros in the 32-bit binary representation of the number 2 which is 00000000000000000000000000000010.

The third output to the console log returned 29 since there are 29 leadings zeros in the 32-bit binary representation of the number 4 which is 00000000000000000000000000000100.