Bit functions

**Returned value**

Returns the shifted value with type equal to that of `a`.

**Examples**

**Usage example with binary encoding**

```
SELECT 99 AS a, bin(a), bitShiftLeft(a, 2) AS a_shifted, bin(a_shifted);
```

```
┌──a─┬─bin(99)──┬─a_shifted─┬─bin(bitShiftLeft(99, 2))─┐
│ 99 │ 01100011 │       140 │ 10001100                 │
└────┴──────────┴───────────┴──────────────────────────┘
```

**Usage example with hexadecimal encoding**

```
SELECT 'abc' AS a, hex(a), bitShiftLeft(a, 4) AS a_shifted, hex(a_shifted);
```

```
┌─a───┬─hex('abc')─┬─a_shifted─┬─hex(bitShiftLeft('abc', 4))─┐
│ abc │ 616263     │ &0        │ 06162630                    │
└─────┴────────────┴───────────┴─────────────────────────────┘
```

**Usage example with Fixed String encoding**

```
SELECT toFixedString('abc', 3) AS a, hex(a), bitShiftLeft(a, 4) AS a_shifted, hex(a_shifted);
```

```
┌─a───┬─hex(toFixedString('abc', 3))─┬─a_shifted─┬─hex(bitShiftLeft(toFixedString('abc', 3), 4))─┐
│ abc │ 616263                       │ &0        │ 162630                                        │
└─────┴──────────────────────────────┴───────────┴───────────────────────────────────────────────┘
```

## bitShiftRight[​](#bitShiftRight "Direct link to bitShiftRight")

Introduced in: v1.1.0

Shifts the binary representation of a value to the right by a specified number of bit positions.

A `FixedString` or a `String` is treated as a single multibyte value.

Bits of a `FixedString` value are lost as they are shifted out.
On the contrary, a `String` value is extended with additional bytes, so no bits are lost.

**Syntax**

```
bitShiftRight(a, N)
```

**Arguments**

- `a` — A value to shift. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`String`](/docs/sql-reference/data-types/string) or [`FixedString`](/docs/sql-reference/data-types/fixedstring)
- `N` — The number of positions to shift. [`UInt8/16/32/64`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns the shifted value with type equal to that of `a`.

**Examples**

**Usage example with binary encoding**

```
SELECT 101 AS a, bin(a), bitShiftRight(a, 2) AS a_shifted, bin(a_shifted);
```

```
┌───a─┬─bin(101)─┬─a_shifted─┬─bin(bitShiftRight(101, 2))─┐
│ 101 │ 01100101 │        25 │ 00011001                   │
└─────┴──────────┴───────────┴────────────────────────────┘
```

**Usage example with hexadecimal encoding**

```
SELECT 'abc' AS a, hex(a), bitShiftLeft(a, 4) AS a_shifted, hex(a_shifted);
```

```
┌─a───┬─hex('abc')─┬─a_shifted─┬─hex(bitShiftRight('abc', 12))─┐
│ abc │ 616263     │           │ 0616                          │
└─────┴────────────┴───────────┴───────────────────────────────┘
```

**Usage example with Fixed String encoding**

```
SELECT toFixedString('abc', 3) AS a, hex(a), bitShiftRight(a, 12) AS a_shifted, hex(a_shifted);
```

```
┌─a───┬─hex(toFixedString('abc', 3))─┬─a_shifted─┬─hex(bitShiftRight(toFixedString('abc', 3), 12))─┐
│ abc │ 616263                       │           │ 000616                                          │
└─────┴──────────────────────────────┴───────────┴─────────────────────────────────────────────────┘
```

## bitSlice[​](#bitSlice "Direct link to bitSlice")

Introduced in: v22.2.0

Returns a substring starting with the bit from the 'offset' index that is 'length' bits long.

**Syntax**

```
bitSlice(s, offset[, length])
```

**Arguments**

- `s` — The String or Fixed String to slice. [`String`](/docs/sql-reference/data-types/string) or [`FixedString`](/docs/sql-reference/data-types/fixedstring)
- `offset` —
  Returns the starting bit position (1-based indexing).
- Positive values: count from the beginning of the string.
- Negative values: count from the end of the string.

  [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)
- `length` —
  Optional. The number of bits to extract.
- Positive values: extract `length` bits.
- Negative values: extract from the offset to `(string_length - |length|)`.
- Omitted: extract from offset to end of string.
- If length is not a multiple of 8, the result is padded with zeros on the right.
  [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

Returns a string containing the extracted bits, represented as a binary sequence. The result is always padded to byte boundaries (multiples of 8 bits) [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT bin('Hello'), bin(bitSlice('Hello', 1, 8));
SELECT bin('Hello'), bin(bitSlice('Hello', 1, 2));
SELECT bin('Hello'), bin(bitSlice('Hello', 1, 9));
SELECT bin('Hello'), bin(bitSlice('Hello', -4, 8));
```

```
┌─bin('Hello')─────────────────────────────┬─bin(bitSlice('Hello', 1, 8))─┐
│ 0100100001100101011011000110110001101111 │ 01001000                     │
└──────────────────────────────────────────┴──────────────────────────────┘
┌─bin('Hello')─────────────────────────────┬─bin(bitSlice('Hello', 1, 2))─┐
│ 0100100001100101011011000110110001101111 │ 01000000                     │
└──────────────────────────────────────────┴──────────────────────────────┘
┌─bin('Hello')─────────────────────────────┬─bin(bitSlice('Hello', 1, 9))─┐
│ 0100100001100101011011000110110001101111 │ 0100100000000000             │
└──────────────────────────────────────────┴──────────────────────────────┘
┌─bin('Hello')─────────────────────────────┬─bin(bitSlice('Hello', -4, 8))─┐
│ 0100100001100101011011000110110001101111 │ 11110000                      │
└──────────────────────────────────────────┴───────────────────────────────┘
```

## bitTest[​](#bitTest "Direct link to bitTest")

Introduced in: v1.1.0

Takes any number and converts it into [binary form](https://en.wikipedia.org/wiki/Binary_number), then returns the value of the bit at a specified position. Counting is done right-to-left, starting at 0.

**Syntax**

```
bitTest(a, i)
```

**Arguments**

- `a` — Number to convert. [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)
- `i` — Position of the bit to return. [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

Returns the value of the bit at position `i` in the binary representation of `a` [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT bin(2), bitTest(2, 1);
```

```
┌─bin(2)───┬─bitTest(2, 1)─┐
│ 00000010 │             1 │
└──────────┴───────────────┘
```

## bitTestAll[​](#bitTestAll "Direct link to bitTestAll")

Introduced in: v1.1.0

Returns result of the [logical conjunction](https://en.wikipedia.org/wiki/Logical_conjunction) (AND operator) of all bits at the given positions.
Counts right-to-left, starting at 0.

The logical AND between two bits is true if and only if both input bits are true.

**Syntax**

```
bitTestAll(a, index1[, index2, ... , indexN])
```

**Arguments**

- `a` — An integer value. [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint)
- `index1, ...` — One or multiple positions of bits. [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns the result of the logical conjunction [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example 1**

```
SELECT bitTestAll(43, 0, 1, 3, 5);
```

```
┌─bin(43)──┬─bitTestAll(43, 0, 1, 3, 5)─┐
│ 00101011 │                          1 │
└──────────┴────────────────────────────┘
```

**Usage example 2**

```
SELECT bitTestAll(43, 0, 1, 3, 5, 2);
```

```
┌─bin(43)──┬─bitTestAll(4⋯1, 3, 5, 2)─┐
│ 00101011 │                        0 │
└──────────┴──────────────────────────┘
```

## bitTestAny[​](#bitTestAny "Direct link to bitTestAny")

Introduced in: v1.1.0

Returns result of the [logical disjunction](https://en.wikipedia.org/wiki/Logical_disjunction) (OR operator) of all bits at the given positions in a number.
Counts right-to-left, starting at 0.

The logical OR between two bits is true if at least one of the input bits is true.

**Syntax**