Perform AND (&), OR (|), XOR (^), NOT (~), left shift (<<), right shift (>>) on any integer — live binary visualization, truth table, decimal/hex/octal output, bit-masking guide, and code examples for C, Python, Java & JavaScript.
Perform bitwise operations on integers and visualize the binary representation.
| Operation | Symbol | Binary Result (32-bit) | Decimal Result | Description |
|---|
Bitwise operations work on individual bits of binary representations. They are used in low-level programming, cryptography, optimization, and hardware manipulation.
x << 1 Multiply by 2x >> 1 Divide by 2x & 1 Check if oddx ^ x Always 0x & (x-1) Clear rightmost bitThis bitwise calculator performs all six fundamental bitwise operations — AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>) — on any integer with live binary visualization, decimal, hexadecimal, and octal output, a bit-by-bit truth table, and ready-to-use code examples for C, Python, Java, and JavaScript. Whether you are learning binary arithmetic, masking hardware registers, implementing permission flags, or debugging low-level code, this tool shows exactly which bits are set and what each operation produces at every bit position — instantly.
Bitwise operations manipulate the individual binary digits (bits) of integers directly — treating each bit position independently rather than the number as a whole. They are among the fastest single-instruction operations any CPU can execute, and they form the backbone of low-level programming, embedded systems, network protocols, graphics, cryptography, and compression. Unlike arithmetic operations that carry values between bit positions, bitwise operations are purely positional and require no carry propagation.
| Bit A | Bit B | A AND B (&) | A OR B (|) | A XOR B (^) | NOT A (~A) | NOT B (~B) |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 |
| Operator | Symbol | Rule | Primary Use Case | Two-Operand? |
|---|---|---|---|---|
| AND | & | 1 only if BOTH bits = 1 | Test / read / mask specific bits | Yes |
| OR | | | 1 if EITHER bit = 1 | Set specific bits to 1 | Yes |
| XOR | ^ | 1 only if bits DIFFER | Toggle bits, parity, swap, cipher | Yes |
| NOT | ~ | Flip every bit (1→0, 0→1) | Invert; ~x = -(x+1) in two's complement | No (unary) |
| Left Shift | << | Shift bits left, fill right with 0 | Multiply by 2^n; build masks | Yes |
| Right Shift | >> | Shift bits right, discard low bits | Integer divide by 2^n; extract fields | Yes |
Most modern computers represent negative integers using two's complement encoding. To negate any integer: flip all bits (bitwise NOT), then add 1. The key rule for bitwise operations is ~x = -(x+1) for any signed integer. This means ~0 = -1, ~5 = -6, ~-1 = 0. Understanding this is essential when performing NOT operations or right-shifting signed negative numbers.
| Decimal | 8-bit Unsigned Binary | 8-bit Signed (Two's Complement) | NOT Result (~) |
|---|---|---|---|
| 0 | 00000000 | 0 | -1 (11111111) |
| 1 | 00000001 | 1 | -2 (11111110) |
| 5 | 00000101 | 5 | -6 (11111010) |
| 127 | 01111111 | 127 (max signed) | -128 (10000000) |
| 128 | 10000000 | -128 | 127 (01111111) |
| 255 | 11111111 | -1 | 0 (00000000) |
| Task | Code (C/Java/JS) | How It Works |
|---|---|---|
| Test if even | x & 1 == 0 | Bit 0 is 0 for all even numbers |
| Test if power of 2 | x > 0 && (x & (x-1)) == 0 | Powers of 2 have exactly one set bit |
| Multiply by 2^n | x << n | Left shift = fast multiply by power of 2 |
| Divide by 2^n | x >> n | Right shift = floor division by power of 2 |
| Swap two vars | a^=b; b^=a; a^=b | XOR is its own inverse: a^b^b = a |
| Set bit n | x | (1 << n) | OR with single-bit mask sets that bit |
| Clear bit n | x & ~(1 << n) | AND with inverted mask clears that bit |
| Toggle bit n | x ^ (1 << n) | XOR flips the target bit each time |
| Get lowest set bit | x & (-x) | -x = ~x+1; AND isolates lowest 1 bit |
| Clear lowest set bit | x & (x-1) | Subtracting 1 flips bits up to lowest 1 |
| Count set bits (popcount) | Integer.bitCount(x) / bin(x).count('1') | Brian Kernighan: loop x &= x-1 |
| Round up to power of 2 | 1 << (int)(Math.log(x)/Math.log(2)+1) | Find next power of 2 via shift |
| Language | AND | OR | XOR | NOT | Left Shift | Right Shift | Unsigned Right |
|---|---|---|---|---|---|---|---|
| C / C++ | & | | | ^ | ~ | << | >> (impl-def signed) | cast to unsigned |
| Java | & | | | ^ | ~ | << | >> (arithmetic) | >>> |
| JavaScript | & | | | ^ | ~ | << | >> (arithmetic) | >>> |
| Python | & | | | ^ | ~ | << | >> (arithmetic) | N/A (arbitrary prec.) |
| C# | & | | | ^ | ~ | << | >> (arithmetic) | >>> (C# 11+) |
| Rust | & | | | ^ | ! | << | >> (type-dependent) | u-type >> |
Unix file permissions (rwxrwxrwx = 9 bits), Linux capability flags, and database ACLs all store multiple boolean permissions in a single integer. Each flag occupies one bit position, allowing fast OR to grant, AND NOT to revoke, and AND to test permissions in one CPU instruction.
Microcontroller datasheets describe peripheral control registers as collections of named bits. Drivers read with AND, set with OR, and clear with AND NOT to configure timers, UART baud rates, GPIO directions, and interrupt enables without side-effects on neighbouring bits.
IP subnet masking extracts the network address with AND: host_ip & subnet_mask. IPv4 header parsing uses shifts and masks to extract version (bits 28-31), IHL (bits 24-27), and DSCP (bits 18-23) fields from packed 32-bit words.
A packed ARGB colour is a 32-bit integer. Channels are extracted with right shifts and AND masks: alpha = (color >> 24) & 0xFF; red = (color >> 16) & 0xFF; green = (color >> 8) & 0xFF; blue = color & 0xFF. Reassembly uses left shifts and OR.
XOR is the workhorse of stream ciphers — plaintext XOR keystream = ciphertext; ciphertext XOR keystream = plaintext. SHA-256, MD5, and most block ciphers (AES, DES) use AND, OR, XOR, and bit rotations as their primary non-linear mixing operations.
5 & 3 = 1. In binary: 0101 & 0011 = 0001. Only bit position 0 is set in both numbers, so the result is 1.
Bitwise NOT flips every bit. In two's complement the rule is ~x = -(x+1). So ~5 = -(5+1) = -6. This is because flipping all bits then adding 1 gives the negative, so flipping all bits gives the negative minus 1.
Bitwise AND (&) operates on every bit pair and returns an integer. Logical AND (&&) treats each operand as true/false, short-circuits if the first is false, and returns a boolean. Use & for bit manipulation and && for conditional logic.
XOR is self-inverse (x^y^y = x), making it ideal for: toggling specific bits, swapping two variables without a temporary variable, simple stream ciphers, parity bits in error detection, and hash mixing in MD5/SHA/MurmurHash.
A mask is an integer where specific bits are 1 and the rest are 0 (or vice versa). AND with a mask reads those bits. OR with a mask sets those bits. XOR with a mask toggles those bits. AND with the inverted mask clears those bits.