--- id: javascript-bitwise title: "JavaScript Bitwise" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["bitwise operators", "bit manipulation", "AND OR XOR", "bit shift", "two's complement", "32-bit operations"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.87 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "bitwise", "operators"] raw_sources: ["https://www.w3schools.com/js/js_bitwise.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Bitwise]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) JavaScript bitwise operators work bit-by-bit on the binary representation of numbers β€” even though numbers are stored as 64-bit floats, every bitwise operation is performed on 32-bit binary numbers and returns a standard number. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Seven bitwise operators** β€” AND (`&`), OR (`|`), XOR (`^`), NOT (`~`), zero fill left shift (`<<`), signed right shift (`>>`), and zero fill right shift (`>>>`). [S1] - **64-bit storage, 32-bit operations** β€” JavaScript stores numbers as 64-bit floating point numbers, but all bitwise operations are performed on 32-bit binary numbers. [S1] - **NOT and signed shift can produce negatives** β€” `~5` returns `-6`, and `-5 >> 1` returns `-3`, because of two's complement representation. [S1] - **Bitwise assignment operators** β€” shift assignments (`<<=`, `>>=`, `>>>=`) and bitwise assignments (`&=`, `^=`, `|=`) combine the operation with assignment. [S1] - **Base conversion via bitwise** β€” decimal-to-binary and binary-to-decimal can be done with `(dec >>> 0).toString(2)` and `parseInt(bin, 2).toString(10)`. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Per-bit combination** β€” `&`, `|`, `^` compare each pair of bits; `~` inverts every bit. [S1] - **Shift to multiply/divide by powers of two** β€” `5 << 1` doubles to `10`; `5 >>> 1` halves to `2`. [S1] - **Unsigned coercion for printing** β€” `(dec >>> 0)` forces an unsigned 32-bit view before `.toString(2)` to render binary. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Bitwise Operators** β€” JavaScript provides seven bitwise operators: [S1] | Operator | Name | Description | |----------|------|-------------| | `&` | AND | Sets each bit to 1 if both bits are 1 | | `\|` | OR | Sets each bit to 1 if one of two bits is 1 | | `^` | XOR | Sets each bit to 1 if only one of two bits is 1 | | `~` | NOT | Inverts all the bits | | `<<` | Zero fill left shift | Shifts left by pushing zeros in from the right and let the leftmost bits fall off | | `>>` | Signed right shift | Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off | | `>>>` | Zero fill right shift | Shifts right by pushing zeros in from the left, and let the rightmost bits fall off | **Note:** JavaScript stores numbers as 64 bits floating point numbers, but all bitwise operations are performed on 32 bits binary numbers. [S1] **Bitwise AND** β€” returns `1`: [S1] ```javascript let x = 5 & 1; ``` **Bitwise OR** β€” returns `5`: [S1] ```javascript let x = 5 | 1; ``` **Bitwise XOR** β€” returns `4`: [S1] ```javascript let x = 5 ^ 1; ``` **Bitwise NOT** β€” returns `-6`: [S1] ```javascript let x = ~5; ``` **Left Shift (`<<`)** β€” returns `10`: [S1] ```javascript let x = 5 << 1; ``` **Signed Right Shift (`>>`)** β€” returns `-3`: [S1] ```javascript let x = -5 >> 1; ``` **Zero Fill Right Shift (`>>>`)** β€” returns `2`: [S1] ```javascript let x = 5 >>> 1; ``` **Operator results summary** (verbatim values from the page) [S1] | Expression | Result | |------------|--------| | `5 & 1` | `1` | | `5 \| 1` | `5` | | `5 ^ 1` | `4` | | `~5` | `-6` | | `5 << 1` | `10` | | `-5 >> 1` | `-3` | | `5 >>> 1` | `2` | **Bitwise assignment operators** β€” the page documents shift assignment operators (`<<=`, `>>=`, `>>>=`) and bitwise assignment operators (`&=`, `^=`, `|=`). [S1] **Converting Decimal to Binary** [S1] ```javascript function dec2bin(dec){ return (dec >>> 0).toString(2); } ``` **Converting Binary to Decimal** [S1] ```javascript function bin2dec(bin){ return parseInt(bin, 2).toString(10); } ``` The page also includes binary-representation tables showing decimal values and their binary form, and two's-complement format for negative numbers. The exact full numeric contents of those tables were not captured verbatim in the fetched source rendering: Not found in source. ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” the seven single-operator demonstrations and the `dec2bin` / `bin2dec` conversion helpers. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Single bitwise operations (language: JavaScript): ```javascript let a = 5 & 1; // 1 let b = 5 | 1; // 5 let c = 5 ^ 1; // 4 let d = ~5; // -6 let e = 5 << 1; // 10 let f = -5 >> 1; // -3 let g = 5 >>> 1; // 2 ``` Base conversion helpers: ```javascript function dec2bin(dec){ return (dec >>> 0).toString(2); } function bin2dec(bin){ return parseInt(bin, 2).toString(10); } ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.87 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Operators]], [[JavaScript Numbers]], [[JavaScript BigInt]], [[JavaScript Number Properties]] - **μ°Έμ‘° λ§₯락:** Referenced when working with flags, masks, low-level math, or base conversions. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Bitwise β€” https://www.w3schools.com/js/js_bitwise.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Bitwise" page (Astra wiki-curation, P-Reinforce v3.1 format).