Hello,
what is an elegant way to transform low int values into a simple bit representation to store on disc and load it back. At best without external libraries.
I have exact 8 times pos integer values with max value of 31. This 8 times are so 2^5 different values. I looking for an elegant way to store it in 6 bytes (aka UInt8) and take the other 4 bits to store/load similar data with max 15.
For nibbles (right and left as UInt8) I use simple functions like
let storage : UInt8 = (left << 4) + right
let right : UInt8 = (storage << 4) >> 4
let left : UInt8 = (container >> 4)
Is it elegant to take an [UInt8] to store my 8 values or is it better to use UInt64 and what is a very fast way to store and read this values?
UInt64 + the ops similar to what you have above would be the fastest. Nowhere near as elegant as C-s bit fields (which Swift can handle), but those were never truly portable nor appropriate for serialising to / from disk / network.