Skip to main content
Skip to main content

Bit Functions

Bit functions work for any pair of types from UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64, Float32, or Float64. Some functions support String and FixedString types.

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.

bitAnd(a, b)

bitOr(a, b)

bitXor(a, b)

bitNot(a)

bitShiftLeft(a, b)

Shifts the binary representation of a value to the left 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

Arguments

Returned value

  • Shifted value.

The type of the returned value is the same as the type of the input value.

Example

In the following queries bin and hex functions are used to show bits of shifted values.

Result:

bitShiftRight(a, b)

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. Note that the length of a String value is reduced as bits are shifted out.

Syntax

Arguments

Returned value

  • Shifted value.

The type of the returned value is the same as the type of the input value.

Example

Query:

Result:

bitRotateLeft(a, b)

bitRotateRight(a, b)

bitSlice(s, offset, length)

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

Syntax

Arguments

  • s — s is String or FixedString.
  • offset — The start index with bit, A positive value indicates an offset on the left, and a negative value is an indent on the right. Numbering of the bits begins with 1.
  • length — The length of substring with bit. If you specify a negative value, the function returns an open substring [offset, array_length - length]. If you omit the value, the function returns the substring [offset, the_end_string]. If length exceeds s, it will be truncate.If length isn't multiple of 8, will fill 0 on the right.

Returned value

Example

Query:

Result:

byteSlice(s, offset, length)

See function substring.

bitTest

Takes any integer and converts it into binary form, returns the value of a bit at specified position. Counting is right-to-left, starting at 0.

Syntax

Arguments

  • number – Integer number.
  • index – Position of bit.

Returned value

  • Value of the bit at the specified position. UInt8.

Example

For example, the number 43 in base-2 (binary) numeral system is 101011.

Query:

Result:

Another example:

Query:

Result:

bitTestAll

Returns result of logical conjunction (AND operator) of all bits at given positions. Counting is right-to-left, starting at 0.

The conjunction for bit-wise operations:

0 AND 0 = 0

0 AND 1 = 0

1 AND 0 = 0

1 AND 1 = 1

Syntax

Arguments

  • 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 (index1index2, ⋀ index3index4).

Returned value

  • Result of the logical conjunction. UInt8.

Example

For example, the number 43 in base-2 (binary) numeral system is 101011.

Query:

Result:

Another example:

Query:

Result:

bitTestAny

Returns result of logical disjunction (OR operator) of all bits at given positions. Counting is right-to-left, starting at 0.

The disjunction for bit-wise operations:

0 OR 0 = 0

0 OR 1 = 1

1 OR 0 = 1

1 OR 1 = 1

Syntax

Arguments

  • number – Integer number.
  • index1, index2, index3, index4 – Positions of bit.

Returned value

  • Result of the logical disjunction. UInt8.

Example

For example, the number 43 in base-2 (binary) numeral system is 101011.

Query:

Result:

Another example:

Query:

Result:

bitCount

Calculates the number of bits set to one in the binary representation of a number.

Syntax

Arguments

  • xInteger or floating-point number. The function uses the value representation in memory. It allows supporting floating-point numbers.

Returned value

  • Number of bits set to one in the input number. UInt8.
note

The function does not convert the input value to a larger type (sign extension). So, for example, bitCount(toUInt8(-1)) = 8.

Example

Take for example the number 333. Its binary representation: 0000000101001101.

Query:

Result:

bitHammingDistance

Returns the Hamming Distance between the bit representations of two integer values. Can be used with SimHash functions for detection of semi-duplicate strings. The smaller is the distance, the more likely those strings are the same.

Syntax

Arguments

  • int1 — First integer value. Int64.
  • int2 — Second integer value. Int64.

Returned value

  • The Hamming distance. UInt8.

Examples

Query:

Result:

With SimHash:

Result:

bitAnd

Introduced in: v1.1

Syntax

Arguments

Returned value

Returns the result of bitwise operation a AND b

Examples

Usage example

bitCount

Introduced in: v20.3

Syntax

Arguments

Returned value

Returns the number of bits set to one in x. UInt8.

note

The function does not convert the input value to a larger type (sign extension). For example: bitCount(toUInt8(-1)) = 8.

Examples

Usage example

bitHammingDistance

Introduced in: v21.1

Syntax

Arguments

Returned value

Returns the hamming distance between x and y. UInt8.

Examples

Usage example

bitNot

Introduced in: v1.1

Syntax

Arguments

Returned value

Returns the result of ~a i.e a with bits flipped.

Examples

Usage example

bitOr

Introduced in: v1.1

Syntax

Arguments

Returned value

Returns the result of bitwise operation a OR b

Examples

Usage example

bitRotateLeft

Introduced in: v1.1

Syntax

Arguments

Returned value

Returns the rotated value with type equal to that of a.

Examples

Usage example

bitRotateRight

Introduced in: v1.1

Syntax

Arguments

Returned value

Returns the rotated value with type equal to that of a.

Examples

Usage example

bitShiftLeft

Introduced in: v1.1

Syntax

Arguments

Returned value

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

Examples

Usage example with binary encoding

Usage example with hexadecimal encoding

Usage example with Fixed String encoding

bitShiftRight

Introduced in: v1.1

Syntax

Arguments

Returned value

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

Examples

Usage example with binary encoding

Usage example with hexadecimal encoding

Usage example with Fixed String encoding

bitSlice

Introduced in: v22.2

Syntax

Arguments

  • s — The String or Fixed String to slice. String/FixedString(N).

  • offset — The starting bit position (1-based indexing). (U)Int8/16/32/64/Float32/64.

  • Positive values: count from the beginning of the string.

  • Negative values: count from the end of the string.

  • length — Optional. The number of bits to extract. (U)Int8/16/32/64/Float32/64.

  • 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.

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

Examples

Usage example

bitTest

Introduced in: v1.1

Syntax

Arguments

Returned value

Returns the value of the bit at position i in the binary representation of a. UInt8.

Examples

Usage example

bitTestAll

Introduced in: v1.1

Syntax

Arguments

Returned value

Returns the result of the logical conjunction. UInt8.

Examples

Usage example 1

Usage example 2

bitTestAny

Introduced in: v1.1

Syntax

Arguments

Returned value

Returns the result of the logical disjunction. UInt8.

Examples

Usage example 1

Usage example 2

bitXor

Introduced in: v1.1

Syntax

Arguments

Returned value

Returns the result of bitwise operation a XOR b

Examples

Usage example