Understanding the bitwise calculator — concept and principle
Bitwise operators work on the individual bits of integers rather than on their numeric value. They are used for flags and permission masks, packing several small values into one word, hashing, graphics, network masks and fast multiplication or division by powers of two. This calculator applies AND, OR, XOR, NOT and the three shifts to two decimal inputs using 32-bit two's-complement integers, the same rules as JavaScript, Java and C on common platforms.
How it is calculated
- A AND B: a bit is 1 only where both inputs have 1.
- A OR B: a bit is 1 where at least one input has 1.
- A XOR B: a bit is 1 where the inputs differ.
- NOT A: every bit of the 32-bit value is flipped; in two's complement this equals −A − 1.
- A << n: bits move n places left and zeros fill from the right, which multiplies by 2ⁿ as long as nothing overflows.
- A >> n (arithmetic): bits move right and the sign bit is copied in, so it equals floor(A ÷ 2ⁿ), also for negative numbers.
- A >>> n (logical): bits move right and zeros fill from the left, treating the value as unsigned.
The principle
A binary number is a sum of powers of two, and each bit is independent of the others in AND, OR and XOR, so these operations act like 32 parallel logic gates. Shifting moves every bit to the next power of two, which is why it scales by 2. Two's complement represents −x as NOT x + 1, so the top bit acts as the sign and ordinary binary addition works for negative numbers too. That same rule gives NOT A = −A − 1.
Worked example
- Inputs: A = 12 (1100), B = 10 (1010), shift = 2.
- AND: 1100 & 1010 = 1000 = 8. OR: 1110 = 14. XOR: 0110 = 6.
- NOT 12: all 32 bits flipped gives 11111111111111111111111111110011, which is −13 (= −12 − 1).
- 12 << 2 = 110000 = 48 (12 × 4).
- 12 >> 2 = 11 = 3, and because 12 is positive, 12 >>> 2 is also 3.
How to read the result
To test a flag, check whether value AND mask is non-zero; to set it use OR; to clear it use AND with NOT mask; to toggle it use XOR. The arithmetic and logical right shifts only differ for negative inputs: −8 >> 1 is −4, while −8 >>> 1 is 2147483644.
Common mistakes
- Using the logical operators && and || when the bitwise & and | were intended.
- Expecting values above 2³¹ − 1 to survive: inputs are converted to signed 32-bit integers, so larger numbers wrap.
- Shifting by 32 or more: JavaScript and Java use only the low 5 bits of the shift count, so x << 32 equals x.
Frequently asked questions
Why is NOT 12 negative?
Flipping all 32 bits sets the sign bit, and in two's complement the result is −12 − 1 = −13.
Is A << 1 always the same as A × 2?
Only while the result stays inside the 32-bit range; otherwise high bits are lost.
What is XOR used for?
Toggling bits, parity and checksums, and finding where two bit patterns differ.
Sources: Ecma International, ECMA-262 ECMAScript Language Specification (bitwise and shift operators) · Henry S. Warren Jr., Hacker's Delight, 2nd ed., Addison-Wesley, 2012