# Operate on bits

Shape / columns

A variant of [Apply a maths function](https://meridian.online/operations/apply-a-maths-function).

Apply bitwise AND, OR, XOR, NOT or shifts to integers.

Page: <https://meridian.online/operations/apply-a-maths-function/operate-on-bits>

## Status

Runs as a SQL step.

arcform carries it as a `sql:` step today.

## The DuckDB step

apply bitwise AND, OR, XOR, NOT and shifts to integers

```sql
CREATE TEMP TABLE bits AS SELECT * FROM (VALUES (1, 12, 10), (2, 255, 15), (3, NULL, 10)) t(id, a, b);
CREATE TEMP TABLE result AS
SELECT id, a & b AS bit_and, a | b AS bit_or, xor(a, b) AS bit_xor, ~a AS bit_not, a << 2 AS shifted_left, a >> 2 AS shifted_right FROM bits;
SELECT * FROM result ORDER BY id;
SELECT CASE WHEN ((SELECT [bit_and, bit_or, bit_xor, bit_not, shifted_left, shifted_right] FROM result WHERE id = 1) = [8, 14, 6, -13, 48, 3] AND (SELECT [bit_and, bit_or, bit_xor, bit_not, shifted_left, shifted_right] FROM result WHERE id = 2) = [15, 255, 240, -256, 1020, 63] AND (SELECT count(*) FROM result WHERE id = 3 AND bit_and IS NULL AND bit_not IS NULL) = 1) THEN 'as expected' ELSE error('12 and 10 did not give and 8, or 14, xor 6, not -13 and the shifts 48 and 3') END AS verdict;
```

The statement builds its own data.

It needs nothing installed beyond DuckDB itself.

## What other tools call it

Meridian's classification; an independent blind rating agreed on 95.7% of 324 rows (variant, version 3).

- Apache DataFusion: [`Bitwise Operators`](https://datafusion.apache.org/user-guide/sql/operators.html) (read 2026-09-27)
- Ibis: [`BitwiseAnd`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/numeric.py) (read 2026-09-27)
- Ibis: [`BitwiseLeftShift`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/numeric.py) (read 2026-09-27)
- Ibis: [`BitwiseNot`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/numeric.py) (read 2026-09-27)
- Ibis: [`BitwiseOr`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/numeric.py) (read 2026-09-27)
- Ibis: [`BitwiseRightShift`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/numeric.py) (read 2026-09-27)
- Ibis: [`BitwiseXor`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/numeric.py) (read 2026-09-27)
- Polars: [`SQL functions: Bitwise (group)`](https://docs.pola.rs/api/python/stable/reference/sql/functions/bitwise.html) (read 2026-09-27)
