Odin SolutionsOdin Solutions
👝
BIP 39
Week 39, 2026
All SolutionsBits iterator with arbitrary step | greenya | Odin Solutions
package main
import "core:fmt"
import "core:strings"
import "core:crypto/hash"
import "core:math"
import "core:math/bits"
main :: proc () {
words_2048 := load_words(#load("words.txt", string), 2048)
// bytes_32 := [32] byte { 164,215,213,131,229,33,0,124,224,79,124,203,0,22,112,152,113,93,34,121,73,117,200,163,206,127,192,184,106,6,213,237 }
bytes_32 := [32] byte { 55,87,237,88,48,57,128,112,97,44,22,199,125,188,153,242,215,226,41,248,84,142,60,77,87,94,146,232,251,220,6,110 }
bytes_32_hash := hash.hash_bytes(.SHA256, bytes_32[:])
bytes_33: [33] byte = { **bytes_32, bytes_32_hash[0] }
it := bits_iterate(bytes_33[:])
for i in bits_next(&it, 11) do fmt.print(words_2048[i], "")
fmt.println()
}
load_words :: proc (text: string, $N: int) -> (words: [N] string) {
i := 0
text := text
for s in strings.split_lines_iterator(&text) {
w := strings.trim_space(s)
assert(i < len(words))
words[i] = w
i += 1
}
assert(i == len(words))
return
}
Bits_Iterator :: struct {
stream : [] byte,
bit_i : uint,
}
bits_iterate :: proc (stream: [] byte) -> Bits_Iterator {
return { stream=stream }
}
bits_next :: proc (it: ^Bits_Iterator, step: uint) -> (value: uint, ok: bool) {
assert(step <= 8 * size_of(value))
(it.bit_i + step <= 8 * uint(len(it.stream))) or_return
for i in 0..<step {
byte_i, bit_i := math.divmod(it.bit_i + i, 8)
bit := bits.bitfield_extract(uint(it.stream[byte_i]), 8-1-bit_i, 1)
value = bits.bitfield_insert(value, 1, step-1-i, bit)
}
it.bit_i += step
return value, true
}