Understanding the bitwise truth table
Each logical bitwise operation follows a fixed truth table applied independently to every pair of corresponding bit positions.
| Bit A | Bit B | AND | OR | XOR |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
- This calculator restricts inputs to the range of a signed 32-bit integer (0 to 2,147,483,647) because JavaScript's bitwise operators internally convert numbers to 32-bit representations before operating on them, following the ECMAScript specification.
- NOT (~A) always produces a result that, interpreted as a signed 32-bit integer, equals −(A + 1) — this calculator displays the unsigned 32-bit equivalent (e.g. ~12 displays as 4,294,967,283 rather than −13) so that the binary representation shown is unambiguous.
- Shift amounts (B) greater than 31 are capped at 31 by this calculator, since shifting a 32-bit value by 32 or more positions in JavaScript wraps around (using only the low 5 bits of the shift amount) rather than producing 0 as might be intuitively expected.
What are bitwise operations?
A bitwise operation processes a number at the level of its individual binary digits (bits), comparing or shifting the 0s and 1s of its base-2 representation rather than performing ordinary base-10 arithmetic. Every whole number can be written in binary as a sequence of bits, each representing a power of 2 — for example, 12 in binary is 1100, meaning 1×8 + 1×4 + 0×2 + 0×1.
The logical bitwise operations — AND, OR, XOR (exclusive or) and NOT — compare corresponding bit positions of two numbers (or invert the bits of one number) according to standard rules of Boolean logic, applied independently to each bit position. The shift operations — left shift and right shift — move all the bits of a number a specified number of positions to the left or right, which is mathematically equivalent to multiplying or dividing by a power of 2.
Bitwise operations are fundamental to computer science and low-level programming: they are used for setting, clearing and checking individual flags in a set of options (bit flags), for fast multiplication and division by powers of 2, for cryptographic algorithms, for graphics and color manipulation (combining red/green/blue channels), and for compact data encoding.
How to use this bitwise calculator
- Enter the first number (A) as a non-negative whole number (0 to 2,147,483,647, the range of a signed 32-bit integer).
- Enter the second number (B). For AND, OR and XOR, this is the second operand compared bit-by-bit with A. For shift operations, this is the number of positions to shift (0–31 is effective; larger values are capped at 31). For NOT, B is ignored since NOT operates on A alone.
- Select the operation: AND, OR, XOR, NOT, left shift, or right shift.
- Read the decimal result, along with the binary representation of A, B (or the shift amount), and the result.
How each bitwise operation works
AND compares each bit position and returns 1 only where both bits are 1, otherwise 0. Worked example: 12 (1100) AND 10 (1010) = 8 (1000), since only the third bit position (value 8) is 1 in both numbers.
OR compares each bit position and returns 1 where at least one of the two bits is 1. Worked example: 12 (1100) OR 10 (1010) = 14 (1110). XOR (exclusive or) returns 1 where exactly one of the two bits is 1 (but not both). Worked example: 12 (1100) XOR 10 (1010) = 6 (0110).
NOT inverts every bit of a single number (0 becomes 1, 1 becomes 0). Because this calculator operates on 32-bit values, NOT 12 flips all 32 bits, producing 4,294,967,283 when interpreted as an unsigned 32-bit integer (the two's complement representation of −13 if interpreted as a signed integer).
Left shift (A << B) moves every bit of A left by B positions, filling the vacated low-order bits with 0 — equivalent to multiplying A by 2^B. Worked example: 12 << 2 = 48 (equivalent to 12 × 2² = 12 × 4 = 48). Right shift (A >> B) moves every bit of A right by B positions, discarding the low-order bits — equivalent to integer division of A by 2^B, rounding down. Worked example: 12 >> 2 = 3 (equivalent to ⌊12 ÷ 4⌋ = 3).
Common mistakes
- Confusing bitwise AND/OR with logical (boolean) AND/OR — bitwise operators act on every bit of a number independently, while logical operators treat the entire value as a single true/false condition; the two produce different results except in the special case of the values 0 and 1.
- Expecting left shift by a large amount to always produce a proportionally larger number — shifting far enough can push significant bits beyond the 32-bit boundary, causing them to be discarded (overflow), which is why shift amounts here are capped at 31.
- Misreading the NOT result — because NOT flips every one of the 32 bits, the unsigned decimal result of ~A looks much larger than A itself, even though in signed two's-complement interpretation it actually represents a small negative number, −(A+1).
- Treating right shift as always equivalent to plain division — right shift on a non-negative integer matches integer floor division by a power of 2 exactly, but behavior differs for negative numbers depending on whether an arithmetic or logical shift is used.
Часто задаваемые вопросы
What is the difference between bitwise AND and logical AND?
Bitwise AND (&) compares two numbers bit by bit and returns a new number where each bit is 1 only if both corresponding input bits are 1 — for example, 12 & 10 = 8. Logical AND (&&) treats each entire value as a single true/false condition and returns one of the original operands or a boolean, evaluating truthiness rather than combining individual bits. The two operators serve different purposes and generally produce very different results.
How does XOR work?
XOR (exclusive or) compares two numbers bit by bit and returns 1 in each position where exactly one of the two corresponding bits is 1 (not both, and not neither). For 12 (1100) XOR 10 (1010): comparing each bit position gives 0110, which is 6 in decimal. XOR is commonly used to toggle bits, detect differences between two values, and in simple checksum and parity calculations.
What does a left shift do?
A left shift (A << B) moves every bit in A's binary representation B positions to the left, filling the newly vacated low-order positions with 0s. This is mathematically equivalent to multiplying A by 2 raised to the power B. For example, 12 << 2 = 48, the same result as 12 × 2² = 12 × 4 = 48.
What does bitwise NOT do?
Bitwise NOT (~A) inverts every bit of A — every 0 becomes 1 and every 1 becomes 0. Applied to a 32-bit representation of 12 (00000000000000000000000000001100), NOT produces a value with all those bits flipped, which as an unsigned 32-bit number displays as 4,294,967,283, and as a signed integer represents −13 in two's-complement form (following the identity ~A = −(A+1)).
How is right shift different from division?
For non-negative integers, a right shift (A >> B) produces exactly the same result as integer floor division by 2^B: A >> B = ⌊A ÷ 2^B⌋. For example, 12 >> 2 = 3, matching ⌊12 ÷ 4⌋ = 3. The two operations can diverge for negative numbers, where the specific type of shift (arithmetic vs. logical) affects how the sign bit is handled — a detail relevant to lower-level programming contexts.
Why are bitwise operations limited to 32-bit integers here?
JavaScript's built-in bitwise operators (&, |, ^, ~, <<, >>) internally convert their operands to 32-bit integers before performing the operation, per the ECMAScript language specification. This calculator mirrors that standard behavior, so inputs are limited to the range representable in 32 bits (0 to 2,147,483,647 for the non-negative values accepted here).
Источники
- Warren HS Jr. Hacker's Delight. 2nd ed. Addison-Wesley, 2012. (Standard reference for bitwise algorithms and two's-complement arithmetic.)
- ECMA International. ECMA-262: ECMAScript Language Specification, §6.1.6.1 (Bitwise operators, ToInt32/ToUint32). ecma-international.org.
- Patterson DA, Hennessy JL. Computer Organization and Design: The Hardware/Software Interface. 5th ed. Morgan Kaufmann, 2013. (Binary representation and bitwise logic.)