Bitwise Calculator
Perform bitwise AND, OR, XOR, NOT, left shift, and right shift operations on 32-bit integers with binary, hex, and octal output.
Formula & Methodology
Understanding Bitwise Operations
Bitwise operations manipulate individual bits within binary representations of integers. Unlike arithmetic operators that work on decimal values, bitwise operators process each bit position independently, making them fundamental to low-level programming, cryptography, embedded systems, and performance optimization.
This bitwise calculator performs six core operations on 32-bit signed integers, displaying results in decimal, binary, hexadecimal, and octal formats.
Binary Number Representation
Computers store integers in binary (base-2) format. A 32-bit signed integer uses 31 bits for the magnitude and 1 bit (the most significant bit) for the sign. This two's complement representation allows a range from −2,147,483,648 to 2,147,483,647. For example, the decimal number 13 is stored as 00000000 00000000 00000000 00001101 in 32-bit binary.
Bitwise AND (&)
The AND operation compares each bit of two operands. The result bit is 1 only when both corresponding input bits are 1; otherwise, the result is 0.
- Formula: Resulti = Ai · Bi for each bit position i
- Example: 13 AND 10 →
1101AND1010=1000= 8 - Use case: Masking specific bits. To extract the lowest 8 bits of a value, apply
value AND 255(where 255 =11111111).
Bitwise OR (|)
The OR operation produces a 1 in each bit position where at least one of the corresponding input bits is 1.
- Formula: Resulti = Ai + Bi − Ai · Bi
- Example: 13 OR 10 →
1101OR1010=1111= 15 - Use case: Setting flags. To enable bit 3 in a configuration register, apply
config OR 8(where 8 =1000).
Bitwise XOR (^)
The XOR (exclusive OR) operation outputs 1 when the two input bits differ and 0 when they match. XOR has unique algebraic properties: it is both commutative and associative, and any value XORed with itself yields zero (Wikipedia – Bitwise operation).
- Formula: Resulti = Ai ⊕ Bi
- Example: 13 XOR 10 →
1101XOR1010=0111= 7 - Use case: Swapping two variables without a temporary:
a = a ^ b; b = a ^ b; a = a ^ b;
Bitwise NOT (~)
The NOT operation inverts every bit, turning each 0 into 1 and each 1 into 0. In two's complement representation, NOT x equals −(x + 1).
- Formula: Resulti = 1 − Ai
- Example: NOT 13 → NOT
00000000...00001101=11111111...11110010= −14 - Use case: Creating bitmasks. To clear bit 3, compute
value AND (NOT 8).
Left Shift (<<)
Left shift moves all bits toward the most significant position by a specified number of places, filling vacated positions with zeros. Each single-bit left shift effectively multiplies the value by 2.
- Formula: A << n = A × 2n
- Example: 5 << 3 →
101becomes101000= 40 (which equals 5 × 2³) - Use case: Fast multiplication by powers of two in performance-critical code.
Right Shift (>> and >>>)
Right shift moves bits toward the least significant position. Two variants exist, as defined by the MDN Web Docs – Bitwise Operators:
- Signed (arithmetic) right shift (>>): Fills vacated high-order bits with the original sign bit, preserving the number's sign. The formula is A >> n = ⌊A / 2n⌋.
- Unsigned (logical) right shift (>>>): Always fills vacated bits with zeros, treating the operand as an unsigned 32-bit integer.
Example: −8 >> 2 = −2 (sign preserved), while −8 >>> 2 = 1,073,741,822 (sign bit shifted as data).
Practical Applications
Bitwise operations power critical systems across computing. Network engineers use AND with subnet masks to determine network addresses—applying 192.168.1.100 AND 255.255.255.0 yields the network 192.168.1.0. Graphics programmers extract color channels from packed RGBA values using shifts and masks: (pixel >> 16) AND 0xFF isolates the red channel. Cryptographic algorithms such as SHA-256 rely heavily on XOR, AND, and rotations (UNM Computer Science – Chapter 3). Game developers use bit flags to efficiently store multiple boolean states in a single integer, reducing memory usage by up to 32× compared to individual boolean variables.