# 8 bit and 1 byte, is this a valid question to be asked?

I saw these question in our school's past paper, and I'm wondering if this is a valid question.

How big is `bool` in C and C++?

A) 1 bit

B) 4 bit

C) 8 bit

D) 1 byte

What is the smallest unit of memory C or C++ use?

A) 1 bit

B) 4 bit

C) 8 bit

D) 1 byte

The answer to both are D, but I am thinking if this is inappropriate.

EDIT: 1 byte is not necessarily 8 bits on some systems. See this StackOverflow post.

• Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat Sep 5 '13 at 10:03
• Isn't he asking why the question specifies 8 bits and 1 byte when these are the same thing? He isn't asking for the answer to the question. – razethestray Sep 5 '13 at 10:07
• @gnat I don't see a lot more to add to my question. I think it's obvious enough. It's mainly asked for opinion or reasons why this is appropriate or reasons why not. – Shane Hsu Sep 5 '13 at 10:08
• The answer to any question about the size of a type should always be `sizeof(T)`. – Blrfl Sep 5 '13 at 10:55
• The original C had not bool type, you could help yourself with `typedef int bool;`. I'm still fighting with a bigger project, that abuses that type to return other values than 0 or 1 from functions. – ott-- Sep 11 '13 at 20:53

AFAIK, no answers presented here are correct. The size of `bool` and `_Bool` is not constrained to be 1 byte (I seem to remember that there is a constraint in C which forces `sizeof(_Bool)<=sizeof(short)` which hasn't an equivalent in C++)

• +1 yes you are absolutely right for the first question bool is not constrained to be 1 like char – jk. Sep 5 '13 at 11:06
• My answer should have actually been a comment to yours. Anyway, IMO this is the correct answer. – Vorac Sep 5 '13 at 11:13
• Furthermore `bool` is not defined in C, as you point. When doing sloppy teaching, at least indicate that this is just a primer to the students. Just like Newtonian physics - it works, but is not absolutely correct. And they tell you this, when they teach you physics (hopefully). – Vorac Sep 5 '13 at 11:15
• @Vorac: Booleans were introduced in C99 as the native type `_Bool` and related macros, including `bool`, in `<stdbool.h>`. – Blrfl Sep 5 '13 at 11:57
• @Blrfl : Although you are correct, he did not specify the language version, and even in C99, without the <stdbool.h>, bool has as much meaning as cat or dog. – mattnz Sep 6 '13 at 7:35

I'd say the answers are both D) but the questions do leave a bit to be desired, for the first as AProgrammer points out, a bool could be bigger than 1 byte, for the second question it should clarify the smallest unit of addressable memory (bitfields allow a structure of multiple smaller memory units but they aren't addressable)

to clarify why C) is not the answer, there may not be 8 bits in a byte in C || C++

hopefully its obvious why A) and B) are not correct

• Yes, it's very obvious A) and B) are both terrible options. I see the point, thanks! – Shane Hsu Sep 5 '13 at 10:14

The size of bool in std::vector is 1 bit.

Vector of bool

This is a specialized version of vector, which is used for elements of type `bool` and optimizes for space.

It behaves like the unspecialized version of vector, with the following changes:

• The storage is not necessarily an array of `bool` values, but the library implementation may optimize storage so that each value is stored in a single bit.
• Elements are not constructed using the allocator object, but their value is directly set on the proper bit in the internal storage.
• Member function flip and a new signature for member swap.
• A special member type, reference, a class that accesses individual bits in the container's internal storage with an interface that emulates a `bool` reference. Conversely, member type `const_reference` is a plain `bool`.
• The pointer and iterator types used by the container are not necessarily neither pointers nor conforming iterators, although they shall simulate most of their expected behavior.

These changes provide a quirky interface to this specialization and favor memory optimization over processing (which may or may not suit your needs). In any case, it is not possible to instantiate the unspecialized template of vector for `bool` directly. Workarounds to avoid this range from using a different type (`char`, `unsigned char`) or container (like deque) to use wrapper types or further specialize for specific allocator types...

• that's a special case caused by vector being overloaded for `bool` and transforming into a bit vector – ratchet freak Sep 5 '13 at 11:06
• The memory required still defaults to multiples of 8, requiring the use of at least 1 byte in order to store 1 bit. So while that may be true, the minimum memory required is still 1 byte. – Neil Sep 5 '13 at 11:11
• this is essentially the same case as bit fields - the question should probably state addressable memory to get around this – jk. Sep 5 '13 at 11:16
• nevertheless, if you have 8 bools in a vector, it takes up 1 byte of memory. This can be important to know, and shows that a bool can be 1 bit in size. The question could be better phrased. – gbjbaanb Sep 5 '13 at 12:16