I’m not used to binary files, and I’m trying to get the hang of it. I managed to store some integers and unsigned char, and read them without too much pain. Now, when I’m trying to save some booleans, I see that each of my bool takes exactly 1 octet in my file, which seems logical since a lone bool is stored in a char-sized data (correct me if I’m wrong!).
But since I’m going to have 3 or 4 bools to serialize, I figure it is a waste to store them like this : 00000001 00000001 00000000, for instance, when I could have 00000110. I guess to obtain this I should use bitwise operation, but I’m not very good with them… so could somebody tell me:
- How to store up to 8 bools in a single octet using bitwise manipulations?
- How to give proper values to (up to 8 bools) from a single octet using bitwise manipulation?
- (And, bonus question, does anybody can recommend a simple, non-mathematical-oriented-mind like mine, bit manipulation tutorial if this exists? Everything I found I understood but could not put into practice…)
I’m using C++ but I guess most C-syntaxic languages will use the same kind of operation.
To store bools in a byte:
To read it back: