---
title: Std.BitString
---

Operations on raw binary data (Erlang bitstrings).

## Functions

### size

```saga
fun size : (bs: BitString) -> Int
```

Returns the size of the bitstring in bytes.

### from_list

```saga
fun from_list : (bytes: List Int) -> BitString
```

Creates a bitstring from a list of byte values (0-255).

### to_list

```saga
fun to_list : (bs: BitString) -> List Int
```

Converts a bitstring to a list of byte values.

### from_string

```saga
fun from_string : (s: String) -> BitString
```

Creates a bitstring from a UTF-8 encoded string.
This is a zero-cost operation since Dylan strings are already UTF-8 binaries.

### to_string

```saga
fun to_string : (bs: BitString) -> Result String String
```

Attempts to decode a bitstring as a UTF-8 string.
Returns Err if the bitstring is not valid UTF-8.

### at

```saga
fun at : (index: Int) -> (bs: BitString) -> Maybe Int
```

Returns the byte at the given zero-based index, or Nothing if out of bounds.

### slice

```saga
fun slice : (start: Int) -> (len: Int) -> (bs: BitString) -> BitString
```

Extracts a sub-bitstring starting at position for length bytes.

### append

```saga
fun append : (a: BitString) -> (b: BitString) -> BitString
```

Appends two bitstrings. Also available via the <> operator.

### zeroes

```saga
fun zeroes : (n: Int) -> BitString
```

Creates a bitstring of the given size filled with zero bytes.

### from_byte

```saga
fun from_byte : (byte: Int) -> BitString
```

Creates a single-byte bitstring from an integer (0-255).

### encode_int

```saga
fun encode_int : (width: Int) -> (value: Int) -> BitString
```

Encodes an integer as a big-endian bitstring of the given byte width.

### decode_int

```saga
fun decode_int : (bs: BitString) -> Int
```

Decodes a big-endian integer from a bitstring.

### encode_int_little

```saga
fun encode_int_little : (width: Int) -> (value: Int) -> BitString
```

Encodes an integer as a little-endian bitstring of the given byte width.

### decode_int_little

```saga
fun decode_int_little : (bs: BitString) -> Int
```

Decodes a little-endian integer from a bitstring.

### is_empty

```saga
fun is_empty : BitString -> Bool
```

Returns True if the bitstring is empty (zero bytes).

