How the bitwise calculator works
Pick an operation, a width and how you want to type numbers. Operands shorter than the width are padded with zeros, and a negative decimal is stored as its two's complement pattern, the way a signed integer variable holds it. The result card shows the result bits with their hex, unsigned and signed values, and the bit-by-bit panel stacks A, B and the result so you can check each column.
For shifts, the vacated positions are shaded and the bits pushed off the end are listed. Switching the input between binary, hex and decimal rewrites the operands without changing their bits. For ordinary addition and multiplication, use the binary calculator.
The bitwise operators
Example: NOT 00000101 at 8 bits is 255 โ 5 = 250 = 11111010, which reads as โ6 in two's complement.
Worked examples
Example 1 โ masking the low nibble
- A:
- 1011 0110 (0xB6)
- Mask:
- 0000 1111 (0x0F)
- High four bits AND 0: 0000
- Low four bits AND 1: 0110
A AND 0x0F: 0000 0110 (6)
AND with 0x0F keeps the low four bits โ the second hex digit โ and clears the rest.
Example 2 โ arithmetic versus logical shift
- A (8-bit):
- 1111 0000 (โ16)
- Shift:
- right by 2
- Logical >>>: fill with 0: 0011 1100 (60)
- Arithmetic >>: copy the sign bit: 1111 1100 (โ4)
โ16 >> 2: โ4
The arithmetic shift divides a signed value by 4; the logical shift treats it as unsigned 240 and gives 60.
Truth table for AND, OR, XOR and NOT
| A | B | A AND B | A OR B | A XOR B | NOT A |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 |
Key facts
- A bitwise AND gives 1 in a position only when both input bits are 1, so ANDing with a mask keeps the masked bits and clears the rest.
- In two's complement, NOT x equals โx โ 1, so NOT 5 is โ6.
- An arithmetic right shift copies the sign bit into the vacated positions, while a logical right shift fills them with zeros.
- In JavaScript, a shift count on ordinary numbers is taken modulo 32, so 1 << 32 evaluates to 1.
Where bitwise operations are used
Bitwise operators pack and unpack data: flags stored as single bits in a permissions value, colour channels in a 32-bit pixel, fields in a network header or a hardware register. Programmers use this calculator to check a mask before putting it in code, to see why a shift gave a surprising answer, or to decode a value read from a device. The language rules matter: JavaScript converts numbers to 32-bit integers and takes the shift count mod 32 (source: MDN), and its >> copies the sign bit (source: MDN). Hex input pairs well with the binary to hex converter.
Common bitwise mistakes
- Confusing & with &&. Bitwise AND works bit by bit (6 & 3 = 2); logical AND only asks whether both sides are non-zero.
- Ignoring the width. NOT 5 is 250 in 8 bits but 65,530 in 16 bits; the width decides how many bits flip.
- Using >> on unsigned data. A pattern with its top bit set gets ones shifted in; use the logical shift for unsigned values.
- Counting bits from the left. Bit 0 is the rightmost, least significant bit, so the mask for bit n is 1 << n.
Frequently Asked Questions
What is 5 AND 3?
5 AND 3 is 1. In binary 5 is 101 and 3 is 011; only the last bit is 1 in both, so the result is 001. 5 OR 3 is 111 (7), and 5 XOR 3 is 110 (6).
What is the difference between >> and >>>?
>> is the arithmetic (sign-propagating) right shift: it fills the vacated top bits with copies of the sign bit, so a negative number stays negative, and โ9 >> 2 is โ3. >>> is the logical right shift: it always fills with zeros, so it treats the pattern as unsigned.
Why is NOT 5 equal to โ6?
NOT flips every bit. 5 in 8 bits is 00000101, and flipped it is 11111010. Read as unsigned that is 250; read as two's complement it is โ6. In two's complement, NOT x is always โx โ 1, which is why ~5 is โ6 in most programming languages.
What happens when you shift by the width or more?
Mathematically every bit is pushed out, so a left or logical right shift gives 0 and an arithmetic right shift of a negative number gives all ones; that is what this calculator shows. Languages differ: JavaScript takes the shift count mod 32, so 1 << 32 is 1, and in C shifting by the width or more is undefined behaviour.
How do you use AND to check whether a bit is set?
AND the value with a mask that has a 1 only in that bit's position. If the result is non-zero the bit is set. To test bit 3 of 10110110, AND it with 00001000: the result is 00000000, so bit 3 is clear. Bits are counted from 0 on the right.
Why does XOR swap and toggle bits?
XOR with 1 flips a bit and XOR with 0 leaves it alone, so XOR with a mask toggles exactly the masked bits. Doing it twice restores the original, because x XOR m XOR m = x. That property is used for toggling flags, simple checksums and parity.
Related calculators
Notes & Assumptions
- Operands are fixed-width patterns of 8, 16, 32 or 64 bits; a value that needs more bits than the width is reported rather than cut down.
- Shifts are computed mathematically: shifting by the width or more empties the register, which differs from JavaScript's mod-32 rule and from C, where it is undefined.
- Negative decimal input is stored as two's complement at the chosen width.
This tool is for education and debugging. Check how a specific language or processor treats shift counts and signed values against its documentation.