

# Bitstring functions

Use these functions to read, set, count, and find bits in a `BIT` value, and to pad a bitstring to a given length. For the operators that combine, invert, and shift bitstrings, see [Bitwise operators and functions](duckdb-functions-numeric-bitwise.md).

## `bit_count` function
<a name="athena-duckdb-functions-binary-bit-count"></a>

Counts the bits of `x` that are `1`. For an integer, the function counts the bits of its two's complement binary form, at the width of its type. A negative number therefore has its sign bit and its leading bits set. For `-1`, `bit_count` returns `32` when the value is an `INTEGER` and `64` when it is a `BIGINT`.

For a `BIT` value, the function counts the `1` bits in the bitstring. If `x` is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-binary-bit-count-syntax"></a>

```sql
bit_count({{x}})
```

### Arguments
<a name="athena-duckdb-functions-binary-bit-count-arguments"></a>

The `bit_count` function takes the following argument:

**{{x}}**  
The value in which to count the `1` bits, as a `TINYINT`, `SMALLINT`, `INTEGER`, `BIGINT`, or `HUGEINT` value, or a `BIT` value.

### Return type
<a name="athena-duckdb-functions-binary-bit-count-return-type"></a>

`TINYINT` for a `TINYINT`, `SMALLINT`, `INTEGER`, or `BIGINT` value, `SMALLINT` for a `HUGEINT` value, and `BIGINT` for a `BIT` value.

### Examples
<a name="athena-duckdb-functions-binary-bit-count-examples"></a>

The following SQL query counts the bits that are set in the number `31`, which is `11111` in binary.

```sql
SELECT bit_count(31) AS result
```

The query returns the following result.


| result | 
| --- | 
| `5` | 

The following SQL query counts the bits of `-1` as an `INTEGER` and as a `BIGINT`. In two's complement form, every bit of `-1` is set, so the result depends on the width of the type.

```sql
SELECT
    bit_count(-1) AS int_bits,
    bit_count(CAST(-1 AS BIGINT)) AS bigint_bits
```

The query returns the following result.


| int\_bits | bigint\_bits | 
| --- | --- | 
| `32` | `64` | 

The following SQL query counts the `1` bits in a bitstring.

```sql
SELECT bit_count('1011'::BIT) AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

### Considerations
<a name="athena-duckdb-functions-binary-bit-count-considerations"></a>

`bit_count` does not accept `DECIMAL` or floating-point values, so a call such as `bit_count(1.5)` fails. Cast the value to an integer type first.

An integer literal such as `-1` is an `INTEGER`. To count a negative number at another width, cast it, as in `bit_count(CAST(-1 AS BIGINT))`.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-binary-bit-count-differences-from-engine-version-3"></a>

In Athena engine version 3, `bit_count` takes two arguments, the value and the number of bits to count it in, as in `bit_count({{x}}, 64)`. In DuckDB, `bit_count` takes only the value and uses the width of its type, so a call with two arguments fails. To count a value as a 64-bit number, cast it to `BIGINT`, as in `bit_count(CAST({{x}} AS BIGINT))`.

## `bit_length` function
<a name="athena-duckdb-functions-binary-bit-length"></a>

Returns the size of `value` in bits. For a `VARCHAR` value, this is the size of the string's UTF-8 encoding, which is 8 times its length in bytes. For a `BIT` value, this is the number of bits in the bit string. If `value` is `NULL`, the result is `NULL`.

A character outside the ASCII range takes more than one byte in UTF-8, so for such a string the result is more than 8 times its length in characters.

### Syntax
<a name="athena-duckdb-functions-binary-bit-length-syntax"></a>

```sql
bit_length({{value}})
```

### Arguments
<a name="athena-duckdb-functions-binary-bit-length-arguments"></a>

The `bit_length` function takes the following argument:

**{{value}}**  
The string or bitstring to measure, as a `VARCHAR` value or a `BIT` value.

### Return type
<a name="athena-duckdb-functions-binary-bit-length-return-type"></a>

`BIGINT`

### Examples
<a name="athena-duckdb-functions-binary-bit-length-examples"></a>

The following SQL query returns the number of bits in a three-character ASCII string.

```sql
SELECT bit_length('abc') AS result
```

The query returns the following result.


| result | 
| --- | 
| `24` | 

The following SQL query returns the number of bits in a `BIT` value.

```sql
SELECT bit_length(CAST('10101' AS BIT)) AS result
```

The query returns the following result.


| result | 
| --- | 
| `5` | 

The following SQL query uses `chr(233)`, the letter e with an acute accent. It is one character, but it takes 2 bytes in UTF-8, so the result is 16 bits.

```sql
SELECT
    length(chr(233)) AS characters,
    bit_length(chr(233)) AS bits
```

The query returns the following result.


| characters | bits | 
| --- | --- | 
| `1` | `16` | 

### Considerations
<a name="athena-duckdb-functions-binary-bit-length-considerations"></a>

`bit_length` does not accept numbers or `BLOB` values. For a `BLOB` value, use `octet_length({{blob}}) * 8`.

## `bit_position` function
<a name="athena-duckdb-functions-binary-bit-position"></a>

Searches `bitstring` for `substring` and returns the position where the first match starts. Positions start at `1` for the leftmost bit. If `substring` does not occur in `bitstring`, the result is `0`. If either argument is `NULL`, the result is `NULL`.

The bitstring to find is the first argument, and the bitstring to search is the second.

### Syntax
<a name="athena-duckdb-functions-binary-bit-position-syntax"></a>

```sql
bit_position({{substring}}, {{bitstring}})
```

### Arguments
<a name="athena-duckdb-functions-binary-bit-position-arguments"></a>

The `bit_position` function takes the following arguments:

**{{substring}}**  
The bitstring to find, as a `BIT` value.

**{{bitstring}}**  
The bitstring to search, as a `BIT` value.

### Return type
<a name="athena-duckdb-functions-binary-bit-position-return-type"></a>

`INTEGER`

### Examples
<a name="athena-duckdb-functions-binary-bit-position-examples"></a>

The following SQL query finds the bitstring `010` in `1110101`. The first match starts at the fourth bit.

```sql
SELECT
    bit_position('010'::BIT, '1110101'::BIT) AS result
```

The query returns the following result.


| result | 
| --- | 
| `4` | 

The following SQL query searches for two adjacent `1` bits in a bitstring that has none, so it returns `0`.

```sql
SELECT bit_position('11'::BIT, '1010101'::BIT) AS result
```

The query returns the following result.


| result | 
| --- | 
| `0` | 

### Considerations
<a name="athena-duckdb-functions-binary-bit-position-considerations"></a>

`bit_position` counts positions from `1`, but `get_bit` and `set_bit` count positions from `0`. To pass a position from `bit_position` to `get_bit` or `set_bit`, subtract 1.

## `bitstring` function
<a name="athena-duckdb-functions-binary-bitstring"></a>

Returns `bitstring` as a `BIT` value that is `length` bits long, with `0` bits added on the left. The added bits do not change the bits that you pass, so `bitstring('1010', 7)` returns `0001010`. If `bitstring` already has `length` bits, the function returns it unchanged.

You can pass a `VARCHAR` value of `0` and `1` characters instead of a `BIT` value, and the function converts it. If any argument is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-binary-bitstring-syntax"></a>

```sql
bitstring({{bitstring}}, {{length}})
```

### Arguments
<a name="athena-duckdb-functions-binary-bitstring-arguments"></a>

The `bitstring` function takes the following arguments:

**{{bitstring}}**  
The bitstring to pad, as a `BIT` value or as a `VARCHAR` value that holds only the characters `0` and `1`.

**{{length}}**  
The number of bits in the result, as an `INTEGER`.

### Return type
<a name="athena-duckdb-functions-binary-bitstring-return-type"></a>

`BIT`

### Examples
<a name="athena-duckdb-functions-binary-bitstring-examples"></a>

The following SQL query pads a 4-bit value to 7 bits.

```sql
SELECT bitstring('1010'::BIT, 7) AS result
```

The query returns the following result.


| result | 
| --- | 
| `0001010` | 

The following SQL query passes a `VARCHAR` value and pads it to 8 bits.

```sql
SELECT bitstring('101', 8) AS result
```

The query returns the following result.


| result | 
| --- | 
| `00000101` | 

### Considerations
<a name="athena-duckdb-functions-binary-bitstring-considerations"></a>

`bitstring` does not shorten a value. If `length` is less than the number of bits in `bitstring`, the query fails.

If a `VARCHAR` argument holds a character other than `0` or `1`, or is an empty string, the query fails.

## `get_bit` function
<a name="athena-duckdb-functions-binary-get-bit"></a>

Returns the bit of `bitstring` at position `index`, as the `INTEGER` `0` or `1`. Positions start at `0` for the leftmost bit. If any argument is `NULL`, the result is `NULL`.

To change a bit, use the [`set_bit` function](#athena-duckdb-functions-binary-set-bit).

### Syntax
<a name="athena-duckdb-functions-binary-get-bit-syntax"></a>

```sql
get_bit({{bitstring}}, {{index}})
```

### Arguments
<a name="athena-duckdb-functions-binary-get-bit-arguments"></a>

The `get_bit` function takes the following arguments:

**{{bitstring}}**  
The bitstring to read, as a `BIT` value.

**{{index}}**  
The position of the bit to return, as an `INTEGER`. `0` is the leftmost bit.

### Return type
<a name="athena-duckdb-functions-binary-get-bit-return-type"></a>

`INTEGER`

### Examples
<a name="athena-duckdb-functions-binary-get-bit-examples"></a>

The following SQL query returns the bit at position `2`, the third bit from the left.

```sql
SELECT get_bit('0110010'::BIT, 2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1` | 

The following SQL query returns the first and the last bit of a 7-bit value. The first bit is at position `0`, and the last is at position `6`.

```sql
SELECT
    get_bit('1000000'::BIT, 0) AS first_bit,
    get_bit('1000000'::BIT, 6) AS last_bit
```

The query returns the following result.


| first\_bit | last\_bit | 
| --- | --- | 
| `1` | `0` | 

### Considerations
<a name="athena-duckdb-functions-binary-get-bit-considerations"></a>

If `index` is negative, or is not less than the number of bits in `bitstring`, the query fails.

`get_bit` and `set_bit` count positions from `0`, but `bit_position` counts positions from `1`.

## `set_bit` function
<a name="athena-duckdb-functions-binary-set-bit"></a>

Returns `bitstring` with the bit at position `index` set to `new_value`. Positions start at `0` for the leftmost bit. The function returns a new value and leaves its argument unchanged. If any argument is `NULL`, the result is `NULL`.

To read a bit, use the [`get_bit` function](#athena-duckdb-functions-binary-get-bit).

### Syntax
<a name="athena-duckdb-functions-binary-set-bit-syntax"></a>

```sql
set_bit({{bitstring}}, {{index}}, {{new_value}})
```

### Arguments
<a name="athena-duckdb-functions-binary-set-bit-arguments"></a>

The `set_bit` function takes the following arguments:

**{{bitstring}}**  
The bitstring to change, as a `BIT` value.

**{{index}}**  
The position of the bit to set, as an `INTEGER`. `0` is the leftmost bit.

**{{new\_value}}**  
The new value of the bit, as the `INTEGER` `0` or `1`.

### Return type
<a name="athena-duckdb-functions-binary-set-bit-return-type"></a>

`BIT`

### Examples
<a name="athena-duckdb-functions-binary-set-bit-examples"></a>

The following SQL query sets the bit at position `2`, the third bit from the left, to `0`.

```sql
SELECT set_bit('0110010'::BIT, 2, 0) AS result
```

The query returns the following result.


| result | 
| --- | 
| `0100010` | 

The following SQL query sets the leftmost bit, at position `0`, to `1`.

```sql
SELECT set_bit('0110010'::BIT, 0, 1) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1110010` | 

### Considerations
<a name="athena-duckdb-functions-binary-set-bit-considerations"></a>

If `index` is negative or is not less than the number of bits in `bitstring`, or if `new_value` is not `0` or `1`, the query fails.

`get_bit` and `set_bit` count positions from `0`, but `bit_position` counts positions from `1`.