Bitwise Calculator performs bitwise operations on integers: AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). Enter two numbers (in decimal, binary, hex, or octal), select an operation, and see the result along with binary visualization of each bit. It handles signed and unsigned 8-bit, 16-bit, 32-bit, and 64-bit integers.
Bitwise operations work directly on the binary representation of numbers, one bit at a time. AND returns 1 only when both bits are 1. OR returns 1 when either bit is 1. XOR (exclusive OR) returns 1 when exactly one bit is 1. NOT flips all bits. Left shift multiplies by powers of 2 (n << 1 = nร2). Right shift divides by powers of 2 (n >> 1 = nรท2). These operations are fundamental to systems programming, hardware interfacing, and performance-critical code.
Common uses: checking if a number is odd (n & 1 == 1), toggling specific bits in a bitmask, extracting bit fields from packed data (network protocol headers, pixel data), flag systems (Unix file permissions, CSS bitmasks), and hash function implementations. Bitwise operations are O(1) and typically execute in a single CPU cycle โ much faster than multiplication or division.
Check if 42 is even
Result: 42 & 1 = 0 (binary: 101010 & 000001 = 000000) โ even
Set bit 3 of 0b00001010
Result: 0b00001010 | 0b00001000 = 0b00011010 (10 | 8 = 18)
Extract red channel from RGB
Result: 0xFF5733 >> 16 = 0xFF (255) โ shift right 16 bits to get red channel
What is a bitmask?
A bitmask is an integer used with bitwise AND to select specific bits. Flags (0001, 0010, 0100, 1000) are set by OR-ing them together (0101 = flags 1 and 3 are set). A bit is checked by AND-ing with its flag (n & 0b0100 != 0 means flag 3 is set). Unix file permissions (rwx = 4+2+1) are a classic bitmask example.
What is two's complement?
Two's complement is the standard way to represent negative integers in binary. To negate n: flip all bits (NOT) then add 1. So -1 in 8-bit is 11111111 (all ones). -128 is 10000000. The range of a signed 8-bit integer is -128 to 127. Two's complement lets the same addition circuits handle both positive and negative numbers โ no special hardware needed.
What is arithmetic vs logical right shift?
Logical right shift (>>>) fills the vacated bits with 0. Arithmetic right shift (>>) fills vacated bits with the sign bit (0 for positive, 1 for negative) โ preserving the sign. In JavaScript, >> is arithmetic and >>> is logical. In C, the behavior is implementation-defined for signed integers. For positive numbers they are identical.
Why is XOR used in cryptography?
XOR has a useful property: A ^ B ^ B = A. This means XOR-ing with a key masks the data, and XOR-ing again with the same key unmasks it. A one-time pad (perfectly secret encryption) works by XOR-ing plaintext with a random key of the same length. XOR is also used in parity checks, checksums, Feistel networks (DES, AES rounds), and hash functions.
How do bit shifts relate to multiplication?
Left shift by n bits multiplies by 2^n: x << 3 = x ร 8. Right shift by n bits divides by 2^n (integer division): x >> 2 = x รท 4. Compilers use this optimization automatically for power-of-2 constants. For other multipliers, the compiler may combine shifts and additions: x * 6 = (x << 2) + (x << 1) = x*4 + x*2.