Math.clz32()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itโs been available across browsers since โจ2015๋ 7์โฉ.
Math.clz32()
์ ์ ๋ฉ์๋๋ ์ซ์์ 32๋นํธ ์ด์ง ํํ์์ ์ ํ 0 ๋นํธ์ ๊ฐฏ์๋ฅผ ๋ฐํํฉ๋๋ค.
์๋ํด ๋ณด๊ธฐ
// 00000000000000000000000000000001
console.log(Math.clz32(1));
// Expected output: 31
// 00000000000000000000000000000100
console.log(Math.clz32(4));
// Expected output: 29
// 00000000000000000000001111101000
console.log(Math.clz32(1000));
// Expected output: 22
๊ตฌ๋ฌธ
Math.clz32(x)
๋งค๊ฐ๋ณ์
x
-
์ซ์์ ๋๋ค.
๋ฐํ ๊ฐ
32๋นํธ ์ด์ง ํํ์์ x
์ ์ ํ 0 ๋นํธ ์๋ฅผ ๋ฐํํฉ๋๋ค.
์ค๋ช
clz32
๋ CountLeadingZeros32 ์ ์ฝ์์
๋๋ค.
๋ง์ผ x
๊ฐ ์ซ์๊ฐ ์๋๋ผ๋ฉด, ์ด๋ ๊ฐ์ฅ ๋จผ์ ์ซ์๋ก ๋ณํ์ด ๋๊ณ ๊ทธ ๋ค์ 32๋นํธ์ ๊ธฐํธ๊ฐ ์๋ ์ ์๋ก ๋ณํ๋ฉ๋๋ค.
๋ณํ๋ 32๋นํธ์ ๊ธฐํธ ์๋ ์ ์๊ฐ 0
์ด๋ผ๋ฉด 32
๊ฐ ๋ฐํ๋ฉ๋๋ค. ์๋ํ๋ฉด ๋ชจ๋ ๋นํธ๋ 0
์ด๊ธฐ ๋๋ฌธ์
๋๋ค. ๋ง์ผ (์ซ์๊ฐ 231 ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ๊ฒฝ์ฐ์ฒ๋ผ) ๊ฐ์ฅ ํฐ ๋นํธ๊ฐ 1
์ด๋ผ๋ฉด 0
์ด ๋ฐํ๋ฉ๋๋ค.
์ด ํจ์๋ Emscripten ์ฒ๋ผ JS๋ก ์ปดํ์ผ๋๋ ์ฒด๊ณ์ ํนํ ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋ฉ๋๋ค.
์์
Math.clz32() ์ฌ์ฉํ๊ธฐ
Math.clz32(1); // 31
Math.clz32(1000); // 22
Math.clz32(); // 32
const stuff = [
NaN,
Infinity,
-Infinity,
0,
-0,
false,
null,
undefined,
"foo",
{},
[],
];
stuff.every((n) => Math.clz32(n) === 32); // true
Math.clz32(true); // 31
Math.clz32(3.5); // 30
์ ํ ๋นํธ ์ฐ์ฐ๊ณผ ๊ทธ ์ด์
ํ์ฌ๋ ์ ํ ๋นํธ ์ฐ์ฐ("clo" ๊ฐ ์๋๋ผ "clon" ์ด๋ผ๊ณ ๋ถ๋ฅด๋๋ฐ, ์ด๋ "clo" ์ "clz" ๊ฐ ์์ด๋ฅผ ์ฌ์ฉํ์ง ์๋ ์ฌ๋๋ค์๊ฒ ๋งค์ฐ ์ ์ฌํ๊ฒ ๋๊ปด์ง ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค.)์ ์ํ Math.clon
ํจ์๋ ์์ต๋๋ค. ํ์ง๋ง clon
ํจ์๋ ์ฝ๊ฒ ๋นํธ๋ฅผ ์ซ์๋ก ์ฐ์ฐํ์ด Math.clz32
์ ๊ฒฐ๊ด๊ฐ์ผ๋ก ์ ๋ฌ๋ฉ๋๋ค. ์ด๋ 1์ ๋ฐ๋๊ฐ 0์ด๊ณ ๊ทธ ๋ฐ๋๋ ๋ง์ฐฌ๊ฐ์ง์ด๊ธฐ ๋๋ฌธ์ ์๋ํฉ๋๋ค. ๋ฐ๋ผ์ ๋นํธ๋ฅผ ๋ฐ์ ์ํค๋ฉด 0์ ์ธก์ ๋ ์๋( Math.clz32
์์)์ด ๋ฐ์ ๋์ด Math.clz32
๊ฐ 0 ๋์ 1์ ๊ฐ์๋ฅผ ์ธ๊ฒ ๋ฉ๋๋ค.
๋ค์ 32๋นํธ ์์ ๋ฅผ ์ดํด๋ณด์ธ์.
const a = 32776; // 00000000000000001000000000001000 (16๊ฐ์ ์ ํ 0์ด ์์)
Math.clz32(a); // 16
const b = ~32776; // 11111111111111110111111111110111 (32776 ๋ฐ์ , 0๊ฐ์ ์ ํ 0์ด ์์)
Math.clz32(b); // 0 (์ด๋ a์์ ์ผ๋ง๋ ๋ง์ ์ ํ ๊ฐ์ด ์๋์ง์ ๋์ผํ ๊ฐ์
๋๋ค.)
์ด ๋ก์ง์ ์ฌ์ฉํ๋ฉด clon
ํจ์๋ ๋ค์๊ณผ ๊ฐ์ด ์์ฑ๋ฉ๋๋ค.
const clz = Math.clz32;
function clon(integer) {
return clz(~integer);
}
๋ ๋์๊ฐ, ์ด ๊ธฐ์ ์ ์๋์ ๊ฐ์ด ์ ํ๊ฐ ์๋ "ํํ 0 ์ธ๊ธฐ" ํจ์๋ฅผ ๋ง๋๋ ๋ฐ์ ํ์ฅํ์ฌ ์ฌ์ฉํ ์ ์์ต๋๋ค. ctrz
ํจ์๋ ์ ์์ ๊ทธ 2์ ๋ณด์์ ๋นํธ AND ์ฐ์ฐ์ ์ํํฉ๋๋ค. 2์ ๋ณด์๊ฐ ์๋ํ๋ ๋ฐฉ์์ ๋ฐ๋ผ, ๋ชจ๋ ํํ ์ ๋ก๋ 1๋ก ๋ณํ๋๊ณ , 1์ ๋ํ ๋ (์๋๋ 1
์ด์๋) ์ฒซ๋ฒ์งธ 0
์ ๋๋ฌํ ๋๊น์ง ์ฌ๋ฆผ์ด ๋ฐ์ํฉ๋๋ค. ์ด ๋นํธ๋ณด๋ค ๋์ ๋ชจ๋ ๋นํธ๋ ๋์ผํ๊ฒ ์ ์ง๋๋ฉฐ ์๋ ์ ์ ๋นํธ์ ์ญ์์
๋๋ค. ๋ฐ๋ผ์ ์๋ ์ ์์ ๋นํธ AND ์ฐ์ฐ์ ํ ๋, ๋ ๋์ ๋ชจ๋ ๋นํธ๋ 0
์ด ๋๋ฉฐ, ์ด๋ clz
๋ก ์นด์ดํธ๋ ์ ์์ต๋๋ค. ํํ ์ ๋ก์ ์์ ์ฒ์ 1
๋นํธ, ๊ทธ๋ฆฌ๊ณ clz
๋ก ์ธ์ด์ง ์ ํ ๋นํธ์ ํฉ์ 32๊ฐ ๋ฉ๋๋ค.
function ctrz(integer) {
integer >>>= 0; // Uint32๋ก ๋ณํ
if (integer === 0) {
// ์ด ๊ตฌ๋ฌธ์ ์คํตํ๋ฉด -1์ด ๋ฐํ๋ ์ ์์ต๋๋ค.
return 32;
}
integer &= -integer; // ๋ค์ ์ฐ์ฐ๊ณผ ๋์ผํฉ๋๋ค. `int = int & (~int + 1)`
return 31 - clz(integer);
}
"ํํ 0 ์ธ๊ธฐ" ํจ์๋ ๋ค์์ฒ๋ผ ๋์ํฉ๋๋ค.
function ctron(integer) {
return ctrz(~integer);
}
์ด ํฌํผ ํจ์๋ค์ ์ ์ฌ์ ์ธ ์ฑ๋ฅ ๊ฐ์ ์ ์ํด asm.js ๋ชจ๋์ ๋ง๋ค ์ ์์ต๋๋ค.
const countTrailsMethods = (function (stdlib, foreign, heap) {
"use asm";
const clz = stdlib.Math.clz32;
// ํํ 0 ์ธ๊ธฐ
function ctrz(integer) {
integer = integer | 0; // ์ ์๋ก ๋ณํ
if ((integer | 0) == 0) {
// ์ด ๊ตฌ๋ฌธ์ ์คํตํ๋ฉด -1์ด ๋ฐํ๋ ์ ์์ต๋๋ค.
return 32;
}
// Note: asm.js๋ &=์ ๊ฐ์ ๋ณตํฉ ํ ๋น ์ฐ์ฐ์๋ฅผ ๊ฐ์ง๊ณ ์์ง ์์ต๋๋ค.
integer = integer & -integer; // ๋ค์ ์ฐ์ฐ๊ณผ ๋์ผํฉ๋๋ค. `int = int & (~int + 1)`
return (31 - clz(integer)) | 0;
}
// ํํ ๊ฐฏ์ ์ธ๊ธฐ
function ctron(integer) {
integer = integer | 0; // ์ ์๋ก ๋ณํ
return ctrz(~integer) | 0;
}
// asm.js๋ ์์ ๊ฐ์ฒด๋ฅผ ํ์๋ก ํฉ๋๋ค.
return { ctrz: ctrz, ctron: ctron };
})(window, null, null);
const { ctrz, ctron } = countTrailsMethods;
๋ช ์ธ์
Specification |
---|
ECMAScriptยฎ 2026 Language Specification # sec-math.clz32 |
๋ธ๋ผ์ฐ์ ํธํ์ฑ
Loadingโฆ