概述
Math.clz32() 函数返回一个数字在转换成 32 无符号整形数字的二进制形式后, 开头的 0 的个数, 比如 1000000 转换成 32 位无符号整形数字的二进制形式后是 00000000000011110100001001000000, 开头的 0 的个数是 12 个, 则 Math.clz32(1000000) 返回 12.
语法
Math.clz32 (x)
参数
- x
- 一个数字.
描述
"clz32" 是 CountLeadingZeroes32 的缩写.
如果 x 不是数字类型, 则它首先会被转换成数字类型, 然后再转成 32 位无符号整形数字.
如果转换后的 32 位无符号整形数字是 0, 则返回 32, 因为此时所有位上都是 0.
NaN, Infinity, -Infinity 这三个数字转成 32 位无符号整形数字后都是 0.
这个函数主要用于那些编译目标为 JS 语言的系统中, 比如 Emscripten.
示例
Math.clz32(1) // 31
Math.clz32(1000) // 22
Math.clz32() // 32
[NaN, Infinity, -Infinity, 0, -0, null, undefined, "foo", {}, []].filter(function (n) {
return Math.clz32(n) !== 32
}) // []
Math.clz32(true) // 31
Math.clz32(3.5) // 30
Polyfill
Math.clz32 = Math.clz32 || function(value) {
var value = Number(value) >>> 0;
return value ? 32 - value.toString(2).length : 32;
}
规范
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) Math.clz32 |
Standard | Initial definition. |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains.
Find out how you can help!
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 35 | 31 (31) | 未实现 | 未实现 | 未实现 |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | 未实现 | 未实现 | 31.0 (31) | 未实现 | 未实现 | 未实现 |