Compute left shift (<<) and right shift (>>) on any integer — instant binary, decimal & hex results, visual bit movement diagram, logical vs arithmetic shift guide, powers-of-2 reference, and code examples for C, Python, Java & JavaScript.
Calculate bit shift operations (<<, >>) with binary and decimal results
| Operation | Effect | Example (x = 8) |
|---|---|---|
| x << 1 | x × 2 | 8 << 1 = 16 |
| x << 2 | x × 4 | 8 << 2 = 32 |
| x << 3 | x × 8 | 8 << 3 = 64 |
| x >> 1 | x ÷ 2 | 8 >> 1 = 4 |
| x >> 2 | x ÷ 4 | 8 >> 2 = 2 |
| x >> 3 | x ÷ 8 | 8 >> 3 = 1 |
Left Shift (<<): Moves bits to the left by the specified amount. Adds zeros on the right. This operation multiplies the number by 2 for each shift.
Right Shift (>>): Moves bits to the right by the specified amount. Discards bits shifted off. This operation divides the number by 2 for each shift (ignoring remainders).
This bit shift calculator instantly computes left shift (<<) and right shift (>>) operations on any integer with binary, decimal, and hexadecimal results, a visual bit movement diagram, a place-value chart, logical vs arithmetic right shift explanation, and code examples for C, Python, Java, and JavaScript. Bit shifting is one of the most fundamental and efficient operations in digital computing — every CPU executes it in a single clock cycle, and it is the basis for fast power-of-two arithmetic, color channel extraction, hardware register manipulation, and embedded systems programming.
A bit shift moves all the binary digits of an integer a fixed number of positions to the left or right. Left shift (<<) slides every bit toward the most significant end, filling the vacated right positions with zeros — equivalent to multiplying by a power of two. Right shift (>>) slides every bit toward the least significant end, discarding the bits that fall off — equivalent to integer floor division by a power of two. Bits that move beyond the storage width of the data type are simply discarded.
| Operation | Operator | Formula | Worked Example |
|---|---|---|---|
| Left Shift | x << n | x × 2^n (no overflow) | 5 << 3 = 5 × 8 = 40 |
| Right Shift (unsigned) | x >> n | floor(x ÷ 2^n) | 40 >> 3 = 40 ÷ 8 = 5 |
| Right Shift (signed neg.) | x >> n | Arithmetic: sign bit propagates | -8 >> 1 = -4 |
| Single left shift | x << 1 | x × 2 | 7 << 1 = 14 |
| Single right shift | x >> 1 | floor(x ÷ 2) | 7 >> 1 = 3 |
| Shift by 0 | x << 0 | x (unchanged) | 42 << 0 = 42 |
| Shift n | Multiplier | 1 << n (decimal) | Hex | Bit pattern (32-bit) |
|---|---|---|---|---|
| 0 | ×1 | 1 | 0x00000001 | ...00000001 |
| 1 | ×2 | 2 | 0x00000002 | ...00000010 |
| 2 | ×4 | 4 | 0x00000004 | ...00000100 |
| 3 | ×8 | 8 | 0x00000008 | ...00001000 |
| 4 | ×16 | 16 | 0x00000010 | ...00010000 |
| 7 | ×128 | 128 | 0x00000080 | ...10000000 |
| 8 | ×256 | 256 | 0x00000100 | ...1 00000000 |
| 15 | ×32768 | 32 768 | 0x00008000 | ...1 0000...0000 |
| 16 | ×65536 | 65 536 | 0x00010000 | — |
| 24 | ×16777216 | 16 777 216 | 0x01000000 | — |
| 31 | ×2147483648 | 2 147 483 648 | 0x80000000 | MSB only |
| Property | Logical Right Shift | Arithmetic Right Shift |
|---|---|---|
| Vacated left bits | Always filled with 0 | Filled with copy of sign bit |
| Positive numbers | Same result as arithmetic | Same result as logical |
| Negative numbers | Becomes large positive | Stays negative (rounds toward -∞) |
| Example: -8 >> 1 | = 2147483644 (32-bit unsigned) | = -4 (preserves sign) |
| C / C++ | Use unsigned type: (unsigned)x >> n | Signed type >> (impl-defined, usually arith.) |
| Java | >>> operator | >> operator |
| JavaScript | >>> operator | >> operator |
| Python | N/A (arbitrary precision) | >> always arithmetic |
| Rust | u-type (u32, u64) uses >> | i-type (i32, i64) uses >> |
| Expression | 8-bit Result | What Happened |
|---|---|---|
| 1 << 7 | 10000000 = 128 (unsigned) / -128 (signed) | Bit moved into sign bit |
| 1 << 8 | 00000000 = 0 | Bit shifted out of 8-bit width entirely |
| 255 << 1 | 11111110 = 254 (bit 0 lost) | High bit discarded |
| 128 << 1 | 00000000 = 0 | Only set bit shifted out |
| Language | Left Shift | Right Shift (arith.) | Right Shift (logical) | Notes |
|---|---|---|---|---|
| C / C++ | << | >> (signed) | >> (unsigned type) | Signed right shift = impl-defined |
| Java | << | >> | >>> | int=32-bit, long=64-bit |
| JavaScript | << | >> | >>> | 32-bit signed for << and >> |
| Python | << | >> | N/A | Arbitrary precision — no overflow |
| C# | << | >> | >>> (C# 11+) | |
| Rust | << | >> (i-type arith.) | >> (u-type logical) | Panics on overflow in debug mode |
| x86 ASM | SHL / SAL | SAR | SHR | Separate arithmetic/logical instructions |
x << n = x × 2^n and x >> n = floor(x ÷ 2^n) for unsigned values. On older processors, shift instructions execute in one cycle while multiply may take 20+. Modern compilers automatically convert multiplication by constants to shifts, but explicit shifts make the intent clear in embedded systems where every instruction counts.
A 32-bit ARGB pixel packs four 8-bit channels into one integer. Unpacking: alpha = (pixel >> 24) & 0xFF; red = (pixel >> 16) & 0xFF; green = (pixel >> 8) & 0xFF; blue = pixel & 0xFF. Repacking: pixel = (a << 24) | (r << 16) | (g << 8) | b. This is used in every image processing library.
Microcontroller peripheral registers pack multiple fields into one word. To read a 3-bit field starting at bit 4: field = (reg >> 4) & 0x7. To write it: reg = (reg & ~(0x7 << 4)) | (value << 4). This idiom appears in every embedded driver.
Fixed-point numbers represent fractions by treating the lower n bits as the fractional part. Multiplying two Q16.16 fixed-point numbers: result = (a * b) >> 16. Shifting maintains precision without floating-point hardware, used in digital signal processors and game physics.
Bit rotation (left or right rotate — a shift where bits that fall off one end are reinserted at the other) is used in SHA-256, MD5, and AES. Implemented as: rotate_left(x, n) = (x << n) | (x >> (32 - n)).
1 << n creates a bitmask with exactly bit position n set to 1 and all others 0. For example, 1 << 3 = 8 = 0b00001000. This is used constantly in embedded code to target specific register bits: reg |= (1 << 5) sets bit 5.
For unsigned integers and values that don't overflow the data type width, yes — x << 1 = x × 2, x << n = x × 2^n. Overflow occurs when a 1-bit is shifted beyond the MSB, causing the result to wrap around or become 0.
>> is the arithmetic right shift — it copies the sign bit into vacated positions, so negative numbers stay negative. >>> is the logical right shift — it always fills with 0s, making negative signed integers become large positive values. Python has no >>> because its integers have arbitrary precision and >> always behaves arithmetically.
A shift discards bits that fall off one end. A rotation wraps them around to the other end — bits shifted off the MSB reappear at the LSB (left rotation) or vice versa (right rotation). Rotations are common in cryptographic algorithms and are implemented as: rotl(x,n) = (x << n) | (x >> (bits-n)).
Right shift performs integer floor division: floor(7 ÷ 2) = 3. The remainder (the bit that falls off) is discarded. This is why right shift is used for fast integer halving but cannot represent fractional results.