Skip to main content

Bitwise Calculator

Calculate bitwise instantly with our math tool. Shows detailed work, formulas used, and multiple solution methods. See charts, tables, and visual results.

Share this calculator

Formula

AND: 1&1=1 | OR: 1|0=1 | XOR: 1^0=1 | NOT: ~0=1

Bitwise operations apply logical functions to each pair of corresponding bits independently. AND produces 1 only when both bits are 1. OR produces 1 when at least one bit is 1. XOR produces 1 when bits are different. NOT flips every bit.

Worked Examples

Example 1: Bitwise AND for Subnet Masking

Problem: Apply subnet mask 255.255.255.0 to IP address 192.168.5.137 using bitwise AND.

Solution: IP: 11000000.10101000.00000101.10001001 (192.168.5.137)\nMask: 11111111.11111111.11111111.00000000 (255.255.255.0)\nAND: 11000000.10101000.00000101.00000000 (192.168.5.0)\n\nNetwork address: 192.168.5.0\nHost part: 137 (from IP AND NOT mask)\nBroadcast: 192.168.5.255 (network OR NOT mask)

Result: Network: 192.168.5.0 | Host: 137 | Broadcast: 192.168.5.255

Example 2: XOR for Simple Encryption

Problem: Encrypt the value 170 (binary 10101010) using XOR with key 85 (binary 01010101), then decrypt.

Solution: Original: 10101010 (170)\nKey: 01010101 (85)\nEncrypted: 11111111 (255) [170 XOR 85]\n\nDecrypt: 11111111 XOR 01010101 = 10101010 (170)\nThe original value is restored by XOR-ing with the same key.\n\nHamming distance between 170 and 85: 8 (all bits differ)

Result: Encrypted: 255 | Decrypted: 170 | Hamming Distance: 8

Frequently Asked Questions

What are bitwise operations and how do they work at the hardware level?

Bitwise operations manipulate individual bits within binary numbers, performing logical operations on each pair of corresponding bits independently. At the hardware level, these operations are implemented using logic gates: AND gates, OR gates, XOR gates, and NOT gates (inverters). When you compute A AND B, each bit position is processed by a separate AND gate simultaneously, making bitwise operations among the fastest instructions a CPU can execute (typically one clock cycle). A modern 64-bit CPU processes all 64 bits in parallel. These operations form the foundation of all digital computing, from simple arithmetic to complex encryption algorithms. Understanding bitwise operations gives insight into how computers actually process information at the most fundamental level.

How does the bitwise AND operation work and what is it used for?

Bitwise AND compares each bit pair and produces 1 only when BOTH bits are 1; otherwise it produces 0. The truth table is: 0 AND 0 = 0, 0 AND 1 = 0, 1 AND 0 = 0, 1 AND 1 = 1. The most common use of AND is bit masking, where you use a mask to extract specific bits from a value. For example, to check if bit 3 is set: value AND 8 (binary 1000). If the result is non-zero, bit 3 is set. AND is also used to clear specific bits: value AND (NOT mask) zeros out the bits specified by the mask while preserving all others. In networking, subnet masks use AND to separate network and host portions of IP addresses. These bit manipulation techniques are fundamental to systems programming.

What is the bitwise OR operation and when should you use it?

Bitwise OR compares each bit pair and produces 1 when EITHER or BOTH bits are 1; it produces 0 only when both bits are 0. The truth table is: 0 OR 0 = 0, 0 OR 1 = 1, 1 OR 0 = 1, 1 OR 1 = 1. OR is primarily used to SET specific bits without affecting others. For example, to turn on bit 5: value OR 32 (binary 100000). This guarantees bit 5 will be 1 while leaving all other bits unchanged. OR is widely used in flag-based systems where multiple boolean options are packed into a single integer. For instance, file permissions in Unix use OR to combine read (4), write (2), and execute (1) flags: rwx = 4 OR 2 OR 1 = 7. Graphics programming uses OR to combine color channels and overlay bitmap images.

How does XOR work and why is it special among bitwise operations?

XOR (exclusive OR) produces 1 when the bits are DIFFERENT and 0 when they are the SAME. The truth table is: 0 XOR 0 = 0, 0 XOR 1 = 1, 1 XOR 0 = 1, 1 XOR 1 = 0. XOR is unique because it is its own inverse: A XOR B XOR B = A, meaning XOR-ing with the same value twice restores the original. This property makes XOR essential in cryptography for simple encryption/decryption, in RAID storage systems for parity calculation, and in error-detecting codes. XOR can toggle specific bits without knowing their current state: value XOR mask flips the bits in the mask positions. The classic programming trick of swapping two variables without a temporary uses XOR: a ^= b; b ^= a; a ^= b.

What is the bitwise NOT operation and how does it relate to two's complement?

Bitwise NOT (complement) flips every bit: 0 becomes 1 and 1 becomes 0. For an n-bit unsigned number, NOT x = 2^n - 1 - x. In two's complement signed representation, NOT x = -(x + 1), which is a key identity. For example, NOT 0 = -1 (all bits become 1), NOT 5 = -6, and NOT -1 = 0. This relationship is used to compute two's complement negation: -x = NOT x + 1. NOT is used in combination with AND to clear bits (AND NOT mask), and with other operators to create NAND, NOR, and XNOR operations. In hardware, the NOT gate (inverter) is the simplest logic gate and is fundamental to the construction of all other gates. Understanding NOT is essential for working with complement arithmetic and signed number representations.

How are bitwise operations used in permission and flag systems?

Flag systems pack multiple boolean values into a single integer, using each bit as a separate on/off switch. Common examples include Unix file permissions (read=4, write=2, execute=1), window style flags in GUI programming, and feature flags in configuration systems. To check a flag: (value AND flag) != 0. To set a flag: value = value OR flag. To clear a flag: value = value AND (NOT flag). To toggle a flag: value = value XOR flag. This approach is memory-efficient (32 flags in one integer versus 32 separate booleans) and fast (all flag operations are single CPU instructions). Database systems often use bit flags for storing multiple boolean attributes in a single column, significantly reducing storage requirements.

References