#how do i get bits array from a value?
1 messages · Page 1 of 1 (latest)
There isn't language-level support for bit-packed arrays of this form, but you can fairly easily access individual bits with bitwise operations. Do you know what bit endian you're working in?
i'm sorry, is bit endian sort of like architecture of the processor? 32-64. I don't know a lot about this
No, bit endian is a detail of what you're doing. Basically, when you're trying to access bits in order, you need to decide whether you want to read the LSB of a byte first, or the MSB of a byte first
i really just need to see fourth bit for example, or sixth, something like that
So maybe not least significant of most significant bit, maybe something inbetween
But when you say "first bit", do you mean least significant bit in the first byte (i.e. representing value 1), or most significant bit in the first byte (i.e. representing value 128)
first bit is LSB
Okay, cool - that's bit-little-endian
oh great
gimme a few moments and i'll write you some simple bit getter/setter functions
oh thanks a lot. Much appreciated
pub fn getBit(buf: []const u8, idx: usize) bool {
return (buf[idx / 8] >> @intCast(idx % 8)) & 1 == 1;
}
pub fn setBit(buf: []const u8, idx: usize, val: bool) void {
if (val) {
buf[idx / 8] |= @as(u8, 1) << @intCast(idx % 8);
} else {
buf[idx / 8] &= ~(@as(u8, 1) << @intCast(idx % 8));
}
}
Untested but I think this is right