Bitwise Calculator
Calculate AND, OR, XOR, NOT and bit shifts on integers in decimal, hex, binary or octal, with 8, 16, 32 or 64-bit signed or unsigned results shown bit by bit.
Input
Output
Readme
What does a bitwise calculator do?
Bitwise operations work on the single bits of whole numbers instead of on their values. They're how programs set, clear and test flags, apply masks, pack several small values into one number, read network masks and hardware registers, and do fast multiplication and division by powers of two. This calculator applies AND, OR, XOR, NOT and the three bit shifts to integers of 8, 16, 32 or 64 bits and shows the result in every common base and bit by bit.
Writing numbers
- Decimal:
42or-42. - Hexadecimal with
0x:0x2A. Binary with0b:0b101010. Octal with0o:0o52. The letters may be upper or lower case. - Underscores or spaces may group the digits, as in
0b1111_0000or1 000 000. - A negative number is stored in two's complement, the way computers store signed integers: -1 is all ones.
The operations
| Operation | Written as | What it does |
|---|---|---|
| AND | A & B | A bit is 1 only where both A and B have a 1. Used to keep some bits and clear the rest (masking). |
| OR | A | B | A bit is 1 where A or B has a 1. Used to set bits. |
| XOR | A ^ B | A bit is 1 where A and B differ. Used to flip bits and to compare. |
| NOT | ~A | Every bit of A flips. |
| Left shift | A << B | The bits move B places to the left and zeros come in from the right, which multiplies by 2 to the power of B as long as nothing falls off. |
| Arithmetic right shift | A >> B | The bits move B places to the right and copies of the sign bit come in from the left, which divides a signed number by 2 to the power of B, rounding down. |
| Logical right shift | A >>> B | The bits move B places to the right and zeros come in from the left. |
For shifts, B is the shift amount.
Bit width and signed values
- The bit width sets how many bits every value has: 8, 16, 32 or 64. Results are cut to that width, as they are in a CPU register or a fixed-size variable.
- Every result is shown as a signed decimal (two's complement) and as an unsigned decimal, since the same bits mean both. Hex, octal and binary are the bits themselves.
- An input that fits the width as either a signed or an unsigned number is used as is, so
0xFFand-1are the same eight bits. A larger input keeps only its lowest bits, and the calculator tells you. - Shifting by the bit width or more moves every bit out: the result is 0, or all ones for
>>of a negative number. Programming languages differ here: JavaScript and Java use only the lowest 5 bits of the shift amount for 32-bit numbers (6 bits for Java's 64-bit long), sox << 32isxin JavaScript, and C leaves it undefined. - The Bits section lines up A, B and the result bit by bit, highest bit first, with the bit number over each group of four.
Examples
- 202 AND 0x0F in 8 bits is 10 (0x0A): only the lowest four bits of 202 are kept.
- 0b1100 OR 0b0011 is 15 (0b1111).
- 0xF0 XOR 0xFF in 8 bits is 0x0F.
- NOT 0 in 32 bits is -1 as a signed number and 4294967295 (0xFFFFFFFF) as an unsigned one.
- 1 << 7 in 8 bits is 128 unsigned and -128 signed (0x80).
- -16 >> 2 in 32 bits is -4, while -16 >>> 2 is 1073741820.
How it works
The calculator works with JavaScript's BigInt, so every value is exact, 64-bit ones too. Each input is turned into its bit pattern of the chosen width, the operation runs on those bits, and the result is read back as a signed and an unsigned number. Everything happens in your browser.
Features
- AND, OR, XOR, NOT, left shift, arithmetic and logical right shift
- 8, 16, 32 and 64-bit integers
- Decimal, hex, binary and octal input, negative numbers too
- Signed and unsigned decimal, hex, octal and binary results with copy buttons
- A bit-by-bit view of A, B and the result
- Runs in your browser