Bitwise Operations Calculator

Check the results of bitwise logic operations and shifts as rows of binary digits. Try changing the values to see how a mask extracts, sets or flips particular bits, and how shifts multiply and divide by powers of two.

Values

Prefix hexadecimal with 0x and binary with 0b. Negative decimal numbers are read as two's complement.

Result

Logical shifts fill vacated bits with 0; an arithmetic right shift fills them with the most significant (sign) bit. Bits shifted out on the left are discarded. The value in parentheses in the decimal column is the two's complement value with the top bit treated as the sign.

Bitwise logic and masks

AND, OR and XOR operate on the bits in the same position of two values. AND gives 1 only when both bits are 1, so ANDing with a mask (a value with 1s only where you want bits) extracts particular bits. In the example, 0b10110100 AND 0x0F extracts the lower 4 bits, 0100. OR is used to set bits to 1, XOR to flip bits, and NOT flips every bit.

Shifts and powers of two

Shifting left by k bits multiplies the value by 2ᵏ, and shifting right by k bits divides by 2ᵏ and drops the remainder (as long as nothing overflows). Because shifts are faster than multiplication and division, compilers replace multiplication and division by powers of two with shifts. To shift a negative number right, use an arithmetic shift, which fills with the sign bit and gives the two's complement value divided by 2ᵏ (rounded toward negative infinity). A logical shift fills with 0, which turns a negative number into a large positive one.

Rotation

A rotation is a shift in which the bits that fall off one end come back in at the other. Because no bits are lost, rotations are used in cryptography, hash functions and CRC calculations. Many CPUs have rotate instructions, but C has no rotate operator, so it is written with two shifts and an OR, such as (x << k) | (x >> (w − k)), and the compiler turns it into a rotate instruction.

Common bit tricks

x AND 1 tells whether x is odd or even, and x AND (x − 1) clears the lowest 1 bit (repeating it counts the 1 bits). If x AND (x − 1) is 0, x is a power of two. Representing a set of flags (bits that each record a state) as one integer, setting them with OR, clearing with AND and NOT, flipping with XOR and testing with AND is common in embedded programs that manipulate hardware registers.

Practice related questions

x and y are 8-bit unsigned integers. << and >> are logical shifts: vacated bits are filled with 0, and bits shifted out of the 8 bits are discarded. ∨ is bitwise OR. What is the value of y after the following program runs?

  1. A27
  2. B54
  3. C58
Show answer and explanation

Answer: B. 54

13 is 00001101 in binary. x << 2 shifts it 2 bits left to 00110100 = 52, and x >> 1 shifts it 1 bit right to 00000110 = 6 (the lowest 1 shifted out is discarded). Their OR is 00110110 = 54. 52 and 6 both have the bit for 4 set, so the OR differs from the sum 52 + 6 = 58.

x and y are 8-bit unsigned integers. << and >> are logical shifts: vacated bits are filled with 0, and bits shifted out of the 8 bits are discarded. ∨ is bitwise OR. With x = 75, what is the value of y after the following assignment?

  1. A75
  2. B180
  3. C210
Show answer and explanation

Answer: B. 180

75 is 0100 1011 in binary. x << 4 moves the low 4 bits to the top, giving 1011 0000 = 176 (the 0100 shifted out is discarded), and x >> 4 moves the high 4 bits to the bottom, giving 0000 0100 = 4. Their OR is 1011 0100 = 180: the high and low 4 bits are swapped. Treating x << 4 as 75 × 16 = 1200 without limiting it to 8 bits would change the result.

Which expression multiplies an integer x by 10 using shifts and additions instead of a multiply instruction?

  1. A(x << 3) + (x << 1)
  2. B(x << 10)
  3. C(x << 2) + x
Show answer and explanation

Answer: A. (x << 3) + (x << 1)

Shifting left by one bit doubles a value, so x << 3 is 8x and x << 1 is 2x. Since 8x + 2x = 10x, (x << 3) + (x << 1) computes ten times x. Multiplication by a constant can be replaced this way with shifts and additions, and compilers choose whichever is faster. x << 10 multiplies by 2¹⁰ = 1024, and (x << 2) + x gives 4x + x = 5x. Be careful not to confuse the shift amount with the multiplier.

What is the result of arithmetic right-shifting the 8-bit signed integer −8 (1111 1000) by one bit?

  1. A124 (0111 1100)
  2. B−16 (1111 0000)
  3. C−4 (1111 1100)
Show answer and explanation

Answer: C. −4 (1111 1100)

An arithmetic right shift copies the sign bit into the vacated top bit, so 1111 1000 becomes 1111 1100, which is −4 in two's complement. Even for negative numbers, shifting right by one divides by 2. A logical right shift, which inserts 0, gives 0111 1100 = 124, changing the sign. −16 is the result of shifting left by one. Note also that in C, which kind of shift applies to negative signed integers is implementation-defined.

In C, what are the results of −7 / 2 and −7 >> 1 (with an arithmetic shift) for int?

  1. A−7 / 2 is −3, and −7 >> 1 is −4
  2. BBoth are −4
  3. C−7 / 2 is −4, and −7 >> 1 is −3
Show answer and explanation

Answer: A. −7 / 2 is −3, and −7 >> 1 is −4

C (since C99) defines integer division as truncating toward zero, so −3.5 becomes −3. An arithmetic right shift instead rounds toward negative infinity: shifting −7 (1111 1001) right by one gives 1111 1100 = −4. For negative numbers, shifting and dividing therefore disagree, so when compilers replace division by 2 with a shift they add correction instructions for negatives. For positive numbers the two agree.

FAQ

What is the difference between arithmetic and logical shifts?

Left shifts are the same in both: the vacated low bits are filled with 0. For right shifts, a logical shift fills the high bits with 0 and an arithmetic shift fills them with the value of the sign bit. Use an arithmetic shift to divide a signed number by a power of two.

What is a mask?

A value with 1s (or 0s) in the bit positions you want to extract or change. It is used with AND to extract or clear bits, OR to set them and XOR to flip them.

What is the negative number in parentheses in the decimal column?

It is the value read as two's complement, with the top bit as the sign. For example, the 8-bit value 0b10110100 is 180 unsigned and −76 signed.

How does this appear on exams?

IT certification exams such as Japan's Fundamental Information Technology Engineer Examination ask for the results of AND, OR and XOR with a mask, multiplication and division by shifts, the difference between arithmetic and logical shifts, and, in Part B, traces of programs that use bit operations.

Related terms

Bitwise operation Two's complement

Related calculators

Semiconductor, Electronics and Computing Calculators →