11

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?

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
iloahz
  • 4,491
  • 8
  • 23
  • 31
  • Probably because the processors don't support it. Also, take a look at bit fields. – luiscubal Oct 12 '13 at 11:16
  • 11
    Who says "`bool` is 8 bit"? – Kerrek SB Oct 12 '13 at 11:19
  • 1
    It would be inefficient to use a bit every time you wanted a boolean due to the need to determine the correct corresponding bit in a byte or word in the CPU and check it. The only time it would make sense would be if you were managing a group of boolean values, which is wha ta bit field or bitset is for. – lurker Oct 12 '13 at 11:19
  • 3
    Essentially, because on most machines memory is byte-addressed. – Matteo Italia Oct 12 '13 at 11:30
  • Because objects in C++ want to have addresses, and a bits don't have addresses. (As long as CHAR_BIT != 1) – cooky451 Oct 12 '13 at 12:22
  • I don't think it's exactly the same question as the marked duplicate. It makes sense to have `bool` be addressable, but that doesn't mean there can't be any type which is not addressable (like a 1bit type). In fact, you can get such situations (see my answer). – Albert Oct 12 '13 at 12:33
  • @cooky451: `CHAR_BIT>=8` is required by the language, so `CHAR_BIT` is never 1. – R.. GitHub STOP HELPING ICE Oct 12 '13 at 13:46
  • Also, C has a 1-bit type, `_Bool`. It has exactly 1 value bit, but also has (typically 7 or 31) padding bits. – R.. GitHub STOP HELPING ICE Oct 12 '13 at 13:47
  • @Albert Your bit field doesn't introduce a non-adressable _type_ <. – cooky451 Oct 12 '13 at 13:57

2 Answers2

16

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.

Igor Popov
  • 2,588
  • 17
  • 20
  • 5
    Actually the basic data structure is usually a word, which are often enough 4 (32-bit), 8 (64 bit) or even more (SSE for example) bytes in width. per-byte operations are often faked inside of those bigger word operations and are sometimes even slower than native word operations, similar to what you say about bits. Do not confuse the basic data structure of your memory (e.g. byte addressable) with that of your CPU. – KillianDS Oct 12 '13 at 11:48
16

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.

Albert
  • 65,406
  • 61
  • 242
  • 386