Categories
JavaScript Web development

JavaScript Math.clz32()

JavaScript Math.clz32()

The JavaScript Math.clz32() is short for CountLeadingZeroes32, and it returns the number of leading zeros in a 32-bit binary representation of a number.

Syntax:

Math.clz32(x)

Example:

The number of leading zeros bits in the 32-bit binary representation of a number. If the number is 0, then this method returns 32 (as all bits are 0).

<!DOCTYPE html>
<html>
<body>

<button onclick="clz32Example()">Try it</button>

<p id="example"></p>

<script>
function clz32Example() {
  var a = Math.clz32(0);
  var b = Math.clz32(1);
  var c = Math.clz32(2);
  var d= Math.clz32(5);
  
  var x = a + "<br>" + b + "<br>" + c + "<br>" + d;
  document.getElementById("example").innerHTML = x;
}
</script>

</body>
</html>

Output:

Click the button to return the number of leading zeros in a 32-bit binary representation of different numbers:


Enjoy coding!