# Save a structure and load it back

> Serialization lives in a /wire subpath. It writes versioned bytes and restores the same layout.

**Few items**

```ts
import { roaringBitset } from 'knotkit/bitset'
import { deserializeBitset, serializeBitset } from 'knotkit/bitset/wire'

const ids = roaringBitset({ items: [7, 42, 2 ** 31] })
const bytes = serializeBitset(ids, { integrity: true }) // a Uint8Array to store anywhere

const restored = deserializeBitset(bytes)
restored.kind // 'roaring'
restored.has(42) // true
restored.size // 3
```

**Thousands of items**

```ts
import { roaringBitset } from 'knotkit/bitset'
import { deserializeBitset, serializeBitset } from 'knotkit/bitset/wire'
import { splitmix32 } from 'knotkit/prng'
import { shuffled } from 'knotkit/sampling'

// 50,000 ids from 0 to 49,999, added in shuffled order.
const ids = shuffled(
  splitmix32(2026),
  Array.from({ length: 50_000 }, (_, index) => index),
)
const set = roaringBitset({ items: ids })
const bytes = serializeBitset(set, { integrity: true })

const restored = deserializeBitset(bytes)

restored.kind // 'roaring'
restored.size // 50000
restored.has(49_999) // true
bytes.byteLength // 8288, the ids fill one dense chunk: a bitmap of 8 KiB for each 65,536 possible ids
```

With `integrity` set, a corrupted artifact fails with a parse error code instead of loading. Code that never saves a structure never imports the parser.

**Modules:** [`knotkit/bitset`](/modules/bitset/) · [API reference](/reference/knotkit/bitset/)
