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
Introduced in: v1.1
Performs bitwise AND operation between two values.
Syntax
Arguments
Returned value
Returns the result of bitwise operation a AND b
Examples
Usage example
bitCount
Introduced in: v20.3
Calculates the number of bits set to one in the binary representation of a number.
Syntax
Arguments
Returned value
Returns the number of bits set to one in x
. UInt8
.
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
Returns the Hamming Distance between the bit representations of two numbers.
Can be used with SimHash
functions for detection of semi-duplicate strings.
The smaller the distance, the more similar the strings are.
Syntax
Arguments
x
— First number for Hamming distance calculation.(U)Int*
orFloat*
y
— Second number for Hamming distance calculation.(U)Int*
orFloat*
Returned value
Returns the hamming distance between x
and y
UInt8
Examples
Usage example
bitNot
Introduced in: v1.1
Performs the bitwise NOT operation.
Syntax
Arguments
Returned value
Returns the result of ~a
i.e a
with bits flipped.
Examples
Usage example
bitOr
Introduced in: v1.1
Performs bitwise OR operation between two values.
Syntax
Arguments
Returned value
Returns the result of bitwise operation a OR b
Examples
Usage example
bitRotateLeft
Introduced in: v1.1
Rotate bits left by a certain number of positions. Bits that fall off wrap around to the right.
Syntax
Arguments
a
— A value to rotate.(U)Int8/16/32/64
N
— The number of positions to rotate left.UInt8/16/32/64
Returned value
Returns the rotated value with type equal to that of a
. (U)Int8/16/32/64
Examples
Usage example
bitRotateRight
Introduced in: v1.1
Rotate bits right by a certain number of positions. Bits that fall off wrap around to the left.
Syntax
Arguments
a
— A value to rotate.(U)Int8/16/32/64
N
— The number of positions to rotate right.UInt8/16/32/64
Returned value
Returns the rotated value with type equal to that of a
. (U)Int8/16/32/64
Examples
Usage example
bitShiftLeft
Introduced in: v1.1
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
a
— A value to shift.(U)Int*
orString
orFixedString
N
— The number of positions to shift.UInt8/16/32/64
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
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
Arguments
a
— A value to shift.(U)Int*
orString
orFixedString
N
— The number of positions to shift.UInt8/16/32/64
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
Returns a substring starting with the bit from the 'offset' index that is 'length' bits long.
Syntax
Arguments
-
s
— The String or Fixed String to slice.String
orFixedString
-
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.
-
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
orFloat*
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
Takes any number and converts it into binary form, then returns the value of the bit at a specified position. Counting is done right-to-left, starting at 0.
Syntax
Arguments
a
— Number to convert.(U)Int8/16/32/64
orFloat*
i
— Position of the bit to return.(U)Int8/16/32/64
orFloat*
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
Returns result of the 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
Arguments
a
— An integer value.(U)Int8/16/32/64
index1, ...
— One or multiple positions of bits.(U)Int8/16/32/64
Returned value
Returns the result of the logical conjunction UInt8
Examples
Usage example 1
Usage example 2
bitTestAny
Introduced in: v1.1
Returns result of the 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
Arguments
a
— An integer value.(U)Int8/16/32/64
index1, ...
— One or multiple positions of bits.(U)Int8/16/32/64
Returned value
Returns the result of the logical disjunction UInt8
Examples
Usage example 1
Usage example 2
bitXor
Introduced in: v1.1
Performs bitwise exclusive or (XOR) operation between two values.
Syntax
Arguments
Returned value
Returns the result of bitwise operation a XOR b
Examples
Usage example