Bitwise Calculator
Calculate bitwise instantly with our math tool. Shows detailed work, formulas used, and multiple solution methods. See charts, tables, and visual results.
Reviewed for accuracy by Manoj Kumar, Mathematics Educator
Bitwise Calculator
Calculator
Adjust values & calculateEnter your values below. Every result is computed in your browser — no data is sent to any server.
Formula: AND: 1&1=1 | OR: 1|0=1 | XOR: 1^0=1 | NOT: ~0=1
Worked example — Network: 192.168.5.0 | Host: 137 | Broadcast: 192.168.5.255
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) Mask: 11111111.11111111.11111111.00000000 (255.255.255.0) AND: 11000000.10101000.00000101.00000000 (192.168.5.0) Network address: 192.168.5.0 Host part: 137 (from IP AND NOT mask) Broadcast: 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) Key: 01010101 (85) Encrypted: 11111111 (255) [170 XOR 85] Decrypt: 11111111 XOR 01010101 = 10101010 (170) The original value is restored by XOR-ing with the same key. Hamming 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.
What is the Hamming distance and how is it calculated using XOR?
The Hamming distance between two binary values is the number of bit positions where the corresponding bits differ. It is calculated by XOR-ing the two values and counting the number of 1 bits (population count) in the result. For example, the Hamming distance between 10110 and 10001 is: 10110 XOR 10001 = 00111, which has three 1 bits, so the distance is 3. Hamming distance is crucial in error detection and correction codes: a code with minimum Hamming distance d can detect up to d-1 errors and correct up to floor((d-1)/2) errors. In information theory, it measures the similarity between binary strings. Telecommunications systems use Hamming distance to design codes that can survive noisy channels.
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.
What are NAND and NOR gates and why are they called universal gates?
NAND (NOT AND) and NOR (NOT OR) are called universal gates because any other logic function can be built using only NAND gates or only NOR gates. NAND produces 0 only when both inputs are 1; otherwise it produces 1 (the complement of AND). NOR produces 1 only when both inputs are 0 (the complement of OR). To build NOT from NAND: connect both inputs together. To build AND: NAND followed by NOT (another NAND). To build OR: NOT each input, then NAND. In real-world chip manufacturing, NAND gates are particularly favored because they are simpler and cheaper to fabricate in CMOS technology. Flash memory is named NAND flash because its storage cells are arranged in a NAND gate configuration.
How do bitwise operations apply to network address calculations?
In networking, bitwise AND is the fundamental operation for subnet calculations. An IP address AND subnet mask gives the network address. For example, IP 192.168.1.100 (11000000.10101000.00000001.01100100) AND mask 255.255.255.0 (11111111.11111111.11111111.00000000) gives network 192.168.1.0. The host part is extracted using AND with the inverted mask: IP AND (NOT mask). Broadcast address is network OR (NOT mask). To check if two hosts are on the same subnet: (IP1 AND mask) == (IP2 AND mask). CIDR notation /24 means 24 leading 1 bits in the mask. These bitwise calculations are performed millions of times per second by network routers to make forwarding decisions, making efficient bitwise operations critical to internet infrastructure.
What is the population count (popcount) and how is it computed efficiently?
Population count (popcount), also called the Hamming weight, counts the number of 1 bits in a binary number. The naive approach checks each bit individually in a loop, taking O(n) time for n bits. A faster approach uses the Brian Kernighan trick: repeatedly clear the lowest set bit with n = n AND (n-1) and count iterations, running in O(k) time where k is the number of set bits. Modern CPUs provide hardware POPCNT instructions that compute this in one cycle. Popcount has applications in chess programming (counting pieces on a bitboard), database systems (counting set flags), cryptography (calculating bit distances), and computational biology (comparing genetic sequences). Many modern hash functions and bloom filters rely on efficient popcount operations.
References
Reviewed for accuracy by Manoj Kumar, Mathematics Educator · Editorial policy
Related Calculators
🧮Bitwise Mask Calculator
Calculate bitwise mask with inputs, formulas, and instant results.
🧮Annulus Area Calculator
Calculate annulus area with inputs, formulas, and instant results.
🧮Area Calculator
Calculate area with inputs, formulas, and instant results.
🧮Area of a Rectangle Calculator
Calculate the area, perimeter, and diagonal of a rectangle. Find missing sides from known area. Convert between metric and imperial area units.
🧮Area of Crescent Calculator
Calculate area of crescent with inputs, formulas, and instant results.
🧮Center of Mass Calculator
Calculate center of mass with inputs, formulas, and instant results.
🧮Centroid Calculator
Calculate centroid with inputs, formulas, and instant results.
🧮Chord Length Calculator
Calculate chord length with inputs, formulas, and instant results.