Bitwise Calculator

AND, OR, XOR, NOT, NAND, NOR, XNOR on decimal, hex or binary operands.

Apply AND, OR, XOR, NOT or a bit shift to integer operands.

Inputs

What it does

Applies AND, OR, XOR, NOT, NAND, NOR and XNOR bit by bit to two numbers written in decimal, hex or binary.

Where it's used

Setting, clearing and testing register bits in firmware, masks and flags.

How to use it

  1. Type operand A (decimal, 0x hex or 0b binary).
  2. Choose the operation.
  3. Type operand B (not needed for NOT).
  4. Choose the word size.

The formula explained

Set bit n
x | (1 ≪ n)
Clear bit n
x & ~(1 ≪ n)
Toggle bit n
x ^ (1 ≪ n)
Test bit n
(x ≫ n) & 1

Worked example

Inverting the bits of 0x3C by XOR with 0xFF.

Inputs:

The calculator gives:

Tips and common mistakes

Questions people ask

How do I set, clear, toggle or test one bit?

Set: x |= 1 << n. Clear: x &= ~(1 << n). Toggle: x ^= 1 << n. Test: (x >> n) & 1. These are the everyday tools for microcontroller registers.

Why does 0xC3 show as both 195 and −61?

Unsigned, 0xC3 is 195. As a signed 8-bit number the top bit means −128, so it is 195 − 256 = −61. The bits are the same; only the interpretation changes.

How do I pull a field out of a register?

Shift it down, then mask it: (x >> shift) & mask. The upper four bits of 0xC3 are (0xC3 >> 4) & 0xF = 0xC.

Related Digital Logic calculators

Enter your values to get the result with the formula, substituted values and an explanation of each step.

All engineering calculators