Bitwise Calculator

Perform AND, OR, XOR, NOT, and left/right shift operations on two integers, and see the result in decimal, binary, and hexadecimal for an 8, 16, or 32-bit width.

Quick Facts

Bitwise basics
AND, OR, XOR, NOT, and shifts act on each bit independently
Negative numbers use two's complement within the chosen bit width; shift amounts are taken modulo the width.

Your Results

Calculated
Result (decimal)
-
Signed value at the chosen bit width
Result (binary)
-
Two's complement bit pattern
Result (hexadecimal)
-
Base-16 representation
Set bits (popcount)
-
Count of 1 bits in the result

Ready

Enter A, B, choose an operation and bit width, then press Calculate.

Understanding the Bitwise Calculator

This tool performs bitwise operations — AND, OR, XOR, NOT, and left/right shifts — directly on the binary (base-2) representation of two integers. Every operation acts independently on each pair of corresponding bits, which is what makes bitwise logic fast and predictable in low-level programming, digital electronics, networking, and cryptography.

The formulas

For two bits x and y, the core operations follow simple truth tables, applied at every bit position:

  • AND (A & B): 1 only if both bits are 1; otherwise 0. Used to mask or clear bits.
  • OR (A | B): 1 if either bit (or both) is 1; otherwise 0. Used to set bits.
  • XOR (A ^ B): 1 if the bits differ, 0 if they match. Used to toggle bits and build simple checksums.
  • NOT (~A): flips every bit of A — 0 becomes 1 and 1 becomes 0.

Left shift (A << B) moves every bit of A left by B positions and fills the vacated low bits with zero — equivalent to multiplying A by 2B, before any overflow is truncated to the chosen width. Right shift (A >> B) moves every bit right by B positions; this calculator uses an arithmetic shift, which fills the vacated high bits with copies of the sign bit, equivalent to dividing A by 2B and rounding toward negative infinity.

Two's complement and bit width

Negative integers are stored using two's complement: to negate a number, invert every bit and add 1. The bit width you choose (8, 16, or 32) sets how many bits are available and therefore the representable range — an 8-bit signed value runs from -128 to 127, for example. Any input outside that range wraps around modulo 2width before the operation runs, exactly as it would in a fixed-width hardware register or a typed variable such as int8_t or int16_t. Shift amounts are likewise taken modulo the bit width.

Common sources of error

  • Sign confusion: the same 8-bit pattern 11111000 is -8 read as signed two's complement, or 248 read as unsigned.
  • Wrong bit width: operating at the wrong width truncates bits you meant to keep, or wraps a value you expected to fit.
  • Logical vs. arithmetic shift: an arithmetic right shift preserves the sign of a negative number; a logical (unsigned) right shift instead fills with zeros and gives a different, positive result.

Checking your result

Some quick sanity checks: AND can only turn bits off (its result is a subset of both inputs' set bits), OR can only turn bits on (its result is a superset of both inputs' set bits), and XOR-ing any value with itself always yields zero. A left shift by B is the same as multiplying by 2B as long as no bits are truncated by the chosen width — handy for verifying small examples by hand.

Applications

Bitwise operations underpin flag and permission systems (each bit is an independent on/off setting), fast integer arithmetic (shifting instead of multiplying or dividing by powers of two), checksums and parity bits (XOR), color and pixel manipulation, and low-level networking, where a subnet mask is applied to an IP address with a plain AND.

Frequently Asked Questions

What is the difference between AND, OR, and XOR?
AND returns 1 only when both bits are 1, so it is used to mask or clear specific bits. OR returns 1 when either bit is 1, so it is used to set bits. XOR returns 1 only when the two bits differ, which makes it useful for toggling bits and simple checksums, since XOR-ing a value with itself always yields zero.
What is two's complement and why does it matter here?
Two's complement is the standard way computers represent negative integers: to get the negative of x, invert every bit of x and add 1. It lets addition, subtraction, AND, OR, and XOR all work correctly on signed numbers using the same circuitry as unsigned numbers. This calculator's decimal result reflects the two's complement interpretation for the bit width you choose.
Why does changing the bit width change the result?
The bit width sets how many bits are kept. Inputs are first wrapped to fit that width (modulo 2 to the power of the width), and the operation's output is truncated to the same width. A value like 200 fits in 16 or 32 bits unchanged, but wraps to -56 in 8 bits, so the same operation can produce different results at different widths.
What does a left shift or right shift do?
A left shift moves every bit left by the given number of places and fills the emptied low bits with zero, which multiplies the value by 2 raised to that power (subject to truncation at the chosen width). A right shift moves bits right by that many places; this calculator performs an arithmetic shift, copying the sign bit into the emptied high bits, equivalent to dividing by the same power of 2 and rounding toward negative infinity.