CS With NehalCS With Nehal

Bitwise Operations Simulator

Apply AND, OR, XOR, NOT, logical and arithmetic shifts and rotations to 8-bit values, and use masks to set, clear and test bits.

Operation

Result bit is 1 only if both bits are 1. Use it with a mask to clear bits or test one bit.

Bits

Bits

Click bits of A or B to change them. Bits that change in the result are marked orange.

b7b6b5b4b3b2b1b0
A
182 · 0xB6
AND B
15 · 0x0F
Result
0
0
0
0
0
1
1
0
6 · 0x06

Common masks

182 / -74
A (unsigned / signed)
6 / 6
Result (unsigned / signed)

AND truth table

ABA AND B
000
010
100
111

About This Simulation

This bitwise operations simulator applies AND, OR, XOR, NOT, shifts and rotations to 8-bit values. Click bits to change the operands, try common masks for setting, clearing, toggling and testing bits, and see the result in binary, denary and hexadecimal.

Features

  • AND, OR, XOR and NOT on values from 1 to 16 bits
  • Logical shift left and right, arithmetic shift right
  • Rotate left and right
  • Clickable bits and preset bit masks
  • Truth tables and ×2 / ÷2 explanations for shifts

Topics covered

  • Bitwise Operators
  • Bit Masking
  • Logical Shifts
  • Arithmetic Shifts
  • Rotation

Explanation

Bitwise operations work on each bit of a binary number separately. They are used to manipulate individual bits, for example to read status flags, set options, pack data efficiently or multiply and divide quickly by powers of two.

How it works

  1. AND with a mask keeps only the bits where the mask is 1. Use it to clear bits or to test whether a bit is set.
  2. OR with a mask sets the bits where the mask is 1 and leaves the others unchanged.
  3. XOR with a mask flips (toggles) the bits where the mask is 1.
  4. A logical shift left by n multiplies an unsigned value by 2ⁿ; a logical shift right divides by 2ⁿ, with 0s coming in.
  5. An arithmetic shift right copies the sign bit in, so it divides signed two's complement numbers correctly.
  6. A rotate (circular shift) moves the bit that falls off one end back in at the other end.

Why it matters

Shifts and masks are on the Cambridge AS Level syllabus and in assembly language programming. Bitwise operations are used in device drivers, graphics, networking (subnet masks), encryption and compression.

Frequently Asked Questions

What is a bit mask?

A binary value used with AND, OR or XOR to select which bits of another value are cleared, set or toggled.

What is the difference between a logical and an arithmetic shift?

A logical right shift always brings in 0s. An arithmetic right shift copies the sign bit, so negative two's complement numbers stay negative.

How do you test if a bit is set?

AND the value with a mask that has only that bit set. A non-zero result means the bit is 1.

Why can shifting left lose information?

Bits shifted off the left end are discarded, so if any of them were 1 the result is no longer the original value × 2.

Related Simulations