View a markdown version of this page

Bitstring functions - Amazon Athena

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.

bit_count function

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

bit_count(x)

Arguments

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

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

Examples

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

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.

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.

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

The query returns the following result.

result

3

Considerations

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

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

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

bit_length(value)

Arguments

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

BIGINT

Examples

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

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.

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.

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

The query returns the following result.

characters bits

1

16

Considerations

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

bit_position function

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

bit_position(substring, bitstring)

Arguments

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

INTEGER

Examples

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

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.

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

The query returns the following result.

result

0

Considerations

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

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

bitstring(bitstring, length)

Arguments

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

BIT

Examples

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

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.

SELECT bitstring('101', 8) AS result

The query returns the following result.

result

00000101

Considerations

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

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.

Syntax

get_bit(bitstring, index)

Arguments

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

INTEGER

Examples

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

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.

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

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

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.

Syntax

set_bit(bitstring, index, new_value)

Arguments

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

BIT

Examples

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

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.

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

The query returns the following result.

result

1110010

Considerations

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.