Bitwise Calculator
Combine two numbers bit by bit. Type them in binary, hex or decimal, choose AND, OR, XOR, a shift or a rotate, and click the bits to see how the pattern changes.
https://flxcode.com/bitwise-calculator/
- T
- 0
- Z
- 0
- Y
- 0
Type with the keyboard or the keys below, or paste a number. Click a row to type in that base.
Bits (click one to flip it)
Try an example
History (this visit only)
Nothing yet. Results appear here, newest first.
Keyboard shortcuts
| 0–9, A–F | type digits (digits outside the input base are refused) |
|---|---|
| + − * / % | add, subtract, multiply, divide, modulo |
| & | ^ ~ | AND, OR, XOR, NOT |
| q w | NAND, NOR |
| < > s | shift left, shift right (logical), shift right (arithmetic) |
| r Shift+R | rotate left, rotate right |
| n | change sign |
| Enter or = | equals (ENTER in RPN mode) |
| Backspace | delete the last digit |
| Esc | clear the number; press again to clear everything |
| x k Delete | RPN: swap X and Y, roll the stack down, drop X |
No ads. No account. Nothing you type or record is uploaded. We count page events with a daily code. Check it.
Problem or idea? Email us
Quick start
- BIN is the input row. Type the first number, or click bits in the grid to build it.
- Press AND, OR, XOR, NAND or NOR, type the second number and press =. NOT acts on the number on screen at once.
- For a shift or a rotate, the second number is the count.
- Read the answer in the BIN row. The HEX row gives the same pattern in a quarter of the digits.
Worked examples
| Operation | Word | Result in binary | Hex | Decimal | What it does |
|---|---|---|---|---|---|
| 1100 AND 1010 | 8-bit unsigned | 0000 1000 | 08 | 8 | A bit survives only where both numbers have one. |
| 1100 OR 1010 | 8-bit unsigned | 0000 1110 | 0E | 14 | A bit is set where either number has one. |
| 1100 XOR 1010 | 8-bit unsigned | 0000 0110 | 06 | 6 | A bit is set where the numbers differ. |
| NAND 1100, 1010 | 8-bit unsigned | 1111 0111 | F7 | 247 | AND, then every bit flipped, up to the word width. |
| NOT 0101 1010 | 8-bit unsigned | 1010 0101 | A5 | 165 | At 8 bits this is the same as XOR with 1111 1111. |
| 1 SHL 7 | 8-bit unsigned | 1000 0000 | 80 | 128 | Bit 0 moves to bit 7: 1 × 2⁷ = 128. |
| 1000 0001 ROL 1 | 8-bit unsigned | 0000 0011 | 03 | 3 | The top bit comes round to the bottom. |
| 1000 0000 SAR 2 (signed) | 8-bit signed | 1110 0000 | E0 | -32 | Sign bits are copied in: -128 becomes -32. |
Truth tables
Each gate looks at one bit from each number. The columns are the four input pairs.
| Gate | 0, 0 | 0, 1 | 1, 0 | 1, 1 |
|---|---|---|---|---|
| AND | 0 | 0 | 0 | 1 |
| OR | 0 | 1 | 1 | 1 |
| XOR | 0 | 1 | 1 | 0 |
| NAND | 1 | 1 | 1 | 0 |
| NOR | 1 | 0 | 0 | 0 |
| Input | NOT |
|---|---|
| 0 | 1 |
| 1 | 0 |
Mask recipes
All five start from the value 0100 0001 (0x41, the letter A in ASCII).
| Goal | Operation | Mask | Result |
|---|---|---|---|
| Set bit 3 | OR | 00001000 | 0100 1001 |
| Clear bit 6 | AND | 10111111 | 0000 0001 |
| Flip bit 0 | XOR | 00000001 | 0100 0000 |
| Keep the low nibble | AND | 00001111 | 0000 0001 |
| Test bit 6 (non-zero if set) | AND | 01000000 | 0100 0000 |
Setting a bit that is already set, or clearing one that is already clear, changes nothing. XOR is the only one of the three that always changes the bit.
Shifts and rotates
- SHL n multiplies an unsigned number by 2 to the power of n, until bits fall off the top. The carry flag shows the last bit that fell off.
- SHR n divides by 2 to the power of n and drops the remainder.
- SAR n does the same for a signed number by copying the sign bit.
- ROL and ROR turn the bits round the word, so no bit is lost.
Signed and unsigned
Bit operations do not care how you read the bits, but the decimal row does. 1111 1111 is 255 unsigned and -1 signed. The AND, OR and XOR results are the same bits either way. Shifts to the right differ: SHR is logical and SAR is arithmetic. Switch the reading with the Signed button.
Numbers this uses
- Word sizes 8, 16, 32 and 64 bits, exact integers (BigInt). This page starts at 8 bits.
- A shift count of the word size or more shifts every bit out. A rotate count is taken modulo the word size.
- NAND and NOR are AND and OR followed by NOT, cut to the word size.
For ordinary arithmetic with carry and borrow see the binary calculator and the hex calculator. The programmer calculator adds RPN, a history and every word size.
Nothing you type is uploaded. We count events without cookies (details).
FAQ
What is a bitwise operation?
An operation that works on each bit position on its own. AND of two numbers looks at bit 0 of both, then bit 1 of both, and so on, and builds a new number from the answers. Nothing carries from one position to the next, which is what separates it from addition.
What is the difference between AND and OR?
AND gives 1 only where both inputs have a 1, so it is used to keep or test bits. OR gives 1 where either input has a 1, so it is used to turn bits on. The truth tables above show all four input pairs for each.
How do I use a bit mask?
To keep certain bits, AND with a mask that has ones in those positions. To set bits, OR with a mask. To flip them, XOR with a mask. To clear them, AND with the mask turned inside out, which is NOT of the bits you want to clear. The mask table above has one example of each.
What does XOR do that OR does not?
XOR is 1 only when the two bits differ. 1 XOR 1 is 0, where 1 OR 1 is 1. That makes XOR a toggle: XOR twice with the same mask gives the original value back. It also makes XOR a quick equality check, because x XOR x is 0.
Why does NOT depend on the word size?
NOT flips every bit in the word, and the word has a fixed number of them. NOT 0000 0101 is 1111 1010 at 8 bits, but 1111 1111 1111 1010 at 16 bits. The calculator masks the answer to the chosen size, so at 8 bits NOT x is 0xFF XOR x.
Why does shifting left multiply by two?
Each place in binary is worth twice the one on its right, so moving every bit one place left doubles the value. Shifting right by one divides by two and drops the remainder. SHL by n multiplies by 2 to the power of n, until bits fall off the top.
When do I use SAR instead of SHR?
Use SAR for signed numbers. It copies the sign bit into the empty space, so a negative number stays negative: -128 SAR 2 is -32. SHR puts zeros in, which turns a negative number into a large positive one.
Is anything I type saved or sent?
No. Everything runs in your browser. What you type is not put in the address bar, not stored and not counted. Only your last word size and base are remembered on this device.
More from FLXCode
Programmer CalculatorNew
Hex, decimal, octal and binary at once with 8 to 64-bit words, AND, OR, XOR, shifts, rotates, a bit grid and RPN.
Binary CalculatorNew
Add, subtract, multiply and divide binary numbers with carry and borrow. Also shows the result in hex and decimal.
Hex CalculatorNew
Add, subtract, multiply and divide hexadecimal numbers. Shows carry and overflow for 8, 16, 32 and 64-bit words.
Long Division CalculatorNew
Long division laid out like on paper, with the remainder and the decimal, repeating part marked.