# `Aerospike.Op.Bit`
[🔗](https://github.com/luisgabrielroldan/aerospike_driver/blob/v0.3.1/lib/aerospike/op/bit.ex#L1)

Bit operations on a byte-array bin for `Aerospike.operate/4`.

Bit operations require an Aerospike blob bin. When seeding a bit bin through
this client, write an explicit blob such as `{:blob, <<0>>}` because plain
Elixir binaries are encoded as Aerospike strings.

Offsets are read left to right within the bin. Negative bit or byte offsets
count backward from the end of the bitmap. Out-of-bounds offsets produce a
server parameter error.

Offsets are in bits for `set/5`, `bw_or/5`, `bw_xor/5`, `bw_and/5`,
`bw_not/4`, `lshift/5`, `rshift/5`, `add/5`, `subtract/5`, `set_int/5`,
`get/4`, `count/4`, `lscan/5`, `rscan/5`, and `get_int/5`. `resize/4`,
`insert/4`, and `remove/4` use byte offsets and sizes.

Pass `ctx:` for nested bitmaps and `flags:` for write policy flags.

# `flags`

```elixir
@type flags() :: atom() | [atom()] | non_neg_integer() | {:raw, non_neg_integer()}
```

Bit operation write policy flags.

Accepts `:default`, `:create_only`, `:update_only`, `:no_fail`, `:partial`,
or a list of those atoms. Compatibility callers may pass a non-negative
integer; use `{:raw, integer}` when deliberately sending an unnamed server
value.

# `opts`

```elixir
@type opts() :: [
  ctx: Aerospike.Ctx.t(),
  flags: flags(),
  overflow_action: overflow_action(),
  signed: boolean()
]
```

Common bit operation options.

Supported keys:

* `:ctx` - nested CDT context path from `Aerospike.Ctx`.
* `:flags` - bit-operation write flags. Defaults to `:default`.
* `:overflow_action` - overflow behavior for `add/5` and `subtract/5`.
* `:signed` - when true, adds the signed flag to `:overflow_action`.

# `overflow_action`

```elixir
@type overflow_action() :: atom() | non_neg_integer() | {:raw, non_neg_integer()}
```

Overflow action for bit add/subtract operations.

Accepts `:fail`, `:saturate`, or `:wrap`. Compatibility callers may pass a
non-negative integer; use `{:raw, integer}` when deliberately sending an
unnamed server value. `signed: true` adds the signed flag before encoding.

# `resize_flags`

```elixir
@type resize_flags() ::
  atom() | [atom()] | non_neg_integer() | {:raw, non_neg_integer()}
```

Resize flags for bit resize operations.

Accepts `:default`, `:from_front`, `:grow_only`, `:shrink_only`, or a list
of those atoms. Compatibility callers may pass a non-negative integer; use
`{:raw, integer}` when deliberately sending an unnamed server value.

# `t`

```elixir
@opaque t()
```

Opaque bit operation for `Aerospike.operate/4`.

# `add`

```elixir
@spec add(String.t(), integer(), integer(), integer(), opts()) :: t()
```

Adds `value` to a sub-integer in the bitmap.

Supports `flags:`, `overflow_action:`, and `signed:`. Use `signed: true` to
OR the signed flag into `overflow_action:`.

# `bw_and`

```elixir
@spec bw_and(String.t(), integer(), integer(), binary(), opts()) :: t()
```

Applies bitwise AND of `value` into `[bit_offset, bit_offset + bit_size)`.

# `bw_not`

```elixir
@spec bw_not(String.t(), integer(), integer(), opts()) :: t()
```

Inverts `[bit_offset, bit_offset + bit_size)`.

# `bw_or`

```elixir
@spec bw_or(String.t(), integer(), integer(), binary(), opts()) :: t()
```

Applies bitwise OR of `value` into `[bit_offset, bit_offset + bit_size)`.

# `bw_xor`

```elixir
@spec bw_xor(String.t(), integer(), integer(), binary(), opts()) :: t()
```

Applies bitwise XOR of `value` into `[bit_offset, bit_offset + bit_size)`.

# `count`

```elixir
@spec count(String.t(), integer(), integer(), opts()) :: t()
```

Returns the count of bits set to `1` in `[bit_offset, bit_offset + bit_size)`.

# `get`

```elixir
@spec get(String.t(), integer(), integer(), opts()) :: t()
```

Returns `bit_size` bits starting at `bit_offset` as a binary.

# `get_int`

```elixir
@spec get_int(String.t(), integer(), integer(), boolean(), opts()) :: t()
```

Reads an integer from `[bit_offset, bit_offset + bit_size)`.

Pass `true` for `signed` to interpret the field as signed.

# `insert`

```elixir
@spec insert(String.t(), integer(), binary(), opts()) :: t()
```

Inserts `bytes` at `byte_offset` in the bin.

# `lscan`

```elixir
@spec lscan(String.t(), integer(), integer(), boolean(), opts()) :: t()
```

Returns the offset of the first bit matching `value` when scanning left to right.

# `lshift`

```elixir
@spec lshift(String.t(), integer(), integer(), integer(), opts()) :: t()
```

Left shifts `[bit_offset, bit_offset + bit_size)` by `shift` bits.

# `overflow_fail`

```elixir
@spec overflow_fail() :: 0
```

Fail bit add/subtract on overflow or underflow.

# `overflow_saturate`

```elixir
@spec overflow_saturate() :: 2
```

Saturate bit add/subtract to min/max on overflow or underflow.

# `overflow_wrap`

```elixir
@spec overflow_wrap() :: 4
```

Wrap bit add/subtract on overflow or underflow.

# `remove`

```elixir
@spec remove(String.t(), integer(), integer(), opts()) :: t()
```

Removes `byte_size` bytes starting at `byte_offset`.

# `resize`

```elixir
@spec resize(String.t(), integer(), resize_flags(), opts()) :: t()
```

Resizes the bin to `byte_size` bytes using `resize_flags`.

# `rscan`

```elixir
@spec rscan(String.t(), integer(), integer(), boolean(), opts()) :: t()
```

Returns the offset of the first bit matching `value` when scanning right to left.

# `rshift`

```elixir
@spec rshift(String.t(), integer(), integer(), integer(), opts()) :: t()
```

Right shifts `[bit_offset, bit_offset + bit_size)` by `shift` bits.

# `set`

```elixir
@spec set(String.t(), integer(), integer(), binary(), opts()) :: t()
```

Overwrites the bit region `[bit_offset, bit_offset + bit_size)` with bits from `value`.

# `set_int`

```elixir
@spec set_int(String.t(), integer(), integer(), integer(), opts()) :: t()
```

Writes integer `value` into `[bit_offset, bit_offset + bit_size)`.

# `subtract`

```elixir
@spec subtract(String.t(), integer(), integer(), integer(), opts()) :: t()
```

Subtracts `value` from a sub-integer in the bitmap.

Supports `flags:`, `overflow_action:`, and `signed:`. Use `signed: true` to
OR the signed flag into `overflow_action:`.

# `write_create_only`

```elixir
@spec write_create_only() :: 1
```

Only create the bit bin when it does not already exist.

# `write_default`

```elixir
@spec write_default() :: 0
```

Use default bit write behavior.

# `write_no_fail`

```elixir
@spec write_no_fail() :: 4
```

Do not fail the command when a bit operation is denied by write flags.

# `write_partial`

```elixir
@spec write_partial() :: 8
```

Commit other valid operations when this bit operation is denied by write flags.

# `write_update_only`

```elixir
@spec write_update_only() :: 2
```

Only update the bit bin when it already exists.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
