Bitwise Mask Calculator
Our free logic & computer science calculator solves bitwise mask problems. Get worked examples, visual aids, and downloadable results.
Reviewed for accuracy by Manoj Kumar, Mathematics Educator
Bitwise Mask 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 only if both 1 | OR: 1 if either 1 | XOR: 1 if different
Worked example โ AND = 0 (network bits extracted) | OR = 192
Formula
AND: 1 only if both 1 | OR: 1 if either 1 | XOR: 1 if different
Bitwise operations compare corresponding bit positions of two operands. AND produces 1 only when both bits are 1. OR produces 1 when at least one bit is 1. XOR produces 1 when the bits differ. NOT inverts all bits. Shifts move bits left or right by specified positions.
Worked Examples
Example 1: Subnet Mask Operation
Problem:Apply a subnet mask 255.255.255.0 (last octet = 00000000) to IP last octet 192 (11000000). What is the network portion?
Solution:Value A = 192 = 11000000 Mask B = 0 = 00000000 AND: 11000000 AND 00000000 = 00000000 (0) OR: 11000000 OR 00000000 = 11000000 (192) This shows the AND operation extracts the network bits (all masked out in the last octet).
Result:AND = 0 (network bits extracted) | OR = 192
Example 2: Permission Flag Manipulation
Problem:Set bit 2 and bit 4 in the value 10100001 (161), then check if bit 5 is set.
Solution:Value A = 161 = 10100001 Set mask for bits 2,4: 00010100 = 20 OR to set: 10100001 OR 00010100 = 10110101 (181) Check bit 5 mask: 00100000 = 32 AND to check: 10110101 AND 00100000 = 00100000 (32, non-zero = bit is set)
Result:After setting: 181 (10110101) | Bit 5 is set
Frequently Asked Questions
What are bitwise operations and why are they important?
Bitwise operations manipulate individual bits within binary numbers. The fundamental operations are AND, OR, XOR, NOT, and bit shifts. They are critically important in computing because they execute in a single CPU clock cycle, making them the fastest possible operations. Bitwise operations are used in low-level programming for hardware control, device drivers, network protocols, graphics rendering, and cryptography. They enable compact storage of multiple boolean flags in a single integer, efficient permission systems (like Unix file permissions), and fast mathematical shortcuts like multiplying by powers of 2 using left shifts. Understanding bitwise operations is essential for systems programming and performance optimization.
How does the bitwise AND operation work?
The bitwise AND operation compares each pair of corresponding bits from two numbers. The result bit is 1 only if both input bits are 1; otherwise, it is 0. For example, 1010 AND 1100 = 1000. Think of AND as a filter or mask: it extracts specific bits from a value. If you AND a number with 00001111, you extract only the lower 4 bits (nibble). AND is used extensively in networking for subnet masking (IP AND subnet mask = network address), in graphics for color channel extraction, and in embedded systems for reading specific hardware register bits. The truth table is: 0 AND 0 = 0, 0 AND 1 = 0, 1 AND 0 = 0, 1 AND 1 = 1.
How does the bitwise OR operation work?
The bitwise OR operation compares each pair of bits and produces 1 if either or both input bits are 1. The result is 0 only when both bits are 0. For example, 1010 OR 1100 = 1110. OR is used to set specific bits without affecting others. If you OR a value with 00001000, you set bit 3 to 1 regardless of its current state, while leaving all other bits unchanged. This is commonly used in configuration registers where each bit controls a different feature. In permission systems, OR combines individual permission flags: READ OR WRITE OR EXECUTE. The truth table is: 0 OR 0 = 0, 0 OR 1 = 1, 1 OR 0 = 1, 1 OR 1 = 1.
What is XOR and what are its unique properties?
XOR (exclusive OR) produces 1 when the input bits are different and 0 when they are the same. Its truth table: 0 XOR 0 = 0, 0 XOR 1 = 1, 1 XOR 0 = 1, 1 XOR 1 = 0. XOR has remarkable properties: it is its own inverse (a XOR b XOR b = a), which makes it essential in cryptography for encryption and decryption with the same key. XOR can swap two values without a temporary variable: a = a XOR b, b = a XOR b, a = a XOR b. It is used in RAID storage for parity calculation, in checksums and error detection codes, and in generating pseudo-random numbers. XOR also toggles specific bits: XORing with a mask flips the masked bits.
What are bit masks and how are they used in practice?
A bit mask is a binary pattern used with bitwise operations to manipulate specific bits within a value. Common mask operations include: setting bits (value OR mask), clearing bits (value AND NOT mask), toggling bits (value XOR mask), and checking bits (value AND mask). In practice, Unix permissions use a 9-bit mask (rwxrwxrwx). Network subnet masks like 255.255.255.0 (11111111.11111111.11111111.00000000) separate network and host portions of IP addresses. Game engines use bitmasks for collision layers and entity component flags. Graphics programming uses masks for alpha blending and color channels. Embedded systems use register masks to control hardware features individually.
How do left shift and right shift operations work?
Left shift (symbol usually written as two less-than signs) moves all bits to the left by a specified number of positions, filling vacated positions with zeros. Each left shift by 1 doubles the value, so left shifting by n positions multiplies by 2 to the power of n. For example, 00001010 left shift 2 = 00101000 (10 becomes 40). Right shift moves bits to the right, effectively dividing by powers of 2. Logical right shift fills with zeros, while arithmetic right shift preserves the sign bit for signed numbers. Shift operations are faster than multiplication and division instructions on most processors, making them common optimization techniques. They are used in hash functions, pixel manipulation, and protocol parsing.
What is the difference between NAND, NOR, and XNOR operations?
NAND, NOR, and XNOR are the negated versions of AND, OR, and XOR respectively. NAND (NOT AND) produces 0 only when both inputs are 1; it is the most important gate in digital electronics because any logic function can be built using only NAND gates. NOR (NOT OR) produces 1 only when both inputs are 0; like NAND, it is also a universal gate. XNOR (NOT XOR) produces 1 when both inputs are the same and 0 when they differ, making it a bit-level equality comparator. In hardware design, NAND and NOR gates are preferred because they are cheaper and faster to manufacture than AND and OR gates. XNOR is used in comparators and error-checking circuits.
How are bitwise operations used in network programming?
Network programming relies heavily on bitwise operations. Subnet masking uses AND to determine if two IP addresses are on the same network: IP AND SubnetMask = NetworkAddress. CIDR notation like /24 means the first 24 bits are the network portion. IP header parsing extracts fields using masks and shifts: the version field occupies the upper 4 bits of the first byte, extracted by right-shifting by 4. TCP flags (SYN, ACK, FIN, RST) are individual bits in the flags field, checked using AND with specific masks. Checksum calculations often use XOR for error detection. MAC address processing, VLAN tagging, and QoS markings all rely on bitwise operations to pack and unpack data efficiently within protocol headers.
How do contiguous bit masks relate to subnet masks?
A contiguous bit mask has all its 1-bits adjacent with no gaps, like 11110000 or 11111100. Subnet masks in networking must always be contiguous: they start with a block of 1s followed by a block of 0s. For example, 255.255.240.0 in binary is twenty 1s followed by twelve 0s, written as /20 in CIDR notation. Non-contiguous subnet masks (like 10101010) are invalid because they would create ambiguous network boundaries. Checking whether a mask is contiguous is done by verifying that the value plus the complement of the value plus one equals a power of 2. Contiguous masks are also used in memory alignment, cache line masking, and address decoding in hardware design. Tools that validate subnet masks always check for contiguity.
References
Reviewed for accuracy by Manoj Kumar, Mathematics Educator ยท Editorial policy
Related Calculators
๐งฎBitwise Calculator
Calculate bitwise 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.