Binary & Bitwise Calculator Online – AND, OR, XOR, NOT, Shift, Rotate | 8/16/32/64-bit

Perform AND, OR, XOR, NOT, NAND, NOR, XNOR, bit shift, and bit rotate operations on 8, 16, 32, or 64-bit integers. Input in decimal, hex (0x), binary (0b), or octal (0o). Results shown as signed/unsigned decimal, hex, octal, and binary with color-coded bit visualization and popcount stats. Runs locally in your browser.

What is a Binary / Bitwise Calculator?

A binary calculator performs arithmetic and logic operations directly on the individual bits of integer values, rather than on their decimal representations. Where a normal calculator computes 12 + 7 = 19, a bitwise calculator computes 0b1100 AND 0b0111 = 0b0100 — operating on the binary bit pattern itself. This is how CPUs, microcontrollers, DSPs, and GPUs actually manipulate data at the hardware level: each instruction such as AND, OR, XOR, and shift operates on every bit of an integer in a single clock cycle, making bitwise operations the most efficient computation a processor can perform.

Every bitwise operation — AND, OR, XOR, NOT, NAND, NOR, XNOR, as well as bit shifts and circular rotations — is a fundamental building block of digital logic, embedded firmware, cryptography, network protocol parsing, and systems programming. This tool lets you apply any of these 11 operations to 8-bit, 16-bit, 32-bit, or 64-bit integers, with input accepted in decimal (including signed negatives), hexadecimal (0x), binary (0b), or octal (0o) notation. Results appear simultaneously in all four bases alongside a color-coded bit visualization, signed and unsigned decimal interpretations, and bit statistics (popcount, leading zeros, trailing zeros). Everything runs in WebAssembly in your browser — no data leaves your machine.

Bitwise AND — Masking and Bit Extraction

The AND operation compares two bit patterns position by position and produces a 1 only when both input bits are 1. In C the operator is &. AND is the workhorse of bit masking: applying AND with a mask whose 1 bits mark the fields of interest zeroes out every other bit, isolating exactly the information you need. It is the standard way to read GPIO pin states, extract protocol fields from packed registers, align addresses to boundaries, and test whether a flag bit is set.

How to Use AND Online

  1. Select AND from the operation buttons.
  2. Enter Operand A — the value you want to mask (e.g. a register snapshot: 0x12AB).
  3. Enter Operand B — the mask (e.g. 0xFF00 to keep the high byte).
  4. Read the result in hex, decimal (signed and unsigned), octal, and binary.
  5. Click any result card to copy that representation to the clipboard.

Example — Extract high byte from a 16-bit register:

Operand A: 0x12AB = 0001 0010 1010 1011

Operand B: 0xFF00 = 1111 1111 0000 0000

Result: 0x1200 = 0001 0010 0000 0000

The lower 8 bits are zeroed out; the upper 8 bits are preserved unchanged. To extract just the high byte value, shift right by 8: 0x1200 >> 8 = 0x12.

Bitwise OR — Setting Bits and Combining Flags

The OR operation produces a 1 bit if at least one of the two input bits is 1. In C the operator is |. OR is used to set specific bits without affecting others: ORing a register with a mask forces every bit that is 1 in the mask to become 1 in the result, while bits that are 0 in the mask pass through unchanged. This is the standard idiom for enabling hardware features, setting interrupt enable bits, and combining multiple option flags into a single integer.

Example — Enable bits 0, 3, and 7 in a control register:

Register: 0b01000100 (current value with some bits already set)

Mask: 0b10001001 = (1<<7) | (1<<3) | (1<<0)

Result: 0b11001101 — bits 0, 3, 7 now set; bits 2, 6 preserved

In C: reg |= (1 << 7) | (1 << 3) | (1 << 0);

Bitwise XOR — Toggling Bits and Encryption

XOR (exclusive OR) produces 1 when the two input bits differ, and 0 when they are equal. In C the operator is ^. XOR has a unique self-inverse property — applying XOR twice with the same key restores the original value — making it the foundation of symmetric encryption, RAID parity computation, and checksum algorithms. XOR is also the standard idiom for toggling a bit: XOR a register with a mask to flip only the bits that are 1 in the mask.

Example 1 — Toggle an LED output bit (bit 4):

GPIO register: 0b00110101

Toggle mask: 0b00010000 = (1 << 4)

After toggle: 0b00100101 — bit 4 flipped, all others unchanged

Example 2 — XOR swap without a temporary variable:

a ^= b; b ^= a; a ^= b; — swaps a and b using only XOR

Example 3 — RAID-5 parity reconstruction:

parity = disk0 XOR disk1 XOR disk2 (if disk1 fails: disk1 = parity XOR disk0 XOR disk2)

Bitwise NOT — Complement and Mask Inversion

NOT is a unary operation that inverts every bit of its operand — each 0 becomes 1 and each 1 becomes 0. In C the operator is ~. The result is the one's complement of the input. Important: the result depends on the selected bit width. NOT of 0x0F in 8-bit mode gives 0xF0 (240), but in 32-bit mode gives 0xFFFFFFF0 (4294967280). NOT is most commonly used to invert a mask so you can clear specific bits: to clear bit N, use reg &= ~(1 << N), which ANDs with a mask that has 0 only at position N and 1 everywhere else.

Example — Generate a bit-clear mask for bit 5 in 8-bit mode:

1 << 5 = 0b00100000

NOT(0b00100000) = 0b11011111 (the clear mask)

reg = 0b11110111 AND 0b11011111 = 0b11010111 — bit 5 cleared, all others preserved

In C: reg &= ~(1 << 5);

Bit Shifts and Rotations

Shift and rotate operations move bits laterally across the integer. Shifts are used for fast multiplication and division by powers of two, byte extraction, and bit-field construction. Rotations are used in cryptographic algorithms where it is essential that no bits are discarded.

Shift Left (<<)

Shift Left moves all bits toward the most significant position by N places, filling vacated low bits with 0. Shifting left by N multiplies the value by 2^N (for unsigned values within range). For example: 0b00000011 << 2 = 0b00001100 (3 × 4 = 12). The N most significant bits are lost. Use Shift Left to position a single-bit mask: 1 << N creates a mask with only bit N set.

Shift Right (>>)

Shift Right moves all bits toward the least significant position by N places. This calculator uses logical (unsigned) shift right — vacated high bits are always filled with 0, equivalent to integer division by 2^N for unsigned values. For example: 0b00110000 >> 3 = 0b00000110 (48 / 8 = 6). The N least significant bits are lost. An arithmetic shift right would instead fill with the sign bit (preserving sign for negative two's complement values) — in C this behavior is implementation-defined for signed integers.

Rotate Left / Rotate Right (ROL / ROR)

Rotation (circular shift) is like shift, but bits that exit one end re-enter the other — no bits are discarded. ROL by 1 in 8-bit: the MSB (bit 7) wraps around to bit 0. ROR is the inverse. For example: ROL(0b10110001, 1) = 0b01100011 in 8-bit. Rotations are the primitive that makes many cryptographic algorithms secure: SHA-256 uses three right-rotations per round; AES SubBytes uses an 8-bit ROL; RC5 round function is entirely rotations and XOR. Modern CPUs implement ROL/ROR as a single instruction that is significantly faster than the equivalent (a << n) | (a >> (width - n)) in software.

Popcount, Leading Zeros, and Trailing Zeros

This calculator shows three bit-statistics for every result, each of which corresponds to a native CPU instruction in modern architectures.

Popcount (Hamming Weight)

Popcount is the count of 1 bits in the result. For example, popcount(0b10110101) = 5. CPUs expose this as a POPCNT instruction; compilers surface it as __builtin_popcount(x) in GCC/Clang or std::popcount(x) in C++20. Applications: computing Hamming distance for error detection (distance = popcount(a XOR b)), balanced key checks in cryptography, counting active set bits in RTOS task-ready bitmasks, and database bitmap index cardinality queries.

Leading Zeros (CLZ)

Leading zeros (CLZ — Count Leading Zeros) is the number of 0 bits above the most significant 1. For a power-of-two value 2^k, CLZ equals (width - 1 - k), so CLZ computes integer log2 in O(1). CPUs expose this as BSR (bit-scan reverse) on x86, CLZ on ARM, or __builtin_clz(x) in GCC. Applications: fast next-power-of-two computation (1 << (32 - clz(n - 1))), variable-length encoding, normalizing floating-point mantissas.

Trailing Zeros (CTZ)

Trailing zeros (CTZ — Count Trailing Zeros) is the number of 0 bits below the least significant 1. For a non-zero integer, CTZ gives the position of the lowest set bit and the 2-adic valuation (how many times 2 divides the number). CPUs expose this as BSF on x86, RBIT+CLZ on ARM, or __builtin_ctz(x). Applications: extracting the lowest set bit in a priority-queue bitmask, stripping trailing zeros, and iterating over set bits in a bitmask one at a time.

Practical example — find position of highest-priority pending task:

Task-ready bitmask: 0b00101000 (tasks 3 and 5 are ready)

CTZ = 3 (bit 3 is the lowest set bit → task 3 has highest priority)

CLZ = 2 (bits 7, 6 are zero above bit 5 → 32-bit CLZ would be 26)

Popcount = 2 (two tasks are ready)

Operations Quick Reference

All operations use the selected bit width. Results are masked to fit the chosen width.

OperationSymbol (C)DescriptionCommon UseExample (8-bit)
AND&1 if both bits 1Bit masking, field extraction0xF0 & 0x3C = 0x30
OR|1 if either bit 1Set bits, combine flags0xF0 | 0x0F = 0xFF
XOR^1 if bits differToggle bits, parity, encryption0xAA ^ 0xFF = 0x55
NOT~Invert all bits (unary)Complement, clear-mask generation~0x0F = 0xF0
NAND~(a&b)NOT of ANDUniversal gate, CMOS logic~(0xF0&0xFF)=0x0F
NOR~(a|b)NOT of ORUniversal gate, logic synthesis~(0xF0|0x0F)=0x00
XNOR~(a^b)1 if bits equalEquality detector, LFSR~(0xAA^0xAA)=0xFF
SHLa << nShift bits toward MSBMultiply by 2^n, bit positioning0x01 << 4 = 0x10
SHRa >> nShift bits toward LSBDivide by 2^n, extract high byte0x80 >> 3 = 0x10
ROLrotl(a,n)Circular shift left (MSB wraps)Cryptography (AES, SHA)ROL(0x81,1)=0x03
RORrotr(a,n)Circular shift right (LSB wraps)Cryptography (SHA-256, MD5)ROR(0x81,1)=0xC0

Two's Complement and Signed Arithmetic

Two's complement is the universal standard for representing signed integers in binary, used by every modern CPU. The most significant bit is the sign bit — when it is 1, the value is negative. To negate a two's complement number: invert all bits (bitwise NOT) and add 1. For example, 3 in 8-bit is 0b00000011; inverting gives 0b11111100; adding 1 gives 0b11111101, which is -3. The key advantage of two's complement over one's complement is that there is exactly one representation of zero and that unsigned addition hardware also works correctly for signed addition.

8-bit two's complement — signed and unsigned interpretations:

  • 0xFF → unsigned: 255 | signed: -1
  • 0x80 → unsigned: 128 | signed: -128 (minimum signed 8-bit value)
  • 0x7F → unsigned: 127 | signed: +127 (maximum signed 8-bit value)
  • 0x00 → unsigned: 0 | signed: 0
  • 0xFE → unsigned: 254 | signed: -2

32-bit signed range: -2,147,483,648 (0x80000000) to +2,147,483,647 (0x7FFFFFFF)

64-bit signed range: -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

Practical Use Cases

Bitwise operations appear in nearly every layer of software. Here are the most common domains where this calculator is useful:

  • Embedded register manipulation: Microcontroller peripheral registers (GPIO, UART, SPI, I2C) are configured by setting and clearing individual bits. Use AND to clear bits, OR to set bits, XOR to toggle bits, and AND-NOT to generate clear masks.
  • Network packet parsing: IP headers, Ethernet frames, and protocol PDUs pack multiple fields into single 16-bit or 32-bit words. AND with a field mask and shift right to extract each field; shift left and OR to construct headers.
  • Cryptographic algorithms: AES, SHA-256, SHA-3, MD5, Salsa20, and ChaCha20 are all built from AND, OR, XOR, NOT, and rotation primitives. SHA-256 uses 64 rounds each containing two ROL/ROR combinations per word.
  • Graphics and color channel extraction: RGBA colors are often packed as 0xAARRGGBB. Extract red: (color >> 16) & 0xFF. Extract alpha: color >> 24. Compose: (a << 24) | (r << 16) | (g << 8) | b.
  • Game state and option flags: A single integer can represent 32 or 64 boolean game-state flags (player abilities, inventory items, difficulty options) using one bit each. OR to grant a flag, AND-NOT to revoke it, AND to test it, XOR to toggle it.
  • Compression algorithms: Huffman coding, LZ77, DEFLATE, and Brotli all operate at the bit level — reading and writing variable-length codes by building up integers with shifts and OR, then extracting codes with AND and shift.
  • Memory address alignment: To align a pointer down to the nearest N-byte boundary (where N is a power of two): addr & ~(N - 1). To check alignment: addr & (N - 1) == 0.

Frequently Asked Questions

How do I clear bit N in a register?

Use the AND NOT pattern: reg &= ~(1 << N). To compute the clear mask in this tool: select NOT, enter 0x01 << N as Operand A to get the inverted mask, then use that mask in an AND operation with the register value. For example, to clear bit 5 of 0xFF: NOT(0x20) = 0xDF in 8-bit mode; then 0xFF AND 0xDF = 0xDF.

How do I set bit N in a register?

Use the OR pattern: reg |= (1 << N). Select OR in this tool, enter the register value as Operand A and 1 << N (a power of two) as Operand B — for example, Operand B = 0x08 to set bit 3 (2^3 = 8). The result has bit N forced to 1 with all other bits unchanged.

How do I toggle bit N?

Use the XOR pattern: reg ^= (1 << N). Select XOR, enter the current register value as Operand A and the single-bit mask as Operand B. XOR with 1 flips the bit; XOR with 0 leaves it unchanged — so only the bits set in the mask are toggled.

How do I check if bit N is set?

Use AND and check if the result is non-zero: if (reg & (1 << N)) { /* bit N is set */ }. In this tool, select AND, enter the register value as Operand A and the single-bit mask as Operand B. If the result is non-zero, the bit was set. The unsigned decimal result card will show the bit's positional value (a power of two) if set, or 0 if clear.

Why does the bit width matter for NOT?

NOT inverts all bits in the selected width. NOT(0x0F) in 8-bit gives 0xF0 (one byte complemented), but in 32-bit gives 0xFFFFFFF0 (four bytes complemented). The high 24 bits are 0xFF each. If you are debugging a 16-bit peripheral register, always select 16-bit; if you compute NOT in 32-bit, you will see a different result than your microcontroller will produce.

What is the fastest way to compute log2 of a power-of-two?

Count trailing zeros (CTZ): for a power-of-two value 2^k, the trailing zeros count is exactly k, so log2(value) = CTZ(value). This is a single instruction on modern CPUs. For example, CTZ(64) = CTZ(0b01000000) = 6, so log2(64) = 6. This calculator shows CTZ for every result in the Bit Layout card.

How is XOR used in cryptography?

XOR is the fundamental mixing operation in symmetric cryptography because it is its own inverse (so decryption is identical to encryption) and it is a bijection (every input maps to a unique output). Stream ciphers produce a pseudo-random keystream and XOR it byte-by-byte with plaintext; the same operation with the same keystream recovers plaintext. RAID-5 uses XOR to compute a parity strip so any single drive can be reconstructed. AES, SHA-256, and ChaCha20 all mix state words with XOR at every round.

Why is NAND called a universal gate?

NAND is called universal (or functionally complete) because every Boolean logic function — AND, OR, NOT, XOR, and any combination — can be constructed using only NAND gates. NOT(a) = NAND(a,a). AND(a,b) = NAND(NAND(a,b), NAND(a,b)). OR(a,b) = NAND(NAND(a,a), NAND(b,b)). This matters in digital circuit design because CMOS NAND gates use fewer transistors than AND gates, so the entire logic of a chip can be optimized using a single gate type.

What is the difference between ROL and Shift Left?

Shift Left discards the bits that overflow the top — in 8-bit mode, shifting 0b11000000 left by 1 gives 0b10000000 (the high bit is lost). Rotate Left wraps those bits around: rotating 0b11000000 left by 1 gives 0b10000001 (the high bit reappears at bit 0). Rotation is lossless and reversible; shifting is destructive. Rotate Right by (width - N) is equivalent to Rotate Left by N, so the two directions are symmetric.

Related Tools

  • 3D Rotation Calculator - Free 3D rotation calculator online. Compose and chain multiple rotations — Euler angles, quaternions, axis-angle, and rotation matrices — and see the compound result in all four formats with live 3D visualization. Runs locally in your browser.
  • Endian Converter - Free online endian converter and byte swap calculator. Convert big endian to little endian with memory layout visualization, C bswap code generator, IEEE 754 float byte order, and htonl/ntohl reference for embedded and network developers
  • Unit Converter - Convert metric and imperial units for length, mass, temperature, pressure, speed, area, volume, data storage, time, energy, and more. Handy for engineering, travel, and homework—runs locally.
  • 3D Rotation Visualizer - Visualize 3D rotations using Quaternions, Euler Angles, and Rotation Matrices with interactive 3D visualization
  • IEEE 754 Converter - Convert between decimal, hexadecimal, and IEEE 754 binary bit patterns for 32-bit float and 64-bit double. Visualize sign, exponent, and mantissa fields. Debug firmware register dumps, NaN/Inf values, and floating-point encoding.

Operation

Operands

Accepts: decimal (e.g. 255), 0xFF hex, 0b11111111 binary, 0o377 octal, -1 signed
Result (32-bit)
Enter operand(s) above to compute the bitwise result
Need to set, clear, or test specific bits with a visual grid? Try the Bit Mask Calculator for a click-to-toggle bit grid with C macro generation.