For bool
, it's 8 bit while has only true and false, why don't they make it single bit.
And I know there's bitset
, however it's not that convenient, and I just wonder why?
For bool
, it's 8 bit while has only true and false, why don't they make it single bit.
And I know there's bitset
, however it's not that convenient, and I just wonder why?
The basic data structure at the hardware level of mainstream CPUs is a byte. Operating on bits in these CPUs require additional processing, i.e. some CPU time.
The same holds for bitset
.
Not exactly an answer to why there is not a native type. But you can get a 1-bit type inside of a struct like this:
struct A {
int a : 1; // 1 bit wide
int b : 1;
int c : 2; // 2 bits
int d : 4; // 4 bits
};
Thus, sizeof(A) == 1
could be if there wouldn't be the padding (which probably takes it to a multiple of sizeof(void*)
, i.e. maybe 4 for 32bit systems).
Note that you cannot get a pointer to any of these fields because of the reasons stated by the other people. That might also be why there does not exist a native type.