Bitwise Calculator - AND, OR, XOR, NOT, and Truth Table

Use this bitwise calculator to apply AND, OR, XOR, NAND, NOR, XNOR, or NOT to two integers in 4 to 32-bit registers with binary, octal, hex, popcount.

Bitwise Calculator

Unsigned integer in the chosen bit width.

Ignored when the operation is NOT.

Boolean rule applied column by column.

Register width used to render binary and to truncate operands.

Results

Result (Decimal)
0
Result (Binary) 0
Result (Octal) 0
Result (Hex) 0
Popcount (1-Bits) 0bits
A in Binary 0
B in Binary 0

What Is a Bitwise Calculator?

A bitwise calculator applies boolean AND, OR, XOR, NAND, NOR, XNOR, or NOT to two integers column by column inside a fixed-width register and shows the result in decimal, binary, octal, and hexadecimal, the fastest way to verify what a low-level flag, mask, or toggle is doing.

  • Debug flag bits in microcontroller registers: Mask a status register with AND, isolate one bit with AND against a power of two, or clear a flag with AND against the bitwise NOT of a mask.
  • Encode permissions in a single integer: Use OR to set a bit, AND with NOT to clear a bit, and XOR to toggle a bit when storing read, write, and execute flags in one byte.
  • Convert color channels in image and UI code: Mask the red, green, blue, and alpha channels out of a packed 32-bit color with AND against 0xFF, 0xFF00, 0xFF0000, and 0xFF000000.

Most programming languages expose the same six operators: & (AND), | (OR), ^ (XOR), ~ (NOT), and the two compound shifts that are usually discussed alongside them. The bitwise calculator is the scratchpad you reach for when an expression like (flags & READ) == READ is hard to read in your head or in code review.

When a mask itself is built from a single power of two, the bit shift calculator builds the same constant in a fixed-width register without doing the multiplication in your head.

How the Bitwise Calculator Works

The calculator writes A and B as n-bit binary strings, lines up the bits, and applies the chosen boolean rule column by column. NOT only needs the first operand and flips every bit in the chosen bit width.

c[i] = a[i] op b[i] (op ∈ {AND, OR, XOR, NAND, NOR, XNOR}) c[i] = 1 - a[i] for NOT
  • a: first operand, treated as an unsigned n-bit integer
  • b: second operand, ignored when the operation is NOT
  • op: boolean rule: AND, OR, XOR, NAND, NOR, XNOR, or NOT
  • n: bit width, from 4 to 32, that fixes the register size

The result panel reports the same number in decimal, binary, octal, and hexadecimal so you can paste the value into the language or debugger you are using, and the popcount row gives the number of 1-bits in the result for the flag-mask checks that need it most.

AND of 0xAA and 0xF0 in 8 bits

A = 170 (10101010), B = 240 (11110000), op = AND, n = 8

AND keeps a 1 only where both bits are 1: 1&1=1, 1&0=0, 0&1=0, 0&0=0. Column by column that gives 10100000.

Result = 0xA0 = 160, popcount = 2.

Use this to mask the low four bits of a status byte; the high nibble comes through, the low nibble is forced to zero.

XOR of 0xAA and 0x55 in 8 bits

A = 170 (10101010), B = 85 (01010101), op = XOR, n = 8

XOR keeps a 1 only where the bits differ. Every column disagrees, so the result is 11111111.

Result = 0xFF = 255, popcount = 8.

XOR with the all-ones bit pattern is the bitwise way to assert that two values are exact complements.

According to Wikipedia, a bitwise operation acts on one or two bit patterns of equal length by applying a boolean operator column by column, with the same truth table used for the per-bit AND, OR, and XOR operators that are built into C, C++, Java, JavaScript, and Python.

According to MDN Web Docs, JavaScript's bitwise operators first coerce their operands to 32-bit signed integers using two's complement, which is why a bitwise NOT in JavaScript is the same as negating the value and subtracting one.

When the same two operands also need an add, subtract, multiply, or divide alongside the bitwise rule, the binary operations calculator runs both arithmetic and bitwise operations from the same form.

Key Concepts Explained

Four ideas that come up every time you work with bitwise operators.

Boolean truth tables

Each of the six two-operand bitwise rules is a per-column truth table: AND is 1 only when both inputs are 1, OR is 1 when at least one input is 1, XOR is 1 when the inputs differ, and NAND, NOR, and XNOR are the inverted versions of the same three. Once you can read the truth table, every register, mask, and check suddenly makes sense.

Bitmask and bit fields

A bitmask is a binary number that picks out the bits you care about. AND against a mask keeps the selected bits and clears the rest, OR against a mask sets the selected bits, and XOR against a mask toggles them. A power-of-two mask is the most common shape in real code and is usually built with a single shift.

Two's complement representation

Signed integers in nearly every language are stored in two's complement, where the top bit is the sign and the bit pattern of a negative value is the bitwise NOT of the positive value plus one. The same AND, OR, and XOR work on signed values, but NOT has to be interpreted inside a fixed bit width to mean what you expect.

Popcount (Hamming weight)

The popcount of a bit pattern is the number of 1-bits it contains. It is also called the Hamming weight and is the standard way to count selected flags, measure Hamming distance, or run a quick sanity check that a mask is the shape you intended.

The four concept cards above are the foundation for everything else. The same vocabulary is used by every language specification, so you can read the operator docs for C, Java, Python, JavaScript, and Rust without translating the names again.

If you only need to see a single number in another base, the binary converter does that without forcing you to pick an operation first.

How to Use This Bitwise Calculator

Pick the operation, set the two operands, choose a bit width, and read the result in the format you need.

  1. 1 Pick an operation: Choose AND, OR, XOR, NAND, NOR, XNOR, or NOT from the dropdown. NOT ignores operand B.
  2. 2 Enter the first operand: Type the unsigned integer for A. The binary row below the inputs shows the bit pattern you typed, padded to the bit width.
  3. 3 Enter the second operand: Type the unsigned integer for B. Two's complement, signed, or negative values are out of scope for this tool.
  4. 4 Choose a bit width: Pick 4, 8, 16, or 32 bits. The width fixes how NOT behaves and how both operands are truncated if they are too wide.
  5. 5 Read the result panel: The result row shows decimal, binary, octal, hex, and popcount at the same time, so you can copy the value directly into the language you are writing.
  6. 6 Reset and try the next case: Hit Reset to restore the default 170 AND 240 example and try a new rule without retyping the operands.

To check whether the third bit of a status register is set, AND the register against 0b00001000 (8 in decimal) and look at the popcount. The same register can be OR'ed with 0b00001000 to set the bit, and XOR'ed with 0b00001000 to toggle it.

If your workflow needs the column-by-column carry rule as well as the bitwise rules, the binary addition calculator shows the addition step by step in binary.

Benefits of Using This Bitwise Calculator

Six practical reasons to verify a bitwise expression with a calculator instead of trusting the compiler or your mental model.

  • Catch off-by-one bit errors before runtime: Mask width, sign extension, and shift counts are the three most common sources of one-bit bugs. Reading the result in binary next to the operands makes the wrong bit obvious.
  • See all four number bases at once: Decimal, binary, octal, and hex are all visible in the same row, which removes the back-and-forth between a debugger and a hex converter.
  • Match the register width of your target: 4, 8, 16, and 32 bit widths let the calculator behave like the actual hardware register, including the way NOT truncates in 8 bits.
  • Learn the operator truth tables by doing: The default example is 0xAA AND 0xF0, which is the textbook case for AND masking and a good warm-up for NAND, NOR, and XNOR.
  • Validate hand-written masks: Engineers who write permission masks or packed struct layouts can paste a candidate mask into B and use AND or XOR against a known value to confirm the shape.
  • Compare bitwise rules side by side: Switching between AND, OR, and XOR with the same operands shows the same column-by-column pattern in three different output rows.

Used in a code review, the calculator is a quick way to show a reviewer that a clever bit-trick is doing exactly what the commit message claims, without having to drop into a debugger to print the intermediate values.

Factors That Affect Your Results

Four things that change what a bitwise operation returns, and two limits of this calculator worth knowing before you paste a result into production code.

Choice of bit width

Changing the bit width is the most common reason a bitwise result looks wrong. NOT in 8 bits flips only the low 8 bits, while NOT in 32 bits flips 32 bits, so the two answers can differ by a large factor.

Truncation of wide operands

Any operand larger than 2^n - 1 is silently truncated to the low n bits. This matches how most CPUs behave but can hide a bug if the high bits were supposed to mean something.

Operator precedence with shifts and arithmetic

In C, Java, and JavaScript the bitwise operators are lower precedence than shifts and arithmetic, so a & b == c is parsed as a & (b == c). This calculator does not apply any precedence.

Signed versus unsigned inputs

The calculator only accepts unsigned inputs from 0 to 2^n - 1. If you need to AND, OR, or XOR two's complement values, use the same operands as their unsigned low n bits.

  • This tool is for plain integer bitwise operations. Floating-point bit tricks, IEEE 754 sign and mantissa extraction, and bitwise operations on big integers larger than 32 bits are not supported.
  • The calculator returns the result in the chosen bit width, so a NOT inside an 8-bit register can never return a value outside 0..255. If you need the language's actual signed 32-bit behaviour, paste the operands into a real language REPL rather than trust the bit width of this tool.

Both limitations are about scope, not correctness: the bitwise rules are stable across every modern language, so once you have a verified result you can paste it into the target code with confidence.

According to Python documentation, the bitwise operations ~, &, |, and ^ assume an infinite-width two's complement representation, which is why -1 has an unlimited run of 1-bits under ~ and why the result is sign-extended before being returned.

For a real-world example of how the same AND and shift rules extract color channels from a packed 32-bit integer, the hex to RGB calculator walks through a 0xRRGGBB value end to end.

bitwise calculator showing AND, OR, XOR, and NOT applied to two 8-bit integers with binary, hex, octal, and popcount output
bitwise calculator showing AND, OR, XOR, and NOT applied to two 8-bit integers with binary, hex, octal, and popcount output

Frequently Asked Questions

Q: What is a bitwise calculator used for?

A: A bitwise calculator applies AND, OR, XOR, NAND, NOR, XNOR, or NOT to two integers column by column inside a fixed-width register and shows the result in decimal, binary, octal, and hex so you can verify a flag mask, packed permission, or hardware register value before you paste it into code.

Q: What is the difference between bitwise AND, OR, and XOR?

A: AND keeps a 1 only where both bits are 1 and is used to mask bits. OR keeps a 1 wherever at least one bit is 1 and is used to set bits. XOR keeps a 1 only where the bits differ and is used to toggle bits or compare two values. The three truth tables are the foundation of every bitwise expression in every language.

Q: How do you compute a bitwise AND by hand?

A: Write A and B in binary, line up the bits so the rightmost columns match, and apply the AND truth table column by column: 1 AND 1 is 1, every other combination is 0. Read the result from left to right to get the binary answer, then convert it back to decimal or hex if needed.

Q: What does a bitwise NOT do to a number?

A: A bitwise NOT flips every bit of its operand inside the chosen bit width. In an 8-bit register, NOT 0xAA is 0x55 because 10101010 becomes 01010101. In a 32-bit register the same expression flips 32 bits, which is why the result can look very different in different languages.

Q: What is a bitmask and how is it used with bitwise operators?

A: A bitmask is a binary number that picks out the bits you care about. AND against a mask keeps only the selected bits and clears the rest, OR against a mask sets the selected bits, and XOR against a mask toggles them. A common pattern is to AND a status byte against 0b10000000 to read just the top flag.

Q: Are bitwise operators the same as logical operators in code?

A: No. Bitwise operators (&, |, ^, ~) act on every bit of an integer, while logical operators (&&, ||, !) act on the truthiness of a whole value and short-circuit. The two pairs are easy to mix up in a code review, which is one of the main reasons a bitwise calculator is useful.