← New search

Other meanings of Bitwise AND

COMPUTING

Bitwise AND

Bitwise AND is a binary operation that compares corresponding bits and returns 1 only when both are 1. Applied to fixed-width integers, it produces a result whose bit is set exactly where the same bit is set in both operands; it is written as & in many programming languages and is distinct from logical conjunction, commonly written && or and.1

2 inputs
operands
binary operation
1 only if both bits are 1
truth condition
per bit
&
common operator
source notation
1

Definition and truth table

Bitwise AND operates independently on each corresponding pair of bits. For each position, the output is 1 only when the left operand and the right operand both contain 1; every other pairing produces 0.1 The operation therefore follows a four-row truth table:

Left bitRight bitResult
000
010
100
111

For example, 1101 AND 1011 is 1001: only the first and fourth aligned positions contain two 1s. Integer values are normally represented with a defined or implementation-dependent width, so leading zeroes are conceptually included when operands are aligned. The operation is commutative and associative, and it distributes over bitwise OR, which makes it useful for algebraic manipulation of masks.

2

Masks and practical programming

Bit masks are the central practical use of Bitwise AND. A mask selects particular positions by placing 1s where information should be retained and 0s elsewhere; ANDing a value with that mask clears all unselected bits.2 For instance, value & 00001111 keeps the low four bits, while value & 00000100 isolates bit 2. A nonzero result from the latter test means that bit is set.

This pattern appears in permission flags, device registers, packed structures, pixel channels, protocol headers, and feature options. To clear selected bits, software often ANDs with the complement of a mask, as in value & ~mask; the exact result depends on the integer width and language rules. In C and related languages, the operator has lower precedence than equality comparisons, so parentheses such as (flags & mask) != 0 make intent clear.3

3

Language behavior and distinctions

Programming languages agree on the per-bit rule but differ in operand types, integer widths, and treatment of arbitrary-precision values. C defines & as a bitwise AND operator for integer types, with the usual integer conversions applied before evaluation.3 Java provides the operation for integral types and also uses & for boolean operands, where it evaluates both operands rather than short-circuiting; && is the short-circuit logical operator.4

Python applies & to integers as an operation on an unbounded two's-complement-style representation and also defines it for sets, where it means intersection.5 JavaScript applies bitwise AND after converting numeric operands to signed 32-bit integers, while BigInt operands use arbitrary precision and cannot be mixed with ordinary Numbers.6 These differences matter when shifting, negating masks, or exchanging binary data between systems.

4

Lesser-known aspects

Bitwise AND is also a building block for low-level data interpretation and cryptography-related techniques, although the operation itself does not provide secrecy. A network address can be derived by ANDing an IP address with a subnet mask, separating the network prefix from the host portion; this is a basic operation in IPv4 routing and address configuration.7

In image and signal work, AND can combine binary masks to retain pixels or samples satisfying multiple conditions. In hardware, an AND gate implements the one-bit truth table, and wider bitwise AND operations are formed from parallel gates. The operation is idempotent: x & x equals x. It also has an absorbing element, because x & 0 is always zero, and an identity element, because x & all-ones preserves x at the chosen width. These properties support compiler simplification, digital-circuit design, and formal reasoning about masks.

5

Sources

References for language semantics and networking usage appear below.

Glossary

Bit mask
A value whose 1 and 0 positions select, preserve, test, or clear corresponding bits in another value.
Bitwise OR
A binary operation that returns 1 when either corresponding input bit is 1.
Logical conjunction
A truth-valued operation that combines propositions or boolean expressions, usually with short-circuit rules in programming languages.
Two's complement
A common representation for signed binary integers in which negative values are formed by complementing and adding one.
Subnet mask
An IPv4 bit mask used to identify the network portion and host portion of an address.

Examples use binary notation for clarity; actual integer width, signed representation, and operator behavior depend on the programming language and operand types.