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.
Did our AI summary help? Let us know.
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.
Ready to run the numbers?
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.
Run the calculator when you are ready.
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.
Related Calculators
OR Calculator
Calculate logical OR operations between values in binary, decimal, or hexadecimal format.
MathematicsXOR Calculator
Calculate logical XOR operations between values in binary, decimal, or hexadecimal format.
MathematicsNOR Calculator
Calculate logical NOR operations between values in binary, decimal, or hexadecimal format.
MathematicsBinary Operations Calculator
Perform binary arithmetic and logical operations with step-by-step explanations. Supports addition, subtraction, multiplication, division, and bitwise...
MathematicsOne's Complement Calculator
Calculate the one's complement of binary, decimal, or hexadecimal values with detailed explanations and visualizations.
MathematicsBitwise Calculator
Perform bitwise operations such as AND, OR, XOR, NOT, and bit shifts on binary, decimal, or hexadecimal values.
Mathematics