Bit shift and bitwise calculator
Pick a width, an operation and your numbers (decimal, 0x hex, 0b binary or 0o octal) to see the result as unsigned and signed values, in hex, and as an aligned bit diagram. Calculations use exact big integers, so 64 bits are no problem.
Runs in your browser: nothing you enter is sent anywhere.
How to use it
- Choose the width and the operation.
- Enter A, and B (the other operand or the shift count). NOT needs only A.
- Read the result in decimal, signed decimal and hex, and follow the bit diagram.
How it works
Both operands become unsigned patterns of the chosen width (negative values are wrapped by adding 2^width). The operation is done with BigInt, and the result is cut to the width with a mask. The signed reading subtracts 2^width when the top bit is set.
Example: 5 << 2 in 8 bits: 0000 0101 becomes 0001 0100, which is 20. NOT 5 in 8 bits is 1111 1010, which is 250 unsigned and -6 signed.
Tips and common mistakes
- A left shift by k is a multiplication by 2^k only if no 1 bits are lost on the left.
- Right-shifting a negative number by 1 is a floor division by 2, not truncation toward zero.
- Masks are usually easiest in hex:
0xF0 AND 0x3C is 0x30.
Frequently asked questions
What is the difference between >> and >>>?
Arithmetic shift right (>>) copies the sign bit into the vacated positions, so negative numbers stay negative: -8 as 8-bit is 11111000, and >> 1 gives 11111100 (-4). Logical shift right (>>>) fills with zeros, so the same input gives 01111100 (124).
What does shifting left do mathematically?
Shifting left by k multiplies by 2^k, as long as the result still fits in the chosen width. Bits pushed out of the top are lost, and the tool shows them. 192 << 1 in 8 bits gives 128 and loses a 1.
How are negative numbers handled?
A negative input is turned into its two's complement pattern for the chosen width, and the result is shown both as an unsigned number and as a signed one. For 8 bits the allowed inputs run from -128 to 255.
What is a rotate?
A rotate moves bits out of one end and back in at the other, so nothing is lost. Rotating 10000001 left by 1 gives 00000011.
Why do languages give different results for big shift counts?
C gives no defined result for a shift by the width or more, and JavaScript uses only the low 5 bits of the count for 32-bit shifts. This tool allows counts from 0 to the width and follows plain mathematics: shifting by the full width clears the value, or fills with the sign bit for the arithmetic shift.