2020-04-03 13:23:32 +00:00
---
toc_priority: 48
toc_title: Bit
---
2020-04-30 18:19:18 +00:00
# Bit Functions {#bit-functions}
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
Bit functions work for any pair of types from UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64, Float32, or Float64.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
The result type is an integer with bits equal to the maximum bits of its arguments. If at least one of the arguments is signed, the result is a signed number. If an argument is a floating-point number, it is cast to Int64.
2017-04-03 19:49:50 +00:00
2020-03-20 10:10:48 +00:00
## bitAnd(a, b) {#bitanda-b}
2017-04-03 19:49:50 +00:00
2020-03-20 10:10:48 +00:00
## bitOr(a, b) {#bitora-b}
2017-04-03 19:49:50 +00:00
2020-03-20 10:10:48 +00:00
## bitXor(a, b) {#bitxora-b}
2017-04-03 19:49:50 +00:00
2020-03-20 10:10:48 +00:00
## bitNot(a) {#bitnota}
2017-04-03 19:49:50 +00:00
2020-03-20 10:10:48 +00:00
## bitShiftLeft(a, b) {#bitshiftlefta-b}
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
## bitShiftRight(a, b) {#bitshiftrighta-b}
2018-04-23 06:20:21 +00:00
2020-03-20 10:10:48 +00:00
## bitRotateLeft(a, b) {#bitrotatelefta-b}
2019-01-30 10:39:46 +00:00
2020-03-20 10:10:48 +00:00
## bitRotateRight(a, b) {#bitrotaterighta-b}
2019-01-30 10:39:46 +00:00
2020-03-18 18:43:51 +00:00
## bitTest {#bittest}
2019-01-30 10:39:46 +00:00
2019-11-18 20:44:30 +00:00
Takes any integer and converts it into [binary form ](https://en.wikipedia.org/wiki/Binary_number ), returns the value of a bit at specified position. The countdown starts from 0 from the right to the left.
2019-01-30 10:39:46 +00:00
2020-03-20 10:10:48 +00:00
**Syntax**
2019-01-30 10:39:46 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-11-18 20:44:30 +00:00
SELECT bitTest(number, index)
```
2019-01-30 10:39:46 +00:00
2021-02-15 21:22:10 +00:00
**Arguments**
2019-11-18 20:44:30 +00:00
2021-03-13 18:18:45 +00:00
- `number` – Integer number.
- `index` – Position of bit.
2019-11-18 20:44:30 +00:00
**Returned values**
Returns a value of bit at specified position.
Type: `UInt8` .
**Example**
For example, the number 43 in base-2 (binary) numeral system is 101011.
Query:
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT bitTest(43, 1);
2019-11-18 20:44:30 +00:00
```
Result:
2020-03-20 10:10:48 +00:00
``` text
2019-11-18 20:44:30 +00:00
┌─bitTest(43, 1)─┐
│ 1 │
└────────────────┘
```
Another example:
Query:
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT bitTest(43, 2);
2019-11-18 20:44:30 +00:00
```
Result:
2020-03-20 10:10:48 +00:00
``` text
2019-11-18 20:44:30 +00:00
┌─bitTest(43, 2)─┐
│ 0 │
└────────────────┘
```
2020-03-18 18:43:51 +00:00
## bitTestAll {#bittestall}
2019-11-18 20:44:30 +00:00
Returns result of [logical conjuction ](https://en.wikipedia.org/wiki/Logical_conjunction ) (AND operator) of all bits at given positions. The countdown starts from 0 from the right to the left.
The conjuction for bitwise operations:
0 AND 0 = 0
2020-02-16 06:09:22 +00:00
2019-11-18 20:44:30 +00:00
0 AND 1 = 0
2020-02-16 06:09:22 +00:00
2019-11-18 20:44:30 +00:00
1 AND 0 = 0
2020-02-16 06:09:22 +00:00
2019-11-18 20:44:30 +00:00
1 AND 1 = 1
2020-03-20 10:10:48 +00:00
**Syntax**
2019-11-18 20:44:30 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-11-18 20:44:30 +00:00
SELECT bitTestAll(number, index1, index2, index3, index4, ...)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2019-11-18 20:44:30 +00:00
2021-03-13 18:18:45 +00:00
- `number` – Integer number.
- `index1` , `index2` , `index3` , `index4` – Positions of bit. For example, for set of positions (`index1`, `index2` , `index3` , `index4` ) is true if and only if all of its positions are true (`index1` ⋀ `index2` , ⋀ `index3` ⋀ `index4` ).
2019-11-18 20:44:30 +00:00
**Returned values**
Returns result of logical conjuction.
Type: `UInt8` .
**Example**
For example, the number 43 in base-2 (binary) numeral system is 101011.
Query:
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT bitTestAll(43, 0, 1, 3, 5);
2019-11-18 20:44:30 +00:00
```
Result:
2020-03-20 10:10:48 +00:00
``` text
2019-11-18 20:44:30 +00:00
┌─bitTestAll(43, 0, 1, 3, 5)─┐
│ 1 │
└────────────────────────────┘
```
Another example:
Query:
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT bitTestAll(43, 0, 1, 3, 5, 2);
2019-11-18 20:44:30 +00:00
```
Result:
2020-03-20 10:10:48 +00:00
``` text
2019-11-18 20:44:30 +00:00
┌─bitTestAll(43, 0, 1, 3, 5, 2)─┐
│ 0 │
└───────────────────────────────┘
```
2020-03-18 18:43:51 +00:00
## bitTestAny {#bittestany}
2019-11-18 20:44:30 +00:00
Returns result of [logical disjunction ](https://en.wikipedia.org/wiki/Logical_disjunction ) (OR operator) of all bits at given positions. The countdown starts from 0 from the right to the left.
The disjunction for bitwise operations:
0 OR 0 = 0
2020-02-16 06:09:22 +00:00
2019-11-18 20:44:30 +00:00
0 OR 1 = 1
2020-02-16 06:09:22 +00:00
2019-11-18 20:44:30 +00:00
1 OR 0 = 1
2020-02-16 06:09:22 +00:00
2019-11-18 20:44:30 +00:00
1 OR 1 = 1
2020-03-20 10:10:48 +00:00
**Syntax**
2019-11-18 20:44:30 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-11-18 20:44:30 +00:00
SELECT bitTestAny(number, index1, index2, index3, index4, ...)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2019-11-18 20:44:30 +00:00
2021-03-13 18:18:45 +00:00
- `number` – Integer number.
- `index1` , `index2` , `index3` , `index4` – Positions of bit.
2019-11-18 20:44:30 +00:00
**Returned values**
Returns result of logical disjuction.
Type: `UInt8` .
**Example**
For example, the number 43 in base-2 (binary) numeral system is 101011.
Query:
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT bitTestAny(43, 0, 2);
2019-11-18 20:44:30 +00:00
```
Result:
2020-03-20 10:10:48 +00:00
``` text
2019-11-18 20:44:30 +00:00
┌─bitTestAny(43, 0, 2)─┐
│ 1 │
└──────────────────────┘
```
Another example:
Query:
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT bitTestAny(43, 4, 2);
2019-11-18 20:44:30 +00:00
```
Result:
2020-03-20 10:10:48 +00:00
``` text
2019-11-18 20:44:30 +00:00
┌─bitTestAny(43, 4, 2)─┐
│ 0 │
└──────────────────────┘
```
2020-04-30 18:19:18 +00:00
2020-03-24 14:27:08 +00:00
## bitCount {#bitcount}
Calculates the number of bits set to one in the binary representation of a number.
2020-04-30 18:19:18 +00:00
**Syntax**
2020-03-24 14:27:08 +00:00
2020-04-30 18:19:18 +00:00
``` sql
2020-03-24 14:27:08 +00:00
bitCount(x)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2020-03-24 14:27:08 +00:00
2020-04-30 18:19:18 +00:00
- `x` — [Integer ](../../sql-reference/data-types/int-uint.md ) or [floating-point ](../../sql-reference/data-types/float.md ) number. The function uses the value representation in memory. It allows supporting floating-point numbers.
2020-03-24 14:27:08 +00:00
**Returned value**
2020-04-30 18:19:18 +00:00
- Number of bits set to one in the input number.
2020-03-24 14:27:08 +00:00
2020-04-30 18:19:18 +00:00
The function doesn’ t convert input value to a larger type ([sign extension](https://en.wikipedia.org/wiki/Sign_extension)). So, for example, `bitCount(toUInt8(-1)) = 8` .
2020-03-24 14:27:08 +00:00
Type: `UInt8` .
**Example**
Take for example the number 333. Its binary representation: 0000000101001101.
Query:
2020-04-30 18:19:18 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT bitCount(333);
2020-03-24 14:27:08 +00:00
```
Result:
2020-04-30 18:19:18 +00:00
``` text
2020-03-24 14:27:08 +00:00
┌─bitCount(333)─┐
│ 5 │
└───────────────┘
```
2018-10-16 10:47:17 +00:00
2021-04-02 11:19:25 +00:00
## bitHammingDistance {#bithammingdistance}
Returns the [Hamming Distance ](https://en.wikipedia.org/wiki/Hamming_distance ) between the bit representations of two integer values. Can be used with [SimHash ](../../sql-reference/functions/hash-functions.md#ngramsimhash ) functions for detection of semi-duplicate strings. The smaller is the distance, the more likely those strings are the same.
**Syntax**
``` sql
bitHammingDistance(int1, int2)
```
**Arguments**
- `int1` — First integer value. [Int64 ](../../sql-reference/data-types/int-uint.md ).
- `int2` — Second integer value. [Int64 ](../../sql-reference/data-types/int-uint.md ).
**Returned value**
- The Hamming distance.
Type: [UInt8 ](../../sql-reference/data-types/int-uint.md ).
**Examples**
Query:
``` sql
SELECT bitHammingDistance(111, 121);
```
Result:
``` text
┌─bitHammingDistance(111, 121)─┐
│ 3 │
└──────────────────────────────┘
```
With [SimHash ](../../sql-reference/functions/hash-functions.md#ngramsimhash ):
``` sql
SELECT bitHammingDistance(ngramSimHash('cat ate rat'), ngramSimHash('rat ate cat'));
```
Result:
``` text
┌─bitHammingDistance(ngramSimHash('cat ate rat'), ngramSimHash('rat ate cat'))─┐
│ 5 │
└──────────────────────────────────────────────────────────────────────────────┘
```