#ifndef SIMPLICITY_BITSTRING_H
#define SIMPLICITY_BITSTRING_H
#include <stddef.h>
#include <stdint.h>
#include <limits.h>
#include <stdbool.h>
#include "simplicity_assert.h"
typedef struct bitstring {
const unsigned char* arr;
size_t len;
size_t offset;
} bitstring;
static inline bool getBit(const bitstring *s, size_t n) {
size_t total_offset = s->offset + n;
rustsimplicity_0_6_assert(n < s->len);
return 1 & (s->arr[total_offset / CHAR_BIT] >> (CHAR_BIT - 1 - (total_offset % CHAR_BIT)));
}
static inline uint_fast8_t getByte(const bitstring *s, size_t n) {
rustsimplicity_0_6_assert(8 <= s->len);
rustsimplicity_0_6_assert(n <= s->len - 8);
size_t total_offset = s->offset + n;
if (total_offset % CHAR_BIT <= CHAR_BIT - 8) {
return (uint_fast8_t)(0xff & (s->arr[total_offset / CHAR_BIT] >> (CHAR_BIT - 8 - (total_offset % CHAR_BIT))));
} else {
return (uint_fast8_t)(0xff & (((1U * s->arr[total_offset / CHAR_BIT]) << (total_offset % CHAR_BIT - (CHAR_BIT - 8)))
| (s->arr[total_offset / CHAR_BIT + 1] >> (CHAR_BIT - (total_offset % CHAR_BIT - (CHAR_BIT - 8))))));
}
}
#endif