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.

What Is a Bitwise Operation?

Bitwise operations treat numbers as sequences of binary digits (0s and 1s) and perform a logical operation on each digit position independently. AND, OR, XOR, NOT, and shift (left and right) are the five representative operations. All five map directly onto basic CPU instructions that hardware executes in a single step, which is why they run far faster than ordinary arithmetic like multiplication or division. In most programming languages you'll see them written as `&`, `|`, `^`, `~`, `<<`, and `>>`, and they show up everywhere from flag management and access permissions to cryptography and image processing.

This simulator lets you specify Value A, Value B (NOT only uses Value A), and a bit width of 8, 16, or 32 bits, then shows you the result of the selected operation visualized bit by bit in a grid. You can type values in decimal, or use a `0x` (hexadecimal) or `0b` (binary) prefix, matching exactly how you'd write them in code. Keep in mind that any value larger than what the chosen bit width can hold won't be represented correctly — deciding on a bit width before you start makes the results much easier to interpret.

How to Use the Bitwise Operation Simulator

  1. Choose a bit width Pick 8, 16, or 32 bits. If you're not sure, start with 8-bit — the grid is small enough that you can see every change at a glance.
  2. Choose an operation Select AND, OR, XOR, NOT, left shift, or right shift. Picking NOT automatically switches the form down to a single input field for Value A.
  3. Enter your values Type Value A (and Value B, unless you chose NOT) in decimal, hexadecimal (`0x`), or binary (`0b`) notation. For either shift operation, also specify how many bits to shift.
  4. Read the result grid The result is calculated automatically as you type. Bits highlighted in yellow show exactly which positions changed from Value A because of the operation.
  5. Compare decimal and hex values Below the binary grid, the decimal and hexadecimal representations of A, B, and the result are shown side by side, so you can cross-check the outcome however you think about it.

Tips for getting more out of it

  • 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.

Practical Use Cases for Bitwise Operations

Learning bitwise operators in C, Java, or Python

NOT and shift results are notoriously hard to work out in your head, especially with sign bits and bit widths involved. Seeing every bit change on screen makes the logic click much faster than reading it in a textbook.

Designing bit flags and permission systems

Unix-style file permissions and any scheme that packs several on/off states into a single integer rely entirely on AND masking and OR-based flag setting. Try your masks here before wiring them into real code.

Competitive programming and bitmask algorithms

Bitmask dynamic programming and set-as-integer representations are staples of algorithm contests. Running a small example through the simulator is a quick way to confirm your mental model before you code it.

Double-checking hex and binary conversions

Because every value is shown in binary, hex, and decimal at once, this tool doubles as a quick sanity check when you're converting between bases alongside a dedicated base-conversion tool.

Color codes and network address calculations

Extracting the red, green, and blue components from a hex color code, or ANDing an IP address against a subnet mask, are both classic bitwise tasks you can verify by hand in seconds here.

Bitwise Operation Glossary

AND
Returns 1 only when both corresponding bits are 1, and 0 otherwise. Commonly used to mask out, or isolate, specific bits of a value.
OR
Returns 1 when either corresponding bit is 1. Commonly used to set, or turn on, specific bits without disturbing the others.
XOR
Returns 1 only when the two corresponding bits differ. Frequently used to toggle bits, swap values, or detect differences between two numbers.
NOT
Inverts every bit of a single value, flipping each 0 to 1 and each 1 to 0. The exact result depends on the bit width you've chosen, since that determines how many bits get flipped.
Shift operation
Slides an entire bit pattern left or right by a given number of positions. A left shift is equivalent to multiplying by a power of two, while a right shift is equivalent to dividing by two (discarding the remainder).
Bitmask
A value combined with AND or OR to read or modify specific bits of another value. For example, `0b00001111` used with AND extracts just the lowest 4 bits.
Bit width
The number of bits used to represent a value, which determines its range. An 8-bit width covers 0–255, while a 16-bit width covers 0–65535.
Binary / hexadecimal notation
Binary notation uses only the digits 0 and 1, while hexadecimal uses 0–9 and A–F. Hex is widely used as a compact, human-readable shorthand for groups of binary digits.

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.
Tool-kun

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.