Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Bitwise operations

Битовые операции

Bitwise operators work on the individual bits of an integer. They can compare matching positions, flip bits, or shift them, making it possible to store and update several on/off settings in one value.

Also known as: XOR, битовая маска

What bitwise operations do

Think of an integer as a row of switches. In binary, 13 is 1101: bits 0, 2, and 3 are set, counting from the right and starting at zero. Bitwise operators let you work with those positions directly. Unlike ordinary addition, a bitwise AND, OR, or XOR never carries a result into the next position.

The main operators

Compare 1101 and 1011 one column at a time:

  • AND (&) keeps a 1 only where both inputs have a 1: 1001.
  • OR (|) keeps a 1 where either input has a 1, including where both do: 1111.
  • XOR (^, exclusive OR) produces a 1 where the inputs differ: 0110.
  • NOT (~) takes one input and flips its bits. If you look at just four bits, 1101 flips to 0010.
  • Left shift (<<) moves bits toward higher positions; right shift (>>) moves them toward lower ones. For example, 0b1101 >> 1 is 0b110: the rightmost bit is discarded. For negative numbers and fixed-width types, check your language's shift rules.

XOR is an operator. A bitmask, by contrast, is a value whose set bits mark the positions you want an operation to act on.

Example: a compact set of options

Assign each option its own bit. In Python, 1 << 2 places a single 1 at position 2, giving 0b0100. That value can serve as the mask for snapping objects to a grid:

SHOW_GRID = 1 << 0
SNAP_TO_GRID = 1 << 2
options = SHOW_GRID | SNAP_TO_GRID  # 0b0101

snapping = (options & SNAP_TO_GRID) != 0  # True
options &= ~SHOW_GRID                     # 0b0100: clear bit 0
options ^= SNAP_TO_GRID                   # 0b0000: toggle bit 2

With a mask that selects several bits, (options & mask) != 0 asks whether any selected option is on. Use (options & mask) == mask to check whether all of them are on.

Where masks help and where they trip you up

Bitmasks are useful when the layout already matters, such as in a device register or binary file format. They also let an algorithm encode a small subset as one integer. For everyday application state, separate Boolean fields may be easier to read unless you need the compact representation.

Don't substitute bitwise AND for logical AND: Python's and tests truthiness and can skip evaluating the right-hand expression, whereas & on integers combines their bits. Also, the number of displayed digits is not the integer's width. In Python, ~13 is -14; to keep only the low four bits of the result, use (~13) & 0b1111, which gives 2.

How this comes up in interviews

A common question is how to tell whether an integer is a power of two. For a positive n, subtracting 1 changes the lowest set bit to 0 and turns the zeros below it into ones. ANDing with the original value therefore removes just that lowest set bit. The test n > 0 and (n & (n - 1)) == 0 checks that there was exactly one set bit. Explain the positive-number guard: without it, zero would pass the bitwise test too.