Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a byte out of 8 bool values (and vice versa)?

I have 8 bool variables, and I want to "merge" them into a byte.

Is there an easy/preferred method to do this?

How about the other way around, decoding a byte into 8 separate boolean values?

I come in assuming it's not an unreasonable question, but since I couldn't find relevant documentation via Google, it's probably another one of those "nonono all your intuition is wrong" cases.

like image 309
xcel Avatar asked Dec 11 '11 00:12

xcel


People also ask

How many bytes is an bool?

Boolean variables are stored as 16-bit (2-byte) numbers, but they can only be True or False.

Why is a bool 4 bytes?

Because it's fast. A 32-bit processor typically works with 32-bit values. Working with smaller values involves longer instructions, or extra logic.

Is a bool 1 byte?

bool The bool type takes one byte and stores a value of true (1) or false(0).

Why does bool take a byte?

A bool takes in real 1 bit, as you need only 2 different values. However, when you do a sizeof(bool), it returns 1, meaning 1 byte. For practical reasons, the 7 bits remaining are stuffed. you can't store a variable of size less than 1 byte.


1 Answers

The hard way:

unsigned char ToByte(bool b[8]) {     unsigned char c = 0;     for (int i=0; i < 8; ++i)         if (b[i])             c |= 1 << i;     return c; } 

And:

void FromByte(unsigned char c, bool b[8]) {     for (int i=0; i < 8; ++i)         b[i] = (c & (1<<i)) != 0; } 

Or the cool way:

struct Bits {     unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1; }; union CBits {     Bits bits;     unsigned char byte; }; 

Then you can assign to one member of the union and read from another. But note that the order of the bits in Bits is implementation defined.

Note that reading one union member after writing another is well-defined in ISO C99, and as an extension in several major C++ implementations (including MSVC and GNU-compatible C++ compilers), but is Undefined Behaviour in ISO C++. memcpy or C++20 std::bit_cast are the safe ways to type-pun in portable C++.

(Also, the bit-order of bitfields within a char is implementation defined, as is possible padding between bitfield members.)

like image 114
rodrigo Avatar answered Sep 22 '22 07:09

rodrigo