Original Post
It's been a few years since I've done direct bit manipulation, so I'm trying to figure out the best way to go about this. I'm implementing a replay feature into a game with a fixed timestep. So I will effectively just be logging inputs, spawning, and the associated frame number, and feed the data back into the engine so it can recreate it. I'm keeping track of the frame number, the operation performed, and the data associated with the operation. Since the game could theoretically last for several hours, I need to compress the record keeping as much as possible, so I'm making ever bit count. With the frame counter, I figured I could fit about five hours into 20 bits (2^20 can hold quite a lot), so I'm sticking with that number. I have a total of 6 possible actions, so that's 3 more bits for the action. Data is associated with the action and thus varies, but it's anywhere from 1 to 15 extra bits. What I want to do is just stack this data end to end, and then break it apart later when I load the replay back up. Effectively, I don't want to waste a single bit. The problem I'm running into is breaking this up over multiple bytes. I've got 20 bytes for the frame number, and then I've got to stuff 3 more bits into that third byte after the 20th bit, and then a varying amount of data packed behind that... and then when the next action is logged, nothing's falling at the beginning of a byte anyway, so it's a big mess. Spreading data across two or more bytes gets complicated, especially when it doesn't start at the beginning of the byte. I'm rather fluent in C++'s bitwise operations but am not sure about the best way to go about getting this to work. Any tips?