### How bool is stored in memory

Pages: 1234
I got that int is stored like:
01000100 01000110 00101101 00110001

char like:
00110001

and double(4 bytes) like:
0 10001000 1000110 00101101 00110001

But what about a boolean?

if I want to store

 `` `` `` bool b=9;``

is it stored like:
00001001
or just
00000001

See [1]. Basically, it's not specified in the standard (i.e, it depends on the compiler's implementation).

[1] SO: How is a bool represented in memory?
https://stackoverflow.com/questions/19351483/how-is-a-bool-represented-in-memory
"Not specified" means its up to the ABI.

For example following System V, a bool in memory is a byte long, byte aligned, and it holds its true-or-false value in bit 0 while bits 1-7 are always low.
Thanks @ElusiveTau, but your link does not help, it seems based on their answers that there is no binary representation of a boolean.

@mbozzi, what's ABI?
Application Binary Interface
https://stackoverflow.com/questions/2171177/what-is-an-application-binary-interface-abi
"An ABI is a set of rules that determines calling convention, and rules for laying out structures."
So if the compiler uses System V for its ABI spec, then it will represent bool b = 9 as 0b0000'0001. But it's beyond the scope of the C++ standard, which doesn't attempt to regulate the ABI beyond basic limitations, like a char is at least 8-bit (but can be more).

 it seems based on their answers that there is no binary representation of a boolean
There is no single, standard binary representation of a boolean per the C++ standard.
Last edited on
Most of the time on most systems its going to be a byte / char type. If you put it into a vector, it MAY be condensed into a bit for you.
The purpose of an ABI is to allow different programs to interpret each other's data. An ABI is the set of rules that allow this.

For example, an ABI specifies how arguments should be passed to functions.
Without these rules, a called function might not be able to find its own arguments.

Here is the spec for System V ABI, for AMD64/x86-64 processors:
https://gitlab.com/x86-psABIs/x86-64-ABI/-/jobs/artifacts/master/raw/x86-64-ABI/abi.pdf?job=build
The System V ABI is used by Unix systems.

It is worth keeping in mind that these rules only need to be enforced on the boundary of binary interfaces.
Last edited on

@jonnin, if I understand your answer right, then I am not asking whether bool is 1 byte or 2..., I am asking how it is stored in memory as bits.

in the line:
`int b = 9;`

My intuition tells me that the compiler notice an integer value(9), then convert it automatically to 0000 0001, but I am not sure.

It seems this is the case happening in system V, based on @mbozzi and @Ganado responses. But they are not sure about the other systems. for me this raises a good question!, how the OS will read a boolean(when a program is running) if it is different than 0000 0001 and 0000 0000
Last edited on
 This to me rise a good! question, how the OS will read a boolean(when a program is running) if it is different than 0000 0001 and 0000 0000

For a bool value that is in an integer register (not in memory as mentioned above), the bool is false only if the entire register is zero.
Last edited on
This to me rise a good! question, how the OS will read a boolean(when a program is running) if it is different than 0000 0001 and 0000 0000

the CPU does not know what a boolean is. Or, at least I do not know of any hardware that differentiates it out as special. In assembly languages you will just see them using integers for these purposes. And, in assembly language, what you most likely will see is more or less

compare value, 0
if equal jump here
else jump there

> I am asking how it is stored in memory as bits.

 Type bool is a distinct type that has the same object representation, value representation, and alignment requirements as an implementation-defined unsigned integer type. https://eel.is/c++draft/basic.fundamental#10

@JLBorges, to be honest I do know what those means:
object representation, value representation, and alignment requirements

can you clarify, i you may please
 I do know what those means: object representation, value representation, and alignment requirements

Basically, you can think of a bool as an unsigned integer that is 1 byte. It acts in the same way under the hood with how it's handled by the CPU and memory.

`bool a = false;`

Will be:

0000 0000

And setting the bool to true will likely just set it to 1:

0000 0001

Every compiler I've tried will only allow a bool to be a 1 or 0. It will allow you to put other values, but any value that's not 0 becomes a 1.

Others have stated this is not standard, but it seems to be what most compilers do.

Try it out yourself:

 ``123456789`` ``````#include #include int main() { bool a = 9; std::cout << std::bitset<8>(a).to_string() << '\n'; std::cout << a << '\n'; }``````
Last edited on
zapshe, that example tells you nothing about how it's stored.
a is true so when you pass a to the bitset constructor it will be converted to an integer (unsigned long long) that has the value 1.
It's specified to happen this way regardless of the binary representation of bool.
Last edited on
 It's specified to happen this way regardless of the binary representation of bool

Which we already know that it's representation is no different than an unsigned int - thanks to JLBorges.

Also note the OP's original question, simply:

 bool b=9; is it stored like: 00001001 or just 00000001
On VS, trying to assign a value of 2 to a bool gives a "truncation from 'uint8_t' to 'bool'" warning message - even though bool has a sizeof 1. And bool has the value 1 - not 2. You can set a value of bool to greater than 1 by casting to a type uint8_t - but that is sort of cheating... and it's still treated as 'true'. For testing, false is zero, true is not zero. For the result from a bool type, 0 is false and 1 is true (unless you're cheating!).

 ``123456789101112131415`` ``````include #include int main() { bool b {}; std::cout << "bytes in bool are " << sizeof(b) << '\n'; *((uint8_t*)(&b)) = 3; std::cout << "b is " << b << std::boolalpha << " as bool " << b << std::noboolalpha << '\n'; b = (uint8_t)2; std::cout << "b is " << b << std::boolalpha << " as bool " << b << '\n'; }``````

 ``` bytes in bool are 1 b is 3 as bool true b is 1 as bool true ```

In terms of bits, false would be 00000000 and true(1) would be 00000001. From the output, it seems that the value is 'truncated' on set and just retrieved on 'get'.
Last edited on
zapshe wrote:
Which we already know that it's representation is no different than an unsigned int - thanks to JLBorges.

In that case I don't understand the purpose of your example.

seeplus wrote:
You can set a value of bool to greater than 1 by casting to a type uint8_t
[...]
`*((uint8_t*)(&b)) = 3;`

Isn't that technically UB?

P2624 says there is a contradiction in the standard:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2624r0.html

GCC gives the same output as your VS output. Clang prints "b is 1" in both cases.
Assuming no UB, it seems like P2624 suggests the Clang behaviour should be standardized.
 In that case I don't understand the purpose of your example.

To answer the simple question that was asked?

 bool b=9; is it stored like: 00001001 or just 00000001

??? Since it's 1, its stored in memory as 0000 0001. What's the mystery?
 Since it's 1, its stored in memory as 0000 0001. What's the mystery?

But we don't want to know how the integer 1 is stored in memory, do we?
 But we don't want to know how the integer 1 is stored in memory, do we?

We do... back to JLBorges' link.

"then why did you show an example"

To answer his question, it's 1 which means its 0000 0001.

"But we don't know how it's stored in memory"

while(1) in real life
Pages: 1234