Skip to main content

Bit Shift Calculator

Free Bit shift Calculator for exponents & logarithms. Enter values to get step-by-step solutions with formulas and graphs.

Share this calculator

Formula

Left: value << n = value * 2^n | Right: value >> n = value / 2^n

Left shift moves bits to the left by n positions, filling with zeros on the right, equivalent to multiplication by 2^n. Right shift moves bits right, with arithmetic shift preserving the sign bit and logical shift filling with zeros.

Worked Examples

Example 1: Left Shift for Fast Multiplication

Problem: Compute 13 << 3 (left shift 13 by 3 positions) and verify it equals 13 * 8.

Solution: 13 in binary: 00001101\nLeft shift by 3: 01101000\n\n01101000 in decimal = 64 + 32 + 8 = 104\n13 * 8 = 104\n\nMultiplier = 2^3 = 8\nBits shifted: the three rightmost positions are filled with 0s\nNo bits are lost since the result fits in the bit width.

Result: 13 << 3 = 104 (equivalent to 13 * 2^3 = 13 * 8)

Example 2: Arithmetic vs Logical Right Shift

Problem: Compare arithmetic and logical right shift of -24 by 2 positions in 32-bit.

Solution: -24 in 32-bit two's complement:\n11111111111111111111111111101000\n\nArithmetic right shift >> 2 (preserves sign):\n11111111111111111111111111111010 = -6\n\nLogical right shift >>> 2 (fills with zeros):\n00111111111111111111111111111010 = 1073741818\n\nArithmetic: -24 / 4 = -6 (rounded toward negative infinity)\nLogical: treats as unsigned, divides by 4

Result: Arithmetic: -24 >> 2 = -6 | Logical: -24 >>> 2 = 1073741818

Frequently Asked Questions

What is the difference between logical and arithmetic right shift?

Logical right shift (>>>) fills the vacated high-order bits with zeros, regardless of the original sign bit. Arithmetic right shift (>>) preserves the sign by filling vacated bits with copies of the original sign bit (the leftmost bit). For positive numbers, both operations produce the same result. The difference matters for negative numbers in two's complement representation. For example, arithmetic right shift of -8 (11111000 in 8-bit) by 1 gives -4 (11111100), preserving the negative sign. Logical right shift of the same value gives 124 (01111100), converting it to a positive number. Most programming languages use >> for arithmetic shift and >>> for logical shift.

Why are bit shifts faster than multiplication and division?

Bit shifts are faster than multiplication and division because they correspond to simple hardware operations that rearrange bit positions in a register, while multiplication requires multiple addition steps and division requires iterative subtraction. Modern CPUs execute bit shifts in one clock cycle, whereas multiplication might take 3-5 cycles and division 20-40 cycles depending on the architecture. Compilers often automatically convert multiplications and divisions by powers of 2 into bit shifts as an optimization. For example, x * 8 becomes x << 3, and x / 4 becomes x >> 2. This optimization is particularly important in embedded systems, game engines, and real-time processing where every CPU cycle matters.

How are bit shifts used in programming and computer science?

Bit shifts have numerous practical applications in programming. They are used for fast multiplication and division by powers of 2, which is common in graphics rendering and game physics. Hash functions use shifts to distribute bits evenly across hash values. Cryptographic algorithms like AES and SHA extensively use bit rotations (circular shifts). In network programming, bit shifts extract individual bytes from multi-byte integers for protocol parsing. Color manipulation in graphics uses shifts to isolate red, green, and blue channels from packed pixel values. Memory-mapped hardware registers in embedded systems require bit shifts to set or read specific bit fields. These applications make bit manipulation a core skill for systems programmers.

What happens when you shift by more bits than the word size?

The behavior of shifting by more than the word size depends on the programming language and architecture. In C and C++, shifting by an amount equal to or greater than the bit width is undefined behavior, meaning the compiler can produce any result. In Java, the shift amount is masked: for 32-bit integers, only the lower 5 bits of the shift amount are used (shift mod 32), so shifting by 33 is the same as shifting by 1. In JavaScript, all numbers are converted to 32-bit integers for bit operations, and the shift amount is masked to 5 bits. Python handles arbitrary-precision integers, so left shifts always work but can produce very large numbers. Understanding these language-specific rules is essential to avoid subtle bugs.

What is a circular shift or bit rotation?

A circular shift (bit rotation) is a shift operation where bits that would be lost from one end are inserted at the other end, so no information is lost. In a left rotation by 1 of an 8-bit number, the leftmost bit wraps around to become the rightmost bit. For example, rotating 10110001 left by 1 gives 01100011. Standard programming languages typically do not have a built-in rotation operator, so it is implemented using a combination of left shift, right shift, and OR: rotateLeft(x, n) = (x << n) | (x >> (bitWidth - n)). Bit rotations are critical in cryptographic algorithms like MD5, SHA-1, and AES, where they provide mixing and diffusion of bits without losing any information from the original value.

How do bit shifts work with negative numbers in two's complement?

In two's complement representation, negative numbers have a 1 in the most significant bit (sign bit). Left shifting a negative number shifts all bits left and fills with zeros on the right, which can change the sign or magnitude in unexpected ways. Arithmetic right shift preserves the sign by filling with 1s on the left, effectively performing signed division by 2 with rounding toward negative infinity. For example, -7 in 8-bit two's complement is 11111001. Arithmetic right shift by 1 gives 11111100, which is -4 (rounding -3.5 toward negative infinity). This differs from truncation division, which would give -3. Understanding this rounding behavior is important for correct signed integer arithmetic using shifts.

References