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.