Skip to content

Bit expressions

For the complete documentation index see: llms.txt

All documentation pages available in markdown.

Bit expressions read and modify blob (bytes) values inside an expression tree: reading bit ranges, counting and scanning bits, resizing or patching bytes, and applying bitwise math without round-tripping the whole blob to the client. For the data type itself and its Operate API, see Blob (bytes).

Read expressions (bit_get, bit_count, bit_lscan, …) evaluate to an integer, a blob, or a string. That makes them projection: they shape what a command returns for each record, in operate, in a batch, and in a query. Wrap one in comparison or logic to get the Boolean a record filter needs.

Modify expressions (bit_set, bit_add, bit_insert, …) evaluate to a new blob and leave the record untouched. Storing that value takes a write operation expression; see Operations and expressions for how the two APIs differ.

A blob operand is a bin, read with bin_blob, or any expression that evaluates to a blob. Reading stored data can evaluate to unknown; see Unknown results.

The examples use a blob bin device_flags holding [0x01, 0x42, 0x03, 0x04, 0x05]; bit_b64_encode uses the Blob reference’s flags, [0x01, 0x42, 0xB0, 0x04, 0x05].

The Developer SDK’s AEL text syntax exposes the same bit/blob functions as path methods, for example $.header.bitGetInt(offset: 0, size: 16). See BLOB (bit) path functions.

Composing expressions

An expression evaluates to a value, and that value is what the next expression operates on — see Expressions compose.

Every bit operation takes its bin operand that way: not only a named blob bin, but the result of any expression evaluating to a blob. bit_set shows the pattern — its example reads the result of a bit_set back with bit_get, so the read sees the patched blob while the record keeps the original.

Each operation’s Example shows the code in nine tabs: Aerospike Expression Language (AEL) text on the Java SDK and Python SDK tabs, and the Exp builder on the other seven. See the AEL reference for AEL grammar.

Modify

bit_add

bit_add(policy, bit_offset, bit_size, value, signed, action, bin)
Description

Returns the blob with value added to the bit_size-bit big-endian integer starting at bit_offset, read as signed or unsigned according to signed. The rest of the blob is unchanged, and its length does not change.

Arguments
NameTypeDescription
policyBit policy

Write policy for the transform. Supplied by your client as a blob or bitwise policy object.

bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region being added to.

bit_sizeinteger

Width of the region in bits, up to 64. This is the width of the integer the addition is performed on, not the width of value.

valueinteger

Integer to add. It is converted to a bit_size-bit integer before the addition, so a value too wide for the region is truncated rather than rejected.

signedboolean literal

Whether the region is read as a signed or an unsigned integer. This does not change the arithmetic; it decides where the limits sit, and so which results count as overflow. In an 8-bit region, adding 1 to 0x7F overflows when signed and does not when unsigned; adding 1 to 0xFF overflows when unsigned and does not when signed.

actionOverflow action

What happens when the addition crosses the region’s limit: fail, which is the default; saturate at the limit; or wrap around past it. Which values count as crossing depends on signed. Supplied by your client as an overflow-action constant.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where adding 1 to the unsigned 8-bit field at bit offset 16 (overflow FAIL) matches reading the next byte at offset 24.

String exp = "$.device_flags.bitAdd(offset: 16, size: 8, value: 1)"
+ ".bitGet(offset: 16, size: 8)"
+ " == $.device_flags.bitGet(offset: 24, size: 8)";

bit_and

bit_and(policy, bit_offset, bit_size, value, bin)
Description

Returns the blob with the bit_size bits at bit_offset replaced by their bitwise AND with value. The rest of the blob is unchanged, and its length does not change.

Arguments
NameTypeDescription
policyBit policy

Write policy for the transform. Supplied by your client as a blob or bitwise policy object.

bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

valueblob

Blob supplying the bits to AND with. Must be at least bit_size bits.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where AND-ing bits 16–23 (0x03) with 0x01 leaves 0x01.

String exp = "$.device_flags.bitAnd(offset: 16, size: 8, value: x'01')"
+ ".bitGet(offset: 16, size: 8) == x'01'";

bit_insert

bit_insert(policy, byte_offset, blob, bin)
Description

Returns the blob with blob inserted at byte_offset, growing it by the length of blob.

Arguments
NameTypeDescription
policyBit policy

Write policy for the transform. Supplied by your client as a blob or bitwise policy object.

byte_offsetinteger

Offset in bytes from the start of the blob at which to insert.

blobblob

Bytes to insert.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where inserting 0xFF at byte offset 1 makes the unsigned 8-bit integer at bit offset 8 equal 0xFF.

String exp = "$.device_flags.bitInsert(byteOffset: 1, value: x'ff')"
+ ".bitGetInt(offset: 8, size: 8) == 0xff";

bit_lshift

bit_lshift(policy, bit_offset, bit_size, shift, bin)
Description

Returns the blob with the bit_size bits at bit_offset shifted left by shift, filling from the right with zeros. Bits shifted out of the region are discarded. The rest of the blob is unchanged, and its length does not change.

Arguments
NameTypeDescription
policyBit policy

Write policy for the transform. Supplied by your client as a blob or bitwise policy object.

bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

shiftinteger

Number of bit positions to shift by.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where shifting the first byte left by two bits preserves the following six bits relative to the unmodified blob.

String exp = "$.device_flags.bitLshift(offset: 0, size: 8, shift: 2)"
+ ".bitGet(offset: 0, size: 6)"
+ " == $.device_flags.bitGet(offset: 2, size: 6)";

bit_not

bit_not(policy, bit_offset, bit_size, bin)
Description

Returns the blob with the bit_size bits at bit_offset replaced by their bitwise complement. The rest of the blob is unchanged, and its length does not change.

Arguments
NameTypeDescription
policyBit policy

Write policy for the transform. Supplied by your client as a blob or bitwise policy object.

bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where NOT-ing one bit at offset 6 makes the first byte match bits 16–23 of the original blob (0x03).

String exp = "$.device_flags.bitNot(offset: 6, size: 1)"
+ ".bitGet(offset: 0, size: 8)"
+ " == $.device_flags.bitGet(offset: 16, size: 8)";

bit_or

bit_or(policy, bit_offset, bit_size, value, bin)
Description

Returns the blob with the bit_size bits at bit_offset replaced by their bitwise OR with value. The rest of the blob is unchanged, and its length does not change.

Arguments
NameTypeDescription
policyBit policy

Write policy for the transform. Supplied by your client as a blob or bitwise policy object.

bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

valueblob

Blob supplying the bits to OR with. Must be at least bit_size bits.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where OR-ing 0x01 into bits 24–31 makes the 8-bit slice at offset 24 match the slice at offset 32.

String exp = "$.device_flags.bitOr(offset: 24, size: 8, value: x'01')"
+ ".bitGet(offset: 24, size: 8)"
+ " == $.device_flags.bitGet(offset: 32, size: 8)";

bit_remove

bit_remove(policy, byte_offset, byte_size, bin)
Description

Returns the blob with byte_size bytes removed starting at byte_offset, shrinking it by that many bytes.

Arguments
NameTypeDescription
policyBit policy

Write policy for the transform. Supplied by your client as a blob or bitwise policy object.

byte_offsetinteger

Offset in bytes from the start of the blob at which to start removing.

byte_sizeinteger

Number of bytes to remove.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where removing the first byte leaves 0x42 as the unsigned 8-bit value at bit offset 0.

String exp = "$.device_flags.bitRemove(byteOffset: 0, byteSize: 1)"
+ ".bitGetInt(offset: 0, size: 8) == 0x42";

bit_resize

bit_resize(policy, byte_size, resize_flags, bin)
Description

Returns the blob resized to byte_size bytes. Growing pads with zero bytes at the end and shrinking drops bytes from the end; resize_flags can work at the front instead, or forbid growing or shrinking.

Arguments
NameTypeDescription
policyBit policy

Write policy for the transform. Supplied by your client as a blob or bitwise policy object.

byte_sizeinteger

New size of the blob in bytes.

resize_flagsResize flags

Where bytes are added or dropped, and whether both directions are allowed: by default at the end, with from_front working at the beginning instead, grow_only forbidding shrinking, and shrink_only forbidding growing.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where the first byte after resizing device_flags to six bytes (default resize flags) is still 0x01 — modify expressions evaluate on a temporary blob value.

String exp = "$.device_flags.bitResize(byteSize: 6)"
+ ".bitGet(offset: 0, size: 8) == x'01'";

bit_rshift

bit_rshift(policy, bit_offset, bit_size, shift, bin)
Description

Returns the blob with the bit_size bits at bit_offset shifted right by shift, filling from the left with zeros. Bits shifted out of the region are discarded. The rest of the blob is unchanged, and its length does not change.

Arguments
NameTypeDescription
policyBit policy

Write policy for the transform. Supplied by your client as a blob or bitwise policy object.

bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

shiftinteger

Number of bit positions to shift by.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where shifting bits 24–31 right by two leaves the following six bits unchanged relative to the original blob.

String exp = "$.device_flags.bitRshift(offset: 24, size: 8, shift: 2)"
+ ".bitGet(offset: 26, size: 6)"
+ " == $.device_flags.bitGet(offset: 24, size: 6)";

bit_set

bit_set(policy, bit_offset, bit_size, value, bin)
Description

Returns the blob with the bit_size bits at bit_offset replaced by the leading bit_size bits of value. value must supply at least that many bits. The rest of the blob is unchanged, and its length does not change.

Arguments
NameTypeDescription
policyBit policy

Write policy for the transform. Supplied by your client as a blob or bitwise policy object.

bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

valueblob

Blob supplying the replacement bits, taken from its leading bits. Must be at least bit_size bits.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where setting one bit at offset 31 makes the 8-bit slice at offset 24 match the original slice at offset 32.

String exp = "$.device_flags.bitSet(offset: 31, size: 1, value: x'80')"
+ ".bitGet(offset: 24, size: 8)"
+ " == $.device_flags.bitGet(offset: 32, size: 8)";

bit_set_int

bit_set_int(policy, bit_offset, bit_size, value, bin)
Description

Returns the blob with the bit_size bits at bit_offset replaced by value, written as a big-endian integer of that width. The rest of the blob is unchanged, and its length does not change.

Arguments
NameTypeDescription
policyBit policy

Write policy for the transform. Supplied by your client as a blob or bitwise policy object.

bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

valueinteger

Integer to write into the region.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where writing integer 0x42 into bits 24–31 makes that 8-bit slice match the original second byte of device_flags.

String exp = "$.device_flags.bitSetInt(offset: 24, size: 8, value: 0x42)"
+ ".bitGet(offset: 24, size: 8)"
+ " == $.device_flags.bitGet(offset: 8, size: 8)";

bit_subtract

bit_subtract(policy, bit_offset, bit_size, value, signed, action, bin)
Description

Returns the blob with value subtracted from the bit_size-bit big-endian integer starting at bit_offset, read as signed or unsigned according to signed. The rest of the blob is unchanged, and its length does not change.

Arguments
NameTypeDescription
policyBit policy

Write policy for the transform. Supplied by your client as a blob or bitwise policy object.

bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

valueinteger

Integer to subtract. It is converted to a bit_size-bit integer first.

signedboolean literal

Whether the region is read as a signed or an unsigned integer. This does not change the arithmetic; it decides where the limits sit, and so which results count as overflow. In an 8-bit region, adding 1 to 0x7F overflows when signed and does not when unsigned; adding 1 to 0xFF overflows when unsigned and does not when signed.

actionOverflow action

What happens when the subtraction crosses the region’s limit: fail, which is the default; saturate at the limit; or wrap around past it. Which values count as crossing depends on signed. Supplied by your client as an overflow-action constant.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where subtracting 1 from the unsigned 8-bit field at bit offset 24 (overflow FAIL) matches the field at offset 16.

String exp = "$.device_flags.bitSubtract(offset: 24, size: 8, value: 1)"
+ ".bitGet(offset: 24, size: 8)"
+ " == $.device_flags.bitGet(offset: 16, size: 8)";

bit_xor

bit_xor(policy, bit_offset, bit_size, value, bin)
Description

Returns the blob with the bit_size bits at bit_offset replaced by their bitwise XOR with value. The rest of the blob is unchanged, and its length does not change.

Arguments
NameTypeDescription
policyBit policy

Write policy for the transform. Supplied by your client as a blob or bitwise policy object.

bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

valueblob

Blob supplying the bits to XOR with. Must be at least bit_size bits.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where XOR-ing the first byte with 0x02 yields the same 8-bit slice as bits 16–23 of the original device_flags.

String exp = "$.device_flags.bitXor(offset: 0, size: 8, value: x'02')"
+ ".bitGet(offset: 0, size: 8)"
+ " == $.device_flags.bitGet(offset: 16, size: 8)";

Read

bit_b64_encode

bit_b64_encode(byte_offset, byte_size, invert_size, bin)
Description

Returns the byte_size bytes starting at byte_offset, base64 encoded as a string. With invert_size, byte_size counts back from the end of the blob instead, so the span runs from byte_offset to that many bytes before the end.

Arguments
NameTypeDescription
byte_offsetinteger

Offset in bytes from the start of the blob.

byte_sizeinteger

Number of bytes to encode. When invert_size is true, this is instead the number of bytes to omit from the end.

invert_sizeboolean literal

When true, byte_size is the number of bytes to omit from the end of the blob rather than the number to encode, so the span runs from byte_offset to byte_size bytes before the end.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
string
Introduced
8.2.0
Example

Filter where the base64 text of the whole flags bin equals AUKwBAU= (the 5-byte [0x01, 0x42, 0xB0, 0x04, 0x05] pattern used throughout the Blob reference).

String exp = "$.flags.b64Encode() == 'AUKwBAU='";

bit_count

bit_count(bit_offset, bit_size, bin)
Description

Returns the number of bits set to 1 in the bit_size bits starting at bit_offset.

Arguments
NameTypeDescription
bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
integer
Introduced
5.2.0.4
Example

Filter where the second byte of device_flags (bits 8–15) has more than one bit set (0x42).

String exp = "$.device_flags.bitCount(offset: 8, size: 8) > 1";

bit_get

bit_get(bit_offset, bit_size, bin)
Description

Returns the bit_size bits starting at bit_offset, as a blob.

Arguments
NameTypeDescription
bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
blob
Introduced
5.2.0.4
Example

Filter where bits 16–23 of blob bin device_flags read as one byte equal 0x03.

String exp = "$.device_flags.bitGet(offset: 16, size: 8) == x'03'";

bit_get_int

bit_get_int(bit_offset, bit_size, signed, bin)
Description

Returns the bit_size bits starting at bit_offset as an integer, read as signed when signed is true and as unsigned when it is false.

Arguments
NameTypeDescription
bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

signedboolean literal

Whether to read the region as a signed integer.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
integer
Introduced
5.2.0.4
Example

Filter where the signed 8-bit integer at bit offset 32 in device_flags equals 5 (fifth byte 0x05).

String exp = "$.device_flags.bitGetInt(offset: 32, size: 8, signed: true) == 5";

bit_lscan

bit_lscan(bit_offset, bit_size, value, bin)
Description

Returns the position of the first bit equal to value, scanning the bit_size bits at bit_offset from left to right. The position is relative to bit_offset, so 0 is the bit at bit_offset itself. Returns -1 when no bit matches.

Arguments
NameTypeDescription
bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

valueboolean

Bit value to look for: true finds a bit set to 1, false finds a bit set to 0.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
integer
Introduced
5.2.0.4
Example

Filter where a left scan for a set bit in the fifth byte of device_flags (bits 32–39) returns index 5.

String exp = "$.device_flags.bitLscan(offset: 32, size: 8, value: true) == 5";

bit_rscan

bit_rscan(bit_offset, bit_size, value, bin)
Description

Returns the position of the last bit equal to value, scanning the bit_size bits at bit_offset from right to left. The position is relative to bit_offset. Returns -1 when no bit matches.

Arguments
NameTypeDescription
bit_offsetinteger

Offset in bits from the start of the blob to the first bit of the region.

bit_sizeinteger

Width of the region in bits.

valueboolean

Bit value to look for: true finds a bit set to 1, false finds a bit set to 0.

binblob

Blob bin to operate on, or any expression that evaluates to a blob.

Returns
integer
Introduced
5.2.0.4
Example

Filter where a right scan for a set bit in the fifth byte of device_flags returns index 7.

String exp = "$.device_flags.bitRscan(offset: 32, size: 8, value: true) == 7";