Bitwise Calculator

Perform bitwise AND, OR, XOR, NOT, NAND, and NOR operations with binary representation and step-by-step solutions.

Select Operation & Enter Values

Result

Result (Decimal)
40
170 AND 105
A (Binary) 10101010
B (Binary) 01101001
Result (Binary) 00101000
Result (Hexadecimal) 0x28
Result (Octal) 050

Step-by-Step Solution

170 AND 105 = 40

Understanding Bitwise Operations

Bitwise operations are fundamental operations in computer science that work at the individual bit level of binary numbers. Unlike arithmetic operations that treat numbers as whole values, bitwise operations process each bit independently according to a specific logical rule. These operations are among the fastest instructions a CPU can perform, making them essential for performance-critical applications.

Every integer in a computer is stored as a sequence of bits (binary digits, each being 0 or 1). Bitwise operations apply a Boolean function to corresponding pairs of bits from two operands, producing a result bit by bit.

Bitwise Operation Reference

AND (&)

Returns 1 only when both corresponding bits are 1. Used for masking and clearing bits.

1 AND 1 = 1, all others = 0

OR (|)

Returns 1 when at least one of the corresponding bits is 1. Used for setting bits.

0 OR 0 = 0, all others = 1

XOR (^)

Returns 1 when the corresponding bits are different. Used for toggling bits and encryption.

same = 0, different = 1

NOT (~)

Flips every bit: 0 becomes 1 and 1 becomes 0. Also called the bitwise complement.

NOT 0 = 1, NOT 1 = 0

NAND

NOT AND: Returns 0 only when both bits are 1. The universal gate in digital logic.

1 NAND 1 = 0, all others = 1

NOR

NOT OR: Returns 1 only when both bits are 0. Another universal gate.

0 NOR 0 = 1, all others = 0

Truth Tables

Each bitwise operation is defined by a truth table that shows the output for every possible combination of input bits. The AND operation outputs 1 only when both inputs are 1. The OR operation outputs 1 when at least one input is 1. XOR outputs 1 when exactly one input is 1 (the inputs differ).

Practical Applications

  • Bit masking: Use AND with a mask to extract specific bits from a value. For example, value & 0x0F extracts the lower nibble.
  • Setting flags: Use OR to set specific bits without affecting others. For example, flags | FLAG_ENABLED.
  • Toggling bits: Use XOR to flip specific bits. value ^ mask toggles the bits specified by the mask.
  • Checking parity: XOR all bits together to determine if a number has an even or odd number of 1-bits.
  • Swapping values: XOR can swap two variables without a temporary variable: a ^= b; b ^= a; a ^= b;
  • Encryption: XOR is the basis of many encryption algorithms because applying the same XOR key twice restores the original value.

Bitwise Operations in Programming

Most programming languages support bitwise operations using dedicated operators. In C, C++, Java, JavaScript, and Python, the operators are: & (AND), | (OR), ^ (XOR), ~ (NOT). Understanding these operations is crucial for systems programming, embedded development, network protocols, and any situation requiring bit-level data manipulation.

Digital Logic and Hardware

Bitwise operations directly correspond to logic gates in digital circuits. AND, OR, and NOT gates are the fundamental building blocks of all digital hardware. Notably, NAND and NOR are called "universal gates" because any other logic function can be built using only NAND gates or only NOR gates. This makes them particularly important in chip design and manufacturing.

Tips for Working with Bitwise Operations

  • Always consider the bit width of your data type to avoid unexpected results.
  • Use hexadecimal notation for masks and constants -- it maps directly to groups of 4 bits.
  • Remember that NOT in two's complement: ~x = -(x + 1).
  • XOR with all 1s is equivalent to NOT.
  • AND with all 1s returns the original value (identity).
  • OR with all 0s returns the original value (identity).