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
Syntax
Arguments
a
— First value.(U)Int*
/Float32/64
.b
— Second value.(U)Int*
/Float32/64
.
Returned value
Returns the result of bitwise operation a AND b
Examples
Usage example
bitCount
Introduced in: v20.3
Syntax
Arguments
x
— An integer or float value.(U)Int*
/Float32/64
.
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
Syntax
Arguments
x
— First number for Hamming distance calculation.(U)Int*
/Float32/64
.y
— Second number for Hamming distance calculation.(U)Int*
/Float32/64
.
Returned value
Returns the hamming distance between x
and y
. UInt8
.
Examples
Usage example
bitNot
Introduced in: v1.1
Syntax
Arguments
a
— Value for which to apply bitwise NOT operation.(U)Int*
/Float32/64
/String.
Returned value
Returns the result of ~a
i.e a
with bits flipped.
Examples
Usage example
bitOr
Introduced in: v1.1
Syntax
Arguments
a
— First value.(U)Int*
/Float32/64
.b
— Second value.(U)Int*
/Float32/64
.
Returned value
Returns the result of bitwise operation a OR b
Examples
Usage example
bitRotateLeft
Introduced in: v1.1
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
.
Examples
Usage example
bitRotateRight
Introduced in: v1.1
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
.
Examples
Usage example
bitShiftLeft
Introduced in: v1.1
Syntax
Arguments
a
— A value to shift.(U)Int*
/String
/FixedString
.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
Syntax
Arguments
a
— A value to shift.(U)Int*
/String
/FixedString
.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
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
a
— Number to convert.(U)Int8/16/32/64
/Float32/64
.i
— Position of the bit to return.(U)Int8/16/32/64
/Float32/64
.
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
a
— An integer value.(U)Int8/16/32/64
.index1[, index2, ... , indexN]
— 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
Syntax
Arguments
a
— An integer value.(U)Int8/16/32/64
.index1[, index2, ... , indexN]
— 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
Syntax
Arguments
a
— First value.(U)Int*
/Float32/64
.b
— Second value.(U)Int*
/Float32/64
.
Returned value
Returns the result of bitwise operation a XOR b
Examples
Usage example