---
title: "Blob/bytes"
description: "Explore Aerospike Blob/bytes data types, including detailed bitwise modify and read operations with code samples."
---

# Blob/bytes

> For the complete documentation index see: [llms.txt](https://aerospike.com/docs/llms.txt)
> 
> All documentation pages available in markdown.

Aerospike supports two main types of Blob/bytes bins. There are general purpose Blob bins and language-specific Blob bins.

The general purpose Blob bins are byte arrays of a specific size. Any binary data of any type can be stored in a Blob bin. They also support [Bitwise Operations](#bitwise-operations).

Language-specific Blob bins are only accessible from the client library language that wrote them. These are useful for serializing objects in that language to the Aerospike database.

## Bitwise Operations

Aerospike supports a rich set of bitwise operations which can be used on the Blob data type. These operations allow an application to manipulate a large Blob bin on the server without needing to pull the entire blob to the client, saving bandwidth.

The examples throughout this page use a 5-byte Blob bin called `flags` with the value `[0x01, 0x42, 0xB0, 0x04, 0x05]`, representing an IoT device configuration record. Bits 0-15 store a 16-bit device ID, byte 2 holds capability flags, and the remaining bytes hold status and version data.

| Name | Value | Description |
| :-- | :-- | :-- |
| `create_only` | `0x01` | Disallow updating an existing value of this bin. |
| `update_only` | `0x02` | Disallow creation of a new Blob bin. |
| `no_fail` | `0x04` | If the operation should fail, continue as if it had succeeded. |
| `partial` | `0X08` | If the number of bytes from the offset to the end of the existing Blob bin is less than the specified number of bytes, then only apply the operations from the offset to the end. |

## Modify operations

#### `add`

`update_only` `no_fail`

```python
add(policy, bin_name, bit_offset, n_bits, value, signed, action)
```

Description: Treat the `n_bits` bits beginning at `offset` in the Blob bin as an `n_bits` bit integer and add the integer `value` to it - the integer `value` will be converted to an `n_bits` bit integer. By default, fail if the result overflows. Integers in the Blob bin are stored and interpreted as big-endian integers.

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `policy` | `library_specific` | 
Bitwise modify policy.

 |
| `bin_name` | `string` | 

Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first bit to apply operation.

 |
| `n_bits` | `integer` | 

Size of the integer in bits (maximum of 64 bits).

 |
| `value` | `integer` | 

The unsigned integer value to be added.

 |
| `signed` | `boolean` | 

Read the integer from the Blob bin as signed (true) or unsigned (false).

 |
| `action` | `client_specific` | 

How to handle integer overflow.

-   (Default) Fail transaction on overflow.
-   Set maximum value on overflow.
-   Wrap the value from the min value.



 |

Returns: `none`

Examples: Increment the device ID

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. Bits 0-15 hold a 16-bit device ID stored as a big-endian unsigned integer. The `add` operation increments this integer in place on the server, avoiding a read-modify-write round trip.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, the first 16 bits represent `0x0142` (decimal 322). Calling `add("flags", 0, 16, 10)` adds 10 to get 332 (`0x014C`). The result is `flags = [0x01, 0x4C, 0xB0, 0x04, 0x05]`.

Code sample: -   [Java](#tab-panel-517)
-   [Python](#tab-panel-518)
-   [C](#tab-panel-519)
-   [Go](#tab-panel-520)
-   [C#](#tab-panel-521)
-   [Node.js](#tab-panel-522)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) + 10 = 0x014C (332)

import com.aerospike.client.operation.BitOverflowAction;

Record record = client.operate(null, key,

    BitOperation.add(BitPolicy.Default, "flags", 0, 16,

        10, false, BitOverflowAction.FAIL));

// flags = [0x01, 0x4C, 0xB0, 0x04, 0x05]
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

# bits 0-15: 0x0142 (322) + 10 = 0x014C (332)

import aerospike

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_add("flags", 0, 16, 10, False,

    aerospike.BIT_OVERFLOW_FAIL)]

client.operate(key, ops)

# flags = [0x01, 0x4C, 0xB0, 0x04, 0x05]
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) + 10 = 0x014C (332)

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_add(&ops, "flags", NULL, NULL, 0, 16,

    10, false, AS_BIT_OVERFLOW_FAIL);

aerospike_key_operate(&as, &err, NULL, &key, &ops, NULL);

// flags = [0x01, 0x4C, 0xB0, 0x04, 0x05]

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) + 10 = 0x014C (332)

record, err := client.Operate(nil, key,

    as.BitAddOp(as.DefaultBitPolicy(), "flags", 0, 16,

        10, false, as.BitOverflowActionFail))

// flags = [0x01, 0x4C, 0xB0, 0x04, 0x05]
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) + 10 = 0x014C (332)

Record record = client.Operate(null, key,

    BitOperation.Add(BitPolicy.Default, "flags", 0, 16,

        10, false, BitOverflowAction.FAIL));

// flags = [0x01, 0x4C, 0xB0, 0x04, 0x05]
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) + 10 = 0x014C (332)

const bw = Aerospike.bitwise

const ops = [bw.add('flags', 0, 16, 10, false)]

await client.operate(key, ops)

// flags = [0x01, 0x4C, 0xB0, 0x04, 0x05]
```

---

#### `and`

`update_only` `no_fail` `partial`

```python
and(policy, bin_name, bit_offset, n_bits, value)
```

Description: Bitwise AND `n_bits` of the `buffer` with the leading `n_bits` of the Blob bin starting from the specified `offset` (in bits).

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `policy` | `library_specific` | 
Bitwise modify policy.

 |
| `bin_name` | `string` | 

Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first bit to apply operation.

 |
| `n_bits` | `integer` | 

Number of bits to apply operation to.

 |
| `value` | `bytes` | 

Buffer containing at least `n_bits` bits.

 |

Returns: `none`

Examples: Mask the lower nibble of a capability byte

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. Byte 0 (bits 0-7) holds a device type identifier. ANDing with a mask clears specific bits while preserving others.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, calling `and("flags", 0, 8, [0xF0])` masks the first byte with `0xF0` (`1111 0000`). This clears the lower four bits: `0x01` AND `0xF0` = `0x00`. The result is `flags = [0x00, 0x42, 0xB0, 0x04, 0x05]`.

Code sample: -   [Java](#tab-panel-523)
-   [Python](#tab-panel-524)
-   [C](#tab-panel-525)
-   [Go](#tab-panel-526)
-   [C#](#tab-panel-527)
-   [Node.js](#tab-panel-528)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.operate(null, key,

    BitOperation.and(BitPolicy.Default, "flags", 0, 8,

        new byte[] {(byte) 0xF0}));

// flags = [0x00, 0x42, 0xB0, 0x04, 0x05]
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_and("flags", 0, 8, 1, bytearray([0xF0]))]

client.operate(key, ops)

# flags = [0x00, 0x42, 0xB0, 0x04, 0x05]
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

uint8_t val[] = {0xF0};

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_and(&ops, "flags", NULL, NULL, 0, 8, 1, val);

aerospike_key_operate(&as, &err, NULL, &key, &ops, NULL);

// flags = [0x00, 0x42, 0xB0, 0x04, 0x05]

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

record, err := client.Operate(nil, key,

    as.BitAndOp(as.DefaultBitPolicy(), "flags", 0, 8,

        []byte{0xF0}))

// flags = [0x00, 0x42, 0xB0, 0x04, 0x05]
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.Operate(null, key,

    BitOperation.And(BitPolicy.Default, "flags", 0, 8,

        new byte[] { 0xF0 }));

// flags = [0x00, 0x42, 0xB0, 0x04, 0x05]
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

const bw = Aerospike.bitwise

const ops = [bw.and('flags', 0, 8, Buffer.from([0xF0]))]

await client.operate(key, ops)

// flags = [0x00, 0x42, 0xB0, 0x04, 0x05]
```

---

#### `insert`

`create_only` `update_only` `no_fail`

```python
insert(policy, bin_name, byte_offset, value)
```

Description: Inserts bytes at the specified `byte_offset` with the contents of `buffer`.

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `policy` | `library_specific` | 
Bitwise modify policy.

 |
| `bin_name` | `string` | 

Name of bin.

 |
| `byte_offset` | `integer` | 

Offset to location of insertion.

 |
| `value` | `bytes` | 

Bytes to be inserted.

 |

Returns: `none`

Examples: Insert extra status bytes

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. To add two new bytes of status data in the middle of the blob, use `insert` at a byte offset. Existing bytes after the offset shift to the right, growing the blob.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]` (5 bytes), calling `insert("flags", 2, [0xAA, 0xBB])` inserts `0xAA 0xBB` at byte offset 2. The result is `flags = [0x01, 0x42, 0xAA, 0xBB, 0xB0, 0x04, 0x05]` (7 bytes).

Code sample: -   [Java](#tab-panel-529)
-   [Python](#tab-panel-530)
-   [C](#tab-panel-531)
-   [Go](#tab-panel-532)
-   [C#](#tab-panel-533)
-   [Node.js](#tab-panel-534)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

Record record = client.operate(null, key,

    BitOperation.insert(BitPolicy.Default, "flags", 2,

        new byte[] {(byte) 0xAA, (byte) 0xBB}));

// flags = [0x01, 0x42, 0xAA, 0xBB, 0xB0, 0x04, 0x05] (7 bytes)
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_insert("flags", 2, 2, bytearray([0xAA, 0xBB]))]

client.operate(key, ops)

# flags = [0x01, 0x42, 0xAA, 0xBB, 0xB0, 0x04, 0x05] (7 bytes)
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

uint8_t val[] = {0xAA, 0xBB};

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_insert(&ops, "flags", NULL, NULL, 2, 2, val);

aerospike_key_operate(&as, &err, NULL, &key, &ops, NULL);

// flags = [0x01, 0x42, 0xAA, 0xBB, 0xB0, 0x04, 0x05] (7 bytes)

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

record, err := client.Operate(nil, key,

    as.BitInsertOp(as.DefaultBitPolicy(), "flags", 2,

        []byte{0xAA, 0xBB}))

// flags = [0x01, 0x42, 0xAA, 0xBB, 0xB0, 0x04, 0x05] (7 bytes)
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

Record record = client.Operate(null, key,

    BitOperation.Insert(BitPolicy.Default, "flags", 2,

        new byte[] { 0xAA, 0xBB }));

// flags = [0x01, 0x42, 0xAA, 0xBB, 0xB0, 0x04, 0x05] (7 bytes)
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

const bw = Aerospike.bitwise

const ops = [bw.insert('flags', 2, Buffer.from([0xAA, 0xBB]))]

await client.operate(key, ops)

// flags = [0x01, 0x42, 0xAA, 0xBB, 0xB0, 0x04, 0x05] (7 bytes)
```

---

#### `lshift`

`update_only` `no_fail` `partial`

```python
lshift(policy, bin_name, bit_offset, n_bits, shift)
```

Description: Bitwise shift `n_bits` bits of the Blob bin `n_bits` to the left starting at the specified `offset` (in bits).

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `policy` | `library_specific` | 
Bitwise modify policy.

 |
| `bin_name` | `string` | 

Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first bit to apply operation.

 |
| `n_bits` | `integer` | 

Number of bits to apply operation to.

 |
| `shift` | `integer` | 

Number of bits to shift.

 |

Returns: `none`

Examples: Shift the device ID left

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. Bits 0-15 hold a 16-bit device ID. Left-shifting multiplies the value by a power of 2. Bits shifted out on the left are lost, and zeros fill in on the right.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, the first two bytes are `0x0142` (decimal 322). Calling `lshift("flags", 0, 16, 3)` shifts the 16-bit field left by 3: `0x0142` << 3 = `0x0A10` (decimal 2576). The result is `flags = [0x0A, 0x10, 0xB0, 0x04, 0x05]`.

Code sample: -   [Java](#tab-panel-535)
-   [Python](#tab-panel-536)
-   [C](#tab-panel-537)
-   [Go](#tab-panel-538)
-   [C#](#tab-panel-539)
-   [Node.js](#tab-panel-540)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) << 3 = 0x0A10 (2576)

Record record = client.operate(null, key,

    BitOperation.lshift(BitPolicy.Default, "flags", 0, 16, 3));

// flags = [0x0A, 0x10, 0xB0, 0x04, 0x05]
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

# bits 0-15: 0x0142 (322) << 3 = 0x0A10 (2576)

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_lshift("flags", 0, 16, 3)]

client.operate(key, ops)

# flags = [0x0A, 0x10, 0xB0, 0x04, 0x05]
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) << 3 = 0x0A10 (2576)

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_lshift(&ops, "flags", NULL, NULL, 0, 16, 3);

aerospike_key_operate(&as, &err, NULL, &key, &ops, NULL);

// flags = [0x0A, 0x10, 0xB0, 0x04, 0x05]

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) << 3 = 0x0A10 (2576)

record, err := client.Operate(nil, key,

    as.BitLShiftOp(as.DefaultBitPolicy(), "flags", 0, 16, 3))

// flags = [0x0A, 0x10, 0xB0, 0x04, 0x05]
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) << 3 = 0x0A10 (2576)

Record record = client.Operate(null, key,

    BitOperation.Lshift(BitPolicy.Default, "flags", 0, 16, 3));

// flags = [0x0A, 0x10, 0xB0, 0x04, 0x05]
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) << 3 = 0x0A10 (2576)

const bw = Aerospike.bitwise

const ops = [bw.lshift('flags', 0, 16, 3)]

await client.operate(key, ops)

// flags = [0x0A, 0x10, 0xB0, 0x04, 0x05]
```

---

#### `not`

`update_only` `no_fail` `partial`

```python
not(policy, bin_name, bit_offset, n_bits)
```

Description: Bitwise NOT `n_bits` of the Blob bin starting from the specified `offset` (in bits).

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `policy` | `library_specific` | 
Bitwise modify policy.

 |
| `bin_name` | `string` | 

Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first bit to apply operation.

 |
| `n_bits` | `integer` | 

Number of bits to apply operation to.

 |

Returns: `none`

Examples: Invert a device type byte

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. NOT inverts every bit in the specified range, flipping `0`s to `1`s and `1`s to `0`s.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, calling `not("flags", 0, 8)` inverts the first byte: NOT `0x01` (`0000 0001`) = `0xFE` (`1111 1110`). The result is `flags = [0xFE, 0x42, 0xB0, 0x04, 0x05]`.

Code sample: -   [Java](#tab-panel-541)
-   [Python](#tab-panel-542)
-   [C](#tab-panel-543)
-   [Go](#tab-panel-544)
-   [C#](#tab-panel-545)
-   [Node.js](#tab-panel-546)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.operate(null, key,

    BitOperation.not(BitPolicy.Default, "flags", 0, 8));

// flags = [0xFE, 0x42, 0xB0, 0x04, 0x05]
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_not("flags", 0, 8)]

client.operate(key, ops)

# flags = [0xFE, 0x42, 0xB0, 0x04, 0x05]
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_not(&ops, "flags", NULL, NULL, 0, 8);

aerospike_key_operate(&as, &err, NULL, &key, &ops, NULL);

// flags = [0xFE, 0x42, 0xB0, 0x04, 0x05]

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

record, err := client.Operate(nil, key,

    as.BitNotOp(as.DefaultBitPolicy(), "flags", 0, 8))

// flags = [0xFE, 0x42, 0xB0, 0x04, 0x05]
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.Operate(null, key,

    BitOperation.Not(BitPolicy.Default, "flags", 0, 8));

// flags = [0xFE, 0x42, 0xB0, 0x04, 0x05]
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

const bw = Aerospike.bitwise

const ops = [bw.not('flags', 0, 8)]

await client.operate(key, ops)

// flags = [0xFE, 0x42, 0xB0, 0x04, 0x05]
```

---

#### `or`

`update_only` `no_fail` `partial`

```python
or(policy, bin_name, bit_offset, n_bits, value)
```

Description: Bitwise OR `n_bits` of the `buffer` with the leading `n_bits` of the Blob bin starting from the specified `offset` (in bits).

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `policy` | `library_specific` | 
Bitwise modify policy.

 |
| `bin_name` | `string` | 

Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first bit to apply operation.

 |
| `n_bits` | `integer` | 

Number of bits to apply operation to.

 |
| `value` | `bytes` | 

Buffer containing at least `n_bits` bits.

 |

Returns: `none`

Examples: Enable a feature flag

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. Byte 0 (bits 0-7) holds a device type identifier. ORing with a value sets specific bits without affecting others.

Given `flags = [0x00, 0x42, 0xB0, 0x04, 0x05]`, calling `or("flags", 0, 8, [0x01])` sets the lowest bit in the first byte: `0x00` OR `0x01` = `0x01`. The result is `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`.

Code sample: -   [Java](#tab-panel-547)
-   [Python](#tab-panel-548)
-   [C](#tab-panel-549)
-   [Go](#tab-panel-550)
-   [C#](#tab-panel-551)
-   [Node.js](#tab-panel-552)

```java
// flags = [0x00, 0x42, 0xB0, 0x04, 0x05]

Record record = client.operate(null, key,

    BitOperation.or(BitPolicy.Default, "flags", 0, 8,

        new byte[] {0x01}));

// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]
```

```python
# flags = [0x00, 0x42, 0xB0, 0x04, 0x05]

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_or("flags", 0, 8, 1, bytearray([0x01]))]

client.operate(key, ops)

# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]
```

```c
// flags = [0x00, 0x42, 0xB0, 0x04, 0x05]

uint8_t val[] = {0x01};

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_or(&ops, "flags", NULL, NULL, 0, 8, 1, val);

aerospike_key_operate(&as, &err, NULL, &key, &ops, NULL);

// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

as_operations_destroy(&ops);
```

```go
// flags = [0x00, 0x42, 0xB0, 0x04, 0x05]

record, err := client.Operate(nil, key,

    as.BitOrOp(as.DefaultBitPolicy(), "flags", 0, 8,

        []byte{0x01}))

// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]
```

```csharp
// flags = [0x00, 0x42, 0xB0, 0x04, 0x05]

Record record = client.Operate(null, key,

    BitOperation.Or(BitPolicy.Default, "flags", 0, 8,

        new byte[] { 0x01 }));

// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]
```

```javascript
// flags = [0x00, 0x42, 0xB0, 0x04, 0x05]

const bw = Aerospike.bitwise

const ops = [bw.or('flags', 0, 8, Buffer.from([0x01]))]

await client.operate(key, ops)

// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]
```

---

#### `remove`

`update_only` `no_fail` `partial`

```python
remove(policy, bin_name, byte_offset, n_bytes)
```

Description: Remove `n_bytes` bytes beginning at the specified `byte_offset`.

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `policy` | `library_specific` | 
Bitwise modify policy.

 |
| `bin_name` | `string` | 

Name of bin.

 |
| `byte_offset` | `integer` | 

Offset to location of removal.

 |
| `n_bytes` | `integer` | 

Number of bytes to remove.

 |

Returns: `none`

Examples: Remove bytes from the middle

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. To remove bytes from the middle of the blob, use `remove` at a byte offset with a count. Bytes after the removed range shift left, shrinking the blob.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]` (5 bytes), calling `remove("flags", 2, 2)` removes 2 bytes starting at byte offset 2 (bytes `0xB0` and `0x04`). The result is `flags = [0x01, 0x42, 0x05]` (3 bytes).

Code sample: -   [Java](#tab-panel-553)
-   [Python](#tab-panel-554)
-   [C](#tab-panel-555)
-   [Go](#tab-panel-556)
-   [C#](#tab-panel-557)
-   [Node.js](#tab-panel-558)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

Record record = client.operate(null, key,

    BitOperation.remove(BitPolicy.Default, "flags", 2, 2));

// flags = [0x01, 0x42, 0x05] (3 bytes)
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_remove("flags", 2, 2)]

client.operate(key, ops)

# flags = [0x01, 0x42, 0x05] (3 bytes)
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_remove(&ops, "flags", NULL, NULL, 2, 2);

aerospike_key_operate(&as, &err, NULL, &key, &ops, NULL);

// flags = [0x01, 0x42, 0x05] (3 bytes)

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

record, err := client.Operate(nil, key,

    as.BitRemoveOp(as.DefaultBitPolicy(), "flags", 2, 2))

// flags = [0x01, 0x42, 0x05] (3 bytes)
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

Record record = client.Operate(null, key,

    BitOperation.Remove(BitPolicy.Default, "flags", 2, 2));

// flags = [0x01, 0x42, 0x05] (3 bytes)
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

const bw = Aerospike.bitwise

const ops = [bw.remove('flags', 2, 2)]

await client.operate(key, ops)

// flags = [0x01, 0x42, 0x05] (3 bytes)
```

---

#### `resize`

`create_only` `update_only` `no_fail`

```python
resize(policy, bin_name, n_bytes, resize_flags)
```

Description: Specify the size of the Blob bin to be `n_bytes`. This operation may (by default) create a new Bytes bin or extend or trim an existing Byte bin to the specified size of `n_bytes`. By default the `resize` operation will extend or trim from the end of the Blob bin.

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `policy` | `library_specific` | 
Bitwise modify policy.

 |
| `bin_name` | `string` | 

Name of bin.

 |
| `n_bytes` | `integer` | 

Number of bytes to resize to.

 |
| `resize_flags` | `integer` | 

| Name | Value | Description |
| --- | --- | --- |
| from\_front | 0x01 | Extend or trim the Blob bin from the beginning instead of the end. |
| grow\_only | 0x02 | Disallow trimming existing objects. |
| shrink\_only | 0x04 | Disallow extending existing objects. |

 |

Returns: `none`

Examples: Extend the blob to accommodate new fields

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. When the device schema grows to include additional fields, `resize` extends the blob to the new size. New bytes are initialized to zero and appended at the end by default.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]` (5 bytes), calling `resize("flags", 10, 0)` extends the blob to 10 bytes. The result is `flags = [0x01, 0x42, 0xB0, 0x04, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00]`.

Code sample: -   [Java](#tab-panel-559)
-   [Python](#tab-panel-560)
-   [C](#tab-panel-561)
-   [Go](#tab-panel-562)
-   [C#](#tab-panel-563)
-   [Node.js](#tab-panel-564)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

Record record = client.operate(null, key,

    BitOperation.resize(BitPolicy.Default, "flags", 10, 0));

// flags = [0x01, 0x42, 0xB0, 0x04, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00] (10 bytes)
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_resize("flags", 10)]

client.operate(key, ops)

# flags = [0x01, 0x42, 0xB0, 0x04, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00] (10 bytes)
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_resize(&ops, "flags", NULL, NULL, 10,

    AS_BIT_RESIZE_DEFAULT);

aerospike_key_operate(&as, &err, NULL, &key, &ops, NULL);

// flags now 10 bytes, padded with zeros at the end

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

record, err := client.Operate(nil, key,

    as.BitResizeOp(as.DefaultBitPolicy(), "flags", 10,

        as.BitResizeFlagsDefault))

// flags now 10 bytes, padded with zeros at the end
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

Record record = client.Operate(null, key,

    BitOperation.Resize(BitPolicy.Default, "flags", 10,

        BitResizeFlags.DEFAULT));

// flags now 10 bytes, padded with zeros at the end
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05] (5 bytes)

const bw = Aerospike.bitwise

const ops = [bw.resize('flags', 10)]

await client.operate(key, ops)

// flags now 10 bytes, padded with zeros at the end
```

---

#### `rshift`

`update_only` `no_fail` `partial`

```python
rshift(policy, bin_name, bit_offset, n_bits, shift)
```

Description: Bitwise shift `n_bits` bits of the Blob bin `n_bits` to the right starting at the specified `offset` (in bits).

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `policy` | `library_specific` | 
Bitwise modify policy.

 |
| `bin_name` | `string` | 

Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first bit to apply operation.

 |
| `n_bits` | `integer` | 

Number of bits to apply operation to.

 |
| `shift` | `integer` | 

Number of bits to shift.

 |

Returns: `none`

Examples: Shift the device ID right

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. Bits 0-15 hold a 16-bit device ID. Right-shifting divides the value by a power of 2. Bits shifted out on the right are lost, and zeros fill in on the left.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, the first two bytes are `0x0142` (decimal 322). Calling `rshift("flags", 0, 16, 3)` shifts the 16-bit field right by 3: `0x0142` >> 3 = `0x0028` (decimal 40). The result is `flags = [0x00, 0x28, 0xB0, 0x04, 0x05]`.

Code sample: -   [Java](#tab-panel-565)
-   [Python](#tab-panel-566)
-   [C](#tab-panel-567)
-   [Go](#tab-panel-568)
-   [C#](#tab-panel-569)
-   [Node.js](#tab-panel-570)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) >> 3 = 0x0028 (40)

Record record = client.operate(null, key,

    BitOperation.rshift(BitPolicy.Default, "flags", 0, 16, 3));

// flags = [0x00, 0x28, 0xB0, 0x04, 0x05]
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

# bits 0-15: 0x0142 (322) >> 3 = 0x0028 (40)

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_rshift("flags", 0, 16, 3)]

client.operate(key, ops)

# flags = [0x00, 0x28, 0xB0, 0x04, 0x05]
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) >> 3 = 0x0028 (40)

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_rshift(&ops, "flags", NULL, NULL, 0, 16, 3);

aerospike_key_operate(&as, &err, NULL, &key, &ops, NULL);

// flags = [0x00, 0x28, 0xB0, 0x04, 0x05]

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) >> 3 = 0x0028 (40)

record, err := client.Operate(nil, key,

    as.BitRShiftOp(as.DefaultBitPolicy(), "flags", 0, 16, 3))

// flags = [0x00, 0x28, 0xB0, 0x04, 0x05]
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) >> 3 = 0x0028 (40)

Record record = client.Operate(null, key,

    BitOperation.Rshift(BitPolicy.Default, "flags", 0, 16, 3));

// flags = [0x00, 0x28, 0xB0, 0x04, 0x05]
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) >> 3 = 0x0028 (40)

const bw = Aerospike.bitwise

const ops = [bw.rshift('flags', 0, 16, 3)]

await client.operate(key, ops)

// flags = [0x00, 0x28, 0xB0, 0x04, 0x05]
```

---

#### `set`

`update_only` `no_fail` `partial`

```python
set(policy, bin_name, bit_offset, n_bits, value)
```

Description: Overwrites `n_bits` bits at the specified `offset` (in bits) with the first `n_bits` of `value`.

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `policy` | `library_specific` | 
Bitwise modify policy.

 |
| `bin_name` | `string` | 

Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first bit to overwrite.

 |
| `n_bits` | `integer` | 

Number of bits to overwrite.

 |
| `value` | `bytes` | 

Buffer containing at least `n_bits` bits to be written. Bits are taken in order from the beginning of the buffer.

 |

Returns: `none`

Examples: Set all capability flags at once

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. Byte 2 (bits 16-23) holds capability flags. To enable all capabilities at once, overwrite that byte with `0xFF`.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, calling `set("flags", 16, 8, [0xFF])` replaces byte 2 with `0xFF` (`1111 1111`), turning on every capability bit. The result is `flags = [0x01, 0x42, 0xFF, 0x04, 0x05]`.

Code sample: -   [Java](#tab-panel-571)
-   [Python](#tab-panel-572)
-   [C](#tab-panel-573)
-   [Go](#tab-panel-574)
-   [C#](#tab-panel-575)
-   [Node.js](#tab-panel-576)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.operate(null, key,

    BitOperation.set(BitPolicy.Default, "flags", 16, 8,

        new byte[] {(byte) 0xFF}));

// flags = [0x01, 0x42, 0xFF, 0x04, 0x05]
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_set("flags", 16, 8, 1, bytearray([0xFF]))]

client.operate(key, ops)

# flags = [0x01, 0x42, 0xFF, 0x04, 0x05]
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

uint8_t val[] = {0xFF};

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_set(&ops, "flags", NULL, NULL, 16, 8, 1, val);

aerospike_key_operate(&as, &err, NULL, &key, &ops, NULL);

// flags = [0x01, 0x42, 0xFF, 0x04, 0x05]

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

record, err := client.Operate(nil, key,

    as.BitSetOp(as.DefaultBitPolicy(), "flags", 16, 8,

        []byte{0xFF}))

// flags = [0x01, 0x42, 0xFF, 0x04, 0x05]
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.Operate(null, key,

    BitOperation.Set(BitPolicy.Default, "flags", 16, 8,

        new byte[] { 0xFF }));

// flags = [0x01, 0x42, 0xFF, 0x04, 0x05]
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

const bw = Aerospike.bitwise

const ops = [bw.set('flags', 16, 8, Buffer.from([0xFF]))]

await client.operate(key, ops)

// flags = [0x01, 0x42, 0xFF, 0x04, 0x05]
```

---

#### `set_int`

`update_only` `no_fail`

```python
set_int(policy, bin_name, bit_offset, n_bits, value)
```

Description: Overwrite `n_bits` bits at offset `offset` with `uint64` converted to an `n_bits` bit big\_endian integer.

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `policy` | `library_specific` | 
Bitwise modify policy.

 |
| `bin_name` | `string` | 

Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first bit to apply operation.

 |
| `n_bits` | `integer` | 

Size of the integer in bits (maximum of 64 bits).

 |
| `value` | `integer` | 

The unsigned integer value to be set.

 |

Returns: `none`

Examples: Set the device ID to a known value

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. Bits 0-15 hold a 16-bit device ID. Instead of computing the raw bytes for a new ID, `set_int` writes an integer directly.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, calling `set_int("flags", 0, 16, 500)` writes `500` (`0x01F4`) into the first 16 bits as a big-endian integer. The result is `flags = [0x01, 0xF4, 0xB0, 0x04, 0x05]`.

Code sample: -   [Java](#tab-panel-577)
-   [Python](#tab-panel-578)
-   [C](#tab-panel-579)
-   [Go](#tab-panel-580)
-   [C#](#tab-panel-581)
-   [Node.js](#tab-panel-582)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// 500 = 0x01F4

Record record = client.operate(null, key,

    BitOperation.setInt(BitPolicy.Default, "flags", 0, 16, 500));

// flags = [0x01, 0xF4, 0xB0, 0x04, 0x05]
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

# 500 = 0x01F4

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_set_int("flags", 0, 16, 500)]

client.operate(key, ops)

# flags = [0x01, 0xF4, 0xB0, 0x04, 0x05]
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// 500 = 0x01F4

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_set_int(&ops, "flags", NULL, NULL, 0, 16, 500);

aerospike_key_operate(&as, &err, NULL, &key, &ops, NULL);

// flags = [0x01, 0xF4, 0xB0, 0x04, 0x05]

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// 500 = 0x01F4

record, err := client.Operate(nil, key,

    as.BitSetIntOp(as.DefaultBitPolicy(), "flags", 0, 16, 500))

// flags = [0x01, 0xF4, 0xB0, 0x04, 0x05]
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// 500 = 0x01F4

Record record = client.Operate(null, key,

    BitOperation.SetInt(BitPolicy.Default, "flags", 0, 16, 500));

// flags = [0x01, 0xF4, 0xB0, 0x04, 0x05]
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// 500 = 0x01F4

const bw = Aerospike.bitwise

// Pass an integer (instead of a Buffer) to set() for set_int

const ops = [bw.set('flags', 0, 16, 500)]

await client.operate(key, ops)

// flags = [0x01, 0xF4, 0xB0, 0x04, 0x05]
```

---

#### `subtract`

`update_only` `no_fail`

```python
subtract(policy, bin_name, bit_offset, n_bits, value, signed, action)
```

Description: Treat the `n_bits` bits beginning at `offset` in the Blob bin as an `n_bits` bit integer and subtract the integer `uint64` from it - the integer `uint64` will be converted to an `n_bits` bit integer. By default, fail if the result underflows. Integers in the Blob bin are stored and interpreted as big-endian integers.

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `policy` | `library_specific` | 
Bitwise modify policy.

 |
| `bin_name` | `string` | 

Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first bit to apply operation.

 |
| `n_bits` | `integer` | 

Size of the integer in bits (maximum of 64 bits).

 |
| `value` | `integer` | 

The unsigned integer value to be subtracted.

 |
| `signed` | `boolean` | 

Read the integer from the Blob bin as signed (true) or unsigned (false).

 |
| `action` | `client_specific` | 

How to handle integer underflow.

-   (Default) Fail transaction on underflow.
-   Set minimum value on underflow.
-   Wrap the value from the max value.



 |

Returns: `none`

Examples: Decrement the device ID

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. Bits 0-15 hold a 16-bit device ID stored as a big-endian unsigned integer. The `subtract` operation decrements this integer in place on the server.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, the first 16 bits represent `0x0142` (decimal 322). Calling `subtract("flags", 0, 16, 5)` subtracts 5 to get 317 (`0x013D`). The result is `flags = [0x01, 0x3D, 0xB0, 0x04, 0x05]`.

Code sample: -   [Java](#tab-panel-583)
-   [Python](#tab-panel-584)
-   [C](#tab-panel-585)
-   [Go](#tab-panel-586)
-   [C#](#tab-panel-587)
-   [Node.js](#tab-panel-588)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) - 5 = 0x013D (317)

import com.aerospike.client.operation.BitOverflowAction;

Record record = client.operate(null, key,

    BitOperation.subtract(BitPolicy.Default, "flags", 0, 16,

        5, false, BitOverflowAction.FAIL));

// flags = [0x01, 0x3D, 0xB0, 0x04, 0x05]
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

# bits 0-15: 0x0142 (322) - 5 = 0x013D (317)

import aerospike

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_subtract("flags", 0, 16, 5, False,

    aerospike.BIT_OVERFLOW_FAIL)]

client.operate(key, ops)

# flags = [0x01, 0x3D, 0xB0, 0x04, 0x05]
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) - 5 = 0x013D (317)

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_subtract(&ops, "flags", NULL, NULL, 0, 16,

    5, false, AS_BIT_OVERFLOW_FAIL);

aerospike_key_operate(&as, &err, NULL, &key, &ops, NULL);

// flags = [0x01, 0x3D, 0xB0, 0x04, 0x05]

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) - 5 = 0x013D (317)

record, err := client.Operate(nil, key,

    as.BitSubtractOp(as.DefaultBitPolicy(), "flags", 0, 16,

        5, false, as.BitOverflowActionFail))

// flags = [0x01, 0x3D, 0xB0, 0x04, 0x05]
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) - 5 = 0x013D (317)

Record record = client.Operate(null, key,

    BitOperation.Subtract(BitPolicy.Default, "flags", 0, 16,

        5, false, BitOverflowAction.FAIL));

// flags = [0x01, 0x3D, 0xB0, 0x04, 0x05]
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0x0142 (322) - 5 = 0x013D (317)

const bw = Aerospike.bitwise

const ops = [bw.subtract('flags', 0, 16, 5, false)]

await client.operate(key, ops)

// flags = [0x01, 0x3D, 0xB0, 0x04, 0x05]
```

---

#### `xor`

`update_only` `no_fail` `partial`

```python
xor(policy, bin_name, bit_offset, n_bits, value)
```

Description: Bitwise XOR `n_bits` of the `buffer` with the leading `n_bits` of the Blob bin starting from the specified `offset` (in bits).

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `policy` | `library_specific` | 
Bitwise modify policy.

 |
| `bin_name` | `string` | 

Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first bit to apply operation.

 |
| `n_bits` | `integer` | 

Number of bits to apply operation to.

 |
| `value` | `bytes` | 

Buffer containing at least `n_bits` bits.

 |

Returns: `none`

Examples: Toggle the lower nibble of a byte

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. XOR flips the bits where the mask has `1`s, leaving other bits unchanged. This is useful for toggling specific flags.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, calling `xor("flags", 0, 8, [0x0F])` flips the lower four bits of the first byte: `0x01` XOR `0x0F` = `0x0E` (`0000 0001` XOR `0000 1111` = `0000 1110`). The result is `flags = [0x0E, 0x42, 0xB0, 0x04, 0x05]`.

Code sample: -   [Java](#tab-panel-589)
-   [Python](#tab-panel-590)
-   [C](#tab-panel-591)
-   [Go](#tab-panel-592)
-   [C#](#tab-panel-593)
-   [Node.js](#tab-panel-594)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.operate(null, key,

    BitOperation.xor(BitPolicy.Default, "flags", 0, 8,

        new byte[] {0x0F}));

// flags = [0x0E, 0x42, 0xB0, 0x04, 0x05]
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_xor("flags", 0, 8, 1, bytearray([0x0F]))]

client.operate(key, ops)

# flags = [0x0E, 0x42, 0xB0, 0x04, 0x05]
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

uint8_t val[] = {0x0F};

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_xor(&ops, "flags", NULL, NULL, 0, 8, 1, val);

aerospike_key_operate(&as, &err, NULL, &key, &ops, NULL);

// flags = [0x0E, 0x42, 0xB0, 0x04, 0x05]

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

record, err := client.Operate(nil, key,

    as.BitXorOp(as.DefaultBitPolicy(), "flags", 0, 8,

        []byte{0x0F}))

// flags = [0x0E, 0x42, 0xB0, 0x04, 0x05]
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.Operate(null, key,

    BitOperation.Xor(BitPolicy.Default, "flags", 0, 8,

        new byte[] { 0x0F }));

// flags = [0x0E, 0x42, 0xB0, 0x04, 0x05]
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

const bw = Aerospike.bitwise

const ops = [bw.xor('flags', 0, 8, Buffer.from([0x0F]))]

await client.operate(key, ops)

// flags = [0x0E, 0x42, 0xB0, 0x04, 0x05]
```

---

## Read operations

#### `count`

`update_only` `no_fail`

```python
count(bin_name, bit_offset, n_bits)
```

Description: Count the number of bits set to 1 in the `n_bits` beginning at `offset`.

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `bin_name` | `string` | 
Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first bit to be checked.

 |
| `n_bits` | `integer` | 

Number of bits to check.

 |

Returns: `bytes`

Examples: Count enabled capabilities

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. Bits 0-15 hold a 16-bit device ID. To find out how many bits are set in the device ID field, use `count` at offset 0 for 16 bits.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, the first two bytes are `0x01 0x42` (`0000 0001 0100 0010` in binary). Calling `count("flags", 0, 16)` returns `4` — four bits are set in the device ID field.

Code sample: -   [Java](#tab-panel-595)
-   [Python](#tab-panel-596)
-   [C](#tab-panel-597)
-   [Go](#tab-panel-598)
-   [C#](#tab-panel-599)
-   [Node.js](#tab-panel-600)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0000 0001 0100 0010

Record record = client.operate(null, key,

    BitOperation.count("flags", 0, 16));

// returns 4
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

# bits 0-15: 0000 0001 0100 0010

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_count("flags", 0, 16)]

_, _, result = client.operate(key, ops)

# result["flags"] == 4
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0000 0001 0100 0010

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_count(&ops, "flags", NULL, 0, 16);

as_record* rec = NULL;

aerospike_key_operate(&as, &err, NULL, &key, &ops, &rec);

// returns 4

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0000 0001 0100 0010

record, err := client.Operate(nil, key,

    as.BitCountOp("flags", 0, 16))

// record.Bins["flags"] == 4
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

// bits 0-15: 0000 0001 0100 0010

Record record = client.Operate(null, key,

    BitOperation.Count("flags", 0, 16));

// returns 4
```

```javascript
// count is not available in the Node.js bitwise operate API
```

---

#### `get`

`update_only` `no_fail`

```python
get(bin_name, bit_offset, n_bits)
```

Description: Retrieve `n_bits` bits beginning at offset `offset`. If `n_bits` is not a multiple of 8 then there will be `n_bits` `modulo 8` zeroed bits padding the end.

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `bin_name` | `string` | 
Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first bit to be retrieved.

 |
| `n_bits` | `integer` | 

Number of bits to retrieve.

 |

Returns: `bytes`

Examples: Read the capability flags byte

An IoT device record stores its configuration in a 5-byte Blob bin called `flags`. Byte 2 (bits 16-23) holds capability flags such as WiFi, Bluetooth, and GPS. To read just that byte without transferring the entire blob, use `get` at bit offset 16 for 8 bits.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, calling `get("flags", 16, 8)` returns `[0xB0]` — the single capability byte.

Code sample: -   [Java](#tab-panel-601)
-   [Python](#tab-panel-602)
-   [C](#tab-panel-603)
-   [Go](#tab-panel-604)
-   [C#](#tab-panel-605)
-   [Node.js](#tab-panel-606)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.operate(null, key,

    BitOperation.get("flags", 16, 8));

// returns [0xB0]
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_get("flags", 16, 8)]

_, _, result = client.operate(key, ops)

# result["flags"] == bytearray(b'\xb0')
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_get(&ops, "flags", NULL, 16, 8);

as_record* rec = NULL;

aerospike_key_operate(&as, &err, NULL, &key, &ops, &rec);

// returns [0xB0]

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

record, err := client.Operate(nil, key,

    as.BitGetOp("flags", 16, 8))

// record.Bins["flags"] == []byte{0xB0}
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.Operate(null, key,

    BitOperation.Get("flags", 16, 8));

// returns [0xB0]
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

const bw = Aerospike.bitwise

const ops = [bw.get('flags', 16, 8)]

const record = await client.operate(key, ops)

// record.bins.flags == Buffer<b0>
```

---

#### `get_integer`

`update_only` `no_fail`

```python
get_integer(bin_name, bit_offset, n_bits, signed)
```

Description: Retrieve the `n_bits` bit big-endian integer beginning at offset `offset` as a 64 bit integer.

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `bin_name` | `string` | 
Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first bit to be retrieved

 |
| `n_bits` | `integer` | 

Number of bits to retrieve.

 |
| `signed` | `boolean` | 

If true, treat the value at offset as a signed `n_bit` bit integer, otherwise treat value as unsigned.

 |

Returns: `integer`

Examples: Read the device ID as an integer

An IoT device stores its configuration in a 5-byte Blob bin called `flags`. Bits 0-15 hold a 16-bit device ID stored as a big-endian integer. To read the device ID as a number, use `get_integer` at offset 0 for 16 bits.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, calling `get_integer("flags", 0, 16, false)` returns `322` (`0x0142` as an unsigned 16-bit integer).

Code sample: -   [Java](#tab-panel-607)
-   [Python](#tab-panel-608)
-   [C](#tab-panel-609)
-   [Go](#tab-panel-610)
-   [C#](#tab-panel-611)
-   [Node.js](#tab-panel-612)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.operate(null, key,

    BitOperation.getInt("flags", 0, 16, false));

// returns 322 (0x0142 unsigned)
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_get_int("flags", 0, 16, False)]

_, _, result = client.operate(key, ops)

# result["flags"] == 322
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_get_int(&ops, "flags", NULL, 0, 16, false);

as_record* rec = NULL;

aerospike_key_operate(&as, &err, NULL, &key, &ops, &rec);

// returns 322 (0x0142 unsigned)

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

record, err := client.Operate(nil, key,

    as.BitGetIntOp("flags", 0, 16, false))

// record.Bins["flags"] == 322
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.Operate(null, key,

    BitOperation.GetInt("flags", 0, 16, false));

// returns 322 (0x0142 unsigned)
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

const bw = Aerospike.bitwise

const ops = [bw.getInt('flags', 0, 16, false)]

const record = await client.operate(key, ops)

// record.bins.flags == 322
```

---

#### `lscan`

`update_only` `no_fail`

```python
lscan(bin_name, bit_offset, n_bits, value)
```

Description: Return the position relative to the `offset` of the first bit set to `value` searching from `offset` plus `n_bits` to `offset`. If the `value` isn’t found, returns -1.

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `bin_name` | `string` | 
Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first (leftmost) bit to be scanned.

 |
| `n_bits` | `integer` | 

Number of bits from the `offset` to scan.

 |
| `value` | `boolean` | 

If true, search for the first set bit. If false, search for the first unset bit.

 |

Returns: `integer`

Examples: Find the first set bit in the blob

An IoT device stores its configuration in a 5-byte (40-bit) Blob bin called `flags`. To find the position of the first set bit across the entire blob, use `lscan` from offset 0 for all 40 bits, searching left to right.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, the first byte `0x01` is `0000 0001`. Calling `lscan("flags", 0, 40, true)` returns `7` — the first set bit is at position 7 (the least-significant bit of the first byte).

Code sample: -   [Java](#tab-panel-613)
-   [Python](#tab-panel-614)
-   [C](#tab-panel-615)
-   [Go](#tab-panel-616)
-   [C#](#tab-panel-617)
-   [Node.js](#tab-panel-618)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.operate(null, key,

    BitOperation.lscan("flags", 0, 40, true));

// returns 7 (first set bit is at position 7)
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_lscan("flags", 0, 40, True)]

_, _, result = client.operate(key, ops)

# result["flags"] == 7
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_lscan(&ops, "flags", NULL, 0, 40, true);

as_record* rec = NULL;

aerospike_key_operate(&as, &err, NULL, &key, &ops, &rec);

// returns 7

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

record, err := client.Operate(nil, key,

    as.BitLScanOp("flags", 0, 40, true))

// record.Bins["flags"] == 7
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.Operate(null, key,

    BitOperation.Lscan("flags", 0, 40, true));

// returns 7
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

const bw = Aerospike.bitwise

const ops = [bw.lscan('flags', 0, 40, true)]

const record = await client.operate(key, ops)

// record.bins.flags == 7
```

---

#### `rscan`

`update_only` `no_fail`

```python
rscan(bin_name, bit_offset, n_bits, value)
```

Description: Return the position relative to the `offset` of the first bit set to `value` searching from `offset` to `offset` plus `n_bits`. If the `value` isn’t found, returns -1.

Arguments: | Name | Type | Description |
| --- | --- | --- |
| `bin_name` | `string` | 
Name of bin.

 |
| `bit_offset` | `integer` | 

Offset (in bits) to the first (leftmost) bit to be scanned.

 |
| `n_bits` | `integer` | 

Number of bits from the `offset` to scan.

 |
| `value` | `boolean` | 

If true, search for the first set bit. If false, search for the first unset bit.

 |

Returns: `integer`

Examples: Find the last set bit in the blob

An IoT device stores its configuration in a 5-byte (40-bit) Blob bin called `flags`. To find the position of the last set bit across the entire blob, use `rscan` from offset 0 for all 40 bits, searching right to left.

Given `flags = [0x01, 0x42, 0xB0, 0x04, 0x05]`, the last byte `0x05` is `0000 0101`. Calling `rscan("flags", 0, 40, true)` returns `37` — the last set bit is at position 37 (bit index 37, counting from bit 0 at the left).

Code sample: -   [Java](#tab-panel-619)
-   [Python](#tab-panel-620)
-   [C](#tab-panel-621)
-   [Go](#tab-panel-622)
-   [C#](#tab-panel-623)
-   [Node.js](#tab-panel-624)

```java
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.operate(null, key,

    BitOperation.rscan("flags", 0, 40, true));

// returns 37 (last set bit is at position 37)
```

```python
# flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

from aerospike_helpers.operations import bitwise_operations as bw

ops = [bw.bit_rscan("flags", 0, 40, True)]

_, _, result = client.operate(key, ops)

# result["flags"] == 37
```

```c
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

as_operations ops;

as_operations_init(&ops, 1);

as_operations_bit_rscan(&ops, "flags", NULL, 0, 40, true);

as_record* rec = NULL;

aerospike_key_operate(&as, &err, NULL, &key, &ops, &rec);

// returns 37

as_operations_destroy(&ops);
```

```go
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

record, err := client.Operate(nil, key,

    as.BitRScanOp("flags", 0, 40, true))

// record.Bins["flags"] == 37
```

```csharp
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

Record record = client.Operate(null, key,

    BitOperation.Rscan("flags", 0, 40, true));

// returns 37
```

```javascript
// flags = [0x01, 0x42, 0xB0, 0x04, 0x05]

const bw = Aerospike.bitwise

const ops = [bw.rscan('flags', 0, 40, true)]

const record = await client.operate(key, ops)

// record.bins.flags == 37
```

---