& (bitwise AND)
PG 7.4+→ bit varyingPerforms bitwise AND on two bit strings of the same length.
Signature
bit varying & bit varying → bit varyingParameters
| Parameter | Type | Description |
|---|---|---|
| bits1 | bit or bit varying | First bit string |
| bits2 | bit or bit varying | Second bit string (same length) |
Examples
SELECT B'10001101' & B'01101100';00001100SELECT permissions & B'00000111' FROM roles;Keep only bits 5, 6, 7 (last three permissions)SELECT (permissions & B'00000011') = B'00000011' AS has_read_and_write FROM roles;true/false — both bits 6 and 7 must be setSELECT role_name FROM roles WHERE (permissions & B'11000000') <> B'00000000';Roles with at least one of the top two permissions setBoth operands of `&` must have identical bit lengths or PostgreSQL raises an error. A common mistake is applying an 8-bit mask to a `BIT VARYING` column that stores fewer bits, breaking the query at runtime.
✓ Instead: Cast to a fixed `BIT(n)` before applying masks, or ensure the mask literal matches the exact declared width of the column: `(perms::bit(8)) & B'00000111'`.
Apply a bitmask with `&` to check multiple permissions at once: `permissions & mask = mask` is true only if all bits in the mask are set.
SELECT role_name FROM roles WHERE (permissions & B'00000011') = B'00000011';Roles with both bit 0 and bit 1 permissions