Bitwise Operation Simulator (AND, OR, XOR, NOT, Shift)

Compute AND, OR, XOR, NOT, left shift, and right shift on two numbers and visualize exactly which bits changed on a binary grid. Supports 8/16/32-bit widths.

Usage Tips

  • You can enter values in decimal, or use a `0x1A` hexadecimal or `0b1010` binary prefix. Paste values straight from your code, exactly as you'd write them in a program.
  • AND, OR, and XOR use two values, while NOT uses only one. The number of input fields changes automatically depending on the operation you pick.
  • Right shift (`>>>`) only supports the logical variant here. See the FAQ for how it differs from an arithmetic shift that preserves the sign bit.
  • Switching the bit width from 8 to 16 to 32 lets you see the same number gain more leading zero bits, which is a great way to build intuition for what "bit width" actually means.
  • Bits highlighted in yellow are the ones that changed between A and the result, so with AND/OR you can visually trace which input "won" at each bit position.

Frequently Asked Questions

Yes. A logical shift (`>>>`) always fills the vacated high bits with 0, while an arithmetic shift (`>>`) copies the sign bit (the most significant bit) into them instead. Since this tool treats values as non-negative integers within the chosen bit width, sign isn't really the point here, so it only offers the simpler, easier-to-understand logical shift.

JavaScript's bitwise operators compute internally as 32-bit signed integers, so applying NOT to an 8-bit `00000001` (1) normally gives `-2`. This tool re-masks the result to your chosen bit width (8/16/32), so for 8 bits it correctly shows `11111110` (254) — the proper representation within that width.

AND returns 1 only when both bits are 1, so it's commonly used to mask out specific bits. OR returns 1 when either bit is 1, so it's used to set specific bits. XOR returns 1 only when the bits differ, so it's used to toggle bits or swap two values.

Left shift (`<<`) is a fast way to multiply by powers of two (doubling, quadrupling, and so on), while right shift (logical, as used here) is commonly used to divide by two or to pull out a specific range of bits from a value. Both show up constantly in competitive programming and bitmask-based algorithms.

Not quite — they're related but distinct terms. Bitwise operations refer to the operations themselves (AND, OR, XOR, NOT, shifts), while a bitmask is a value used together with one of those operations (typically AND or OR) to read or modify specific bits. For example, `value & 0b00001111` is a classic bitmask that extracts just the lowest 4 bits.
ツールくん

Side Note — The Curious Properties of XOR

Bitwise operations map directly onto a computer's logic circuits, making them among the fastest operations a CPU can execute — often a single clock cycle. Because they need far simpler circuitry than arithmetic like multiplication or division, performance-critical code (image processing, cryptography, network protocol implementations) has long used bitwise tricks as substitutes for costlier arithmetic.

XOR (exclusive OR) has a couple of neat properties: XOR-ing a value with itself always yields 0 (`a ^ a = 0`), and XOR-ing with 0 returns the original value unchanged (`a ^ 0 = a`). Combining these two facts gives rise to the classic "XOR swap" trick, which exchanges two variables without a temporary one (`a ^= b; b ^= a; a ^= b;`). It's rarely recommended in production code today for readability reasons, but it remains a favorite teaching example for bitwise intuition.

Bitmasks show up constantly in real-world systems. Unix file permissions (read=4, write=2, execute=1) are literally bitwise OR and AND at work, and pulling the red, green, and blue components out of a color code like `#FF0000` is done with a right shift combined with an AND mask. Packing many on/off flags into a single integer — a technique often called "bit flags" — was widely used in older games and systems that needed to track a lot of state with very little memory.