Bitwise AND Operation
The AND operation compares two numbers bit-by-bit, returning 1 only when both input bits are 1. It's fundamental for bit masking, subnet masks, and flag checking in computing.
Why This Mathematical Concept Matters
Why: AND is used in networking (subnet masks), file permissions (Unix rwx), and hardware design (logic gates).
How: Compare each bit position: if both inputs are 1, output 1; otherwise output 0.
- ●IP subnet masks use AND to extract network addresses from host IPs.
- ●value & (1 << n) checks if bit n is set — the fastest parity test.
- ●AND with 0 clears bits; AND with 1 preserves them.
AND Operation — Bitwise Masking & Filtering
Compare bits, extract masks, check flags. Binary, decimal, hex.
📌 Examples — Click to Load
⚠️For educational and informational purposes only. Verify with a qualified professional.
🧮 Fascinating Math Facts
Unix file permissions use AND: 0644 & 0444 checks read permission for all.
— POSIX
192.168.1.5 & 255.255.255.0 = 192.168.1.0 (network address).
— RFC 791
📋 Key Takeaways
- • AND truth table: 1 & 1 = 1; all other combinations = 0
- • Masking: Use AND with 1s to extract bits, 0s to clear them
- • Filtering: AND isolates specific bit positions (e.g., subnet masks)
💡 Did You Know?
📖 How AND Works
The bitwise AND operation compares two numbers bit by bit. For each position, the result is 1 only if both inputs are 1; otherwise it is 0. This makes AND ideal for masking (extracting or clearing specific bits) and filtering (e.g., checking flags, subnet masks).
Truth Table
| A | B | A AND B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
🎯 Expert Tips
Subnet masks
Use 255.255.255.0 (or /24) to extract network from IP. Host bits become 0.
Flag checking
value & (1 << n) checks if bit n is set. Non-zero = set.
Even/odd
n & 1 === 0 means even; n & 1 === 1 means odd. Fastest parity check.
Clear lower bits
Use mask with 1s in upper bits: 0xFFF0 clears lowest 4 bits.
⚖️ AND vs OR vs XOR
| Operation | Use Case | 1 & 1 | 1 & 0 |
|---|---|---|---|
| AND | Masking, filtering | 1 | 0 |
| OR | Setting bits | 1 | 1 |
| XOR | Toggling, parity | 0 | 1 |
❓ Frequently Asked Questions
What is the difference between logical AND (&&) and bitwise AND (&)?
Logical AND returns true/false for boolean expressions. Bitwise AND operates on each bit of two integers and returns an integer. Use && for conditions; use & for bit manipulation.
How do I check if a specific bit is set using AND?
Use value & (1 << n). If the result is non-zero, bit n is set. Example: 13 & 4 checks if bit 2 is set (13 = 1101, 4 = 0100, result = 4 = set).
How is AND used in IP subnet masking?
The subnet mask has 1s for network bits and 0s for host bits. IP & mask = network address. 192.168.1.5 & 255.255.255.0 = 192.168.1.0.
Can AND be used for even/odd detection?
Yes. n & 1 extracts the LSB. Even numbers have LSB=0, odd have LSB=1. (n & 1) === 0 means even.
What is bit masking with AND?
A mask with 1s keeps those bits, 0s clear them. 0b10111010 & 0b00001111 keeps lower 4 bits, clears upper 4.
How does AND relate to Unix file permissions?
Permissions are octal: r=4, w=2, x=1. 0644 & 0444 checks read permission for all; result 0444 means read is set.
Why use AND instead of division for bit extraction?
AND is a single CPU instruction; division is slower. AND also avoids floating-point and works on unsigned integers.
What happens with different bit lengths in AND?
Shorter values are zero-extended. 8-bit 0xFF & 16-bit 0x0F0F = 0x000F (lower byte AND).
📊 Bitwise AND by the Numbers
📚 Official Sources
⚠️ Disclaimer: This calculator is for educational and programming reference. Bitwise operations assume unsigned integers. For signed integers, behavior may differ by language. Verify results in your target environment.