Terican

Bitwise Calculator

Perform bitwise AND, OR, XOR, NOT, left shift, and right shift operations on 32-bit integers with binary, hex, and octal output.

FreeInstant resultsNo signup
-2,147,483,6482,147,483,647
-2,147,483,6482,147,483,647
01

Result

--

AI Explainer

0/3 free

Get a plain-English breakdown of your result with practical next steps.

Result--

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 → 1101 AND 1010 = 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 → 1101 OR 1010 = 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 → 1101 XOR 1010 = 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 → 101 becomes 101000 = 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.

Frequently Asked Questions

What is a bitwise calculator and when should it be used?
A bitwise calculator performs logical operations on individual bits of integer values. It accepts two operands as 32-bit signed integers and applies operations such as AND, OR, XOR, NOT, left shift, or right shift. Programmers use it when debugging bit manipulation code, designing network subnet masks, working with hardware registers, or verifying cryptographic routines. It is also useful for students learning binary arithmetic and computer architecture fundamentals.
What is the difference between signed right shift (>>) and unsigned right shift (>>>)?
Signed right shift (>>) preserves the sign of the number by filling the leftmost vacated bits with the original sign bit (0 for positive, 1 for negative). Unsigned right shift (>>>) always fills vacated bits with zeros, effectively treating the operand as an unsigned value. For example, -16 >> 2 produces -4, while -16 >>> 2 produces 1,073,741,820. The unsigned variant is essential when processing raw binary data where the sign bit carries no arithmetic meaning.
How does bitwise AND help calculate subnet masks in networking?
Bitwise AND determines a network address by comparing each bit of an IP address against the subnet mask. Only bit positions where both the IP address and the mask contain a 1 produce a 1 in the result. For example, applying AND between 192.168.5.130 (binary: 11000000.10101000.00000101.10000010) and the mask 255.255.255.0 (binary: 11111111.11111111.11111111.00000000) yields 192.168.5.0—the network address. Network administrators use this calculation daily for IP routing and network segmentation.
Why does bitwise NOT of a positive number return a negative result?
Bitwise NOT inverts every bit, including the sign bit, in a 32-bit two's complement representation. The mathematical relationship is NOT(x) = -(x + 1). So NOT 0 equals -1, NOT 5 equals -6, and NOT 255 equals -256. This behavior stems from two's complement encoding, where the most significant bit determines the sign. Inverting a positive number's 0 sign bit to 1 always produces a negative result, and the magnitude follows the -(x + 1) identity.
How can bitwise XOR swap two variables without a temporary variable?
The XOR swap algorithm exploits the property that a value XORed with itself equals zero, and any value XORed with zero equals itself. Given variables a = 12 and b = 7, the three-step process works as follows: (1) a = a XOR b gives a = 11, (2) b = a XOR b gives b = 12 (the original a), (3) a = a XOR b gives a = 7 (the original b). While elegant, modern compilers optimize standard swaps equally well, so XOR swapping is primarily of academic interest today.
What is the valid range for shift amounts in 32-bit bitwise shift operations?
For 32-bit integers, the valid shift amount ranges from 0 to 31 positions. Shifting by 0 returns the original value unchanged. Shifting left by n positions multiplies the value by 2 raised to the power of n—for instance, 1 << 10 equals 1,024. Shifting right by n positions divides by 2 to the power of n (with floor rounding for signed shifts). Shift amounts of 32 or greater produce undefined or implementation-dependent behavior in languages like C and C++, while JavaScript automatically masks the shift count to 5 bits (shift modulo 32).