A bit shift moves all the bits of a value left or right by a number of positions. Shifts are among the fastest CPU operations and underpin fast multiplication and division by powers of two, bit packing, and hash functions. This calculator supports all three common shift modes at fixed register widths.
How it works
Within a register of width bits (mask = (1 << width) - 1):
- Left shift (
<<) — every bit moves toward the most significant end; zeros enter from the right and bits that fall off the top are discarded. This multiplies by2^nmodulo the width. - Logical right shift (
>>>) — bits move toward the least significant end; zeros enter from the left. This is unsigned division by2^n. - Arithmetic right shift (
>>) — like the logical shift, but the sign bit is copied into the vacated high bits, preserving the sign of a two’s-complement value.
Example
Take the 8-bit value 0b00010110 (22):
- Left shift by 2 →
0b01011000= 88 (which is 22 × 4). - Logical right shift by 1 →
0b00001011= 11.
Now take 0b10110110 interpreted as signed (−74). An arithmetic right shift by 1 gives 0b11011011 (−37), keeping the value negative, whereas a logical right shift would give 0b01011011 (91), changing the sign.
Notes
Choose the width to match your target type. Note that arithmetic right shift rounds toward negative infinity, so -1 >> 1 stays -1, unlike integer division which would round toward zero — a subtle but important difference when implementing signed math with shifts.
Practical applications of bit shifts
Bit shifts are not just a theoretical curiosity — they appear throughout systems programming, graphics, and protocol work:
Fast multiplication and division by powers of two
Compilers routinely replace x * 4 with x << 2 in generated code because a shift is a single CPU instruction with one cycle latency, while a general multiply may take several cycles. Division by a power of two uses an arithmetic right shift on signed values or a logical right shift on unsigned values. The key restriction: this only works for exact powers of two (2, 4, 8, 16, 32…).
Extracting fields from packed integers
Network packets, binary file headers, and hardware registers pack multiple fields into a single integer. To extract a field, you mask and shift. For example, to get bits 4–7 of a byte: (value >> 4) & 0x0F. This is the standard idiom for parsing colour values from a 32-bit ARGB pixel, reading IP header fields, or decoding instruction encodings.
Building flags and bitmasks
Setting individual bits with 1 << n is the standard way to define flag constants: const READ = 1 << 0; const WRITE = 1 << 1; const EXEC = 1 << 2;. Testing a flag: if (mode & WRITE). Clearing a flag: mode &= ~WRITE.
Hash functions and pseudorandom generators Many fast hash functions and linear feedback shift registers (LFSRs) use a combination of left shifts, right shifts, and XOR to mix entropy. The shift operations are chosen so that every input bit eventually influences every output bit over enough rounds.
Language-specific shift behaviour
Different languages handle the distinction between logical and arithmetic right shift differently:
| Language | Signed right shift >> | Unsigned right shift |
|---|---|---|
| C / C++ | Implementation-defined (usually arithmetic) | >> on unsigned types |
| Java | >> is arithmetic; >>> is logical | >>> operator |
| JavaScript | >> is arithmetic; >>> is logical | >>> operator |
| Python | >> is arithmetic (extends sign bit) | No separate operator; mask manually |
| Go | >> is arithmetic for signed, logical for unsigned | Use uint type |
This calculator lets you choose explicitly, making it useful for verifying the expected result across languages before writing the code.