Bitwise Calculator
AND, OR, XOR, NOT, and bit shifts on two integers.
Operands and result shown in binary below.
| Value | Decimal | Binary (32-bit signed) |
|---|---|---|
| A | 12 | 1100 |
| B | 10 | 1010 |
| Result | 8 | 1000 |
A bitwise operation lines up two integers in binary and combines them one bit at a time. For example, 12 & 10 = 8, because 12 is 1100 and 10 is 1010, and AND keeps a 1 only where both have a 1 — giving 1000, which is 8.
What bitwise operators do
Every integer is stored as a row of bits. A bitwise operator compares the two numbers bit by bit, in the same column, and produces a new bit for each column. AND, OR, and XOR each take two operands; NOT (~) takes one operand and flips every bit. The shift operators slide the bits of the first operand left or right by the number of places given in the second operand. JavaScript performs these on 32-bit signed integers, which is the model this calculator follows.
The six operators
How each one combines or moves bits.
| Operator | Symbol | Meaning |
|---|---|---|
| AND | & | Result bit is 1 only when both input bits are 1. |
| OR | | | Result bit is 1 when either input bit is 1. |
| XOR | ^ | Result bit is 1 when the two input bits differ. |
| NOT | ~ | Flips every bit of one operand; ~n = −n − 1. |
| Left shift | << | Moves bits left by n places, filling 0 on the right (× 2ⁿ). |
| Right shift | >> | Moves bits right by n places, keeping the sign (÷ 2ⁿ, floored). |
Worked example: 12 & 10
AND keeps a bit only where both numbers have a 1 in that column. Writing both in binary and comparing column by column gives the answer directly.
- 1 Write both operands in binary. 12 = 1100 and 10 = 1010. Pad them to the same width so the columns line up.
- 2 Line the bits up in columns. 1100 over 1010 — each position now has a top bit and a bottom bit.
- 3 Apply AND to each column. Keep a 1 only when both bits are 1: columns give 1, 0, 0, 0 → 1000.
- 4 Convert the result to decimal. 1000 in binary is 8, so 12 & 10 = 8.
Where bit operations are used
Bitwise work shows up wherever you pack many yes/no facts into one integer. AND with a mask isolates specific bits (for example x & 0xFF keeps the low byte); OR sets flags on; XOR toggles them. Shifts are a fast way to multiply or divide by powers of two: x << 1 doubles a value and x >> 1 halves it (rounding toward negative infinity). Remember that JavaScript treats operands as 32-bit signed integers, so the top bit is the sign and NOT returns ~n = −n − 1 rather than a large positive number.