#allocating arbitrary bit sizes

1 messages · Page 1 of 1 (latest)

vivid slate
#

if I alloc(u7,10) will it actually do that or will it in reality allocate 10 whole bytes and trick me?

olive snow
#

it'll allocate 10 times @sizeOf(u7), so 10 bytes on all sane hardware

vivid slate
#

so it will allocate the same amount as alloc(u8,10)?

olive snow
#

yes

vivid slate
#

so if i alloc(u4,10) that will also be 10 bytes?

olive snow
#

yup

vivid slate
#

that is a bit unfortunate, but i suppose it make sense.

olive snow
#

the elements of the returned slice must each reside in its own unique address, so each takes up one byte

#

you can, however, use packed struct to create bit-packed aggregate data on your own

vivid slate
#

So i would theoretically need to allocate 5 u8 and bit shift to get 10 u4?

vivid slate
olive snow
#

yes. you can allocate 5 u8s, or 5 packed struct { a: u4, b: u4 } to compress the data down - but that won't be done automatically for you

vivid slate
#

manual has never stopped me from driving before 😎

#

thank you for the guidance

spring bear
#

std.bit_set though?

vivid slate
#

who is this bit_set you speak of?

olive snow
#

it's this - but last I checked it's not generic over the element, only working with booleans

#

essentially a bit-packed version of std.ArrayListUnmanaged(bool)

vivid slate
#

from the quick skim I just did, this would surly work too.

#

but I think the packed struct method will be easier to work with

#

for my use-case

#

still, thank you both for the help

crystal dust
#

there used to be packedintarray but it was removed

#

it was kinda bad to be fair