How bool is stored in memory

Pages: 1234
Aug 26, 2022 at 7:01pm
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

Thanks for reading
Aug 26, 2022 at 7:27pm
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
Aug 26, 2022 at 7:55pm
"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.
Aug 26, 2022 at 8:55pm
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?
Aug 26, 2022 at 8:56pm
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 Aug 26, 2022 at 9:04pm
Aug 26, 2022 at 9:25pm
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.
Aug 26, 2022 at 9:45pm
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 Aug 28, 2022 at 3:44am
Aug 26, 2022 at 9:48pm
Thanks @Ganado, that's helpful.

@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 Aug 27, 2022 at 9:35am
Aug 26, 2022 at 9:57pm
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 Aug 26, 2022 at 9:57pm
Aug 27, 2022 at 12:29am
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

Aug 27, 2022 at 1:57am
> 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

Aug 27, 2022 at 2:10am
@JLBorges, to be honest I do know what those means:
object representation, value representation, and alignment requirements

can you clarify, i you may please
Aug 27, 2022 at 8:07am
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:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <bitset>

int main()
{
	bool a = 9;
	std::cout << std::bitset<8>(a).to_string() << '\n';
	std::cout << a << '\n';
}
Last edited on Aug 27, 2022 at 8:08am
Aug 27, 2022 at 8:43am
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 Aug 27, 2022 at 8:47am
Aug 27, 2022 at 8:48am
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
Aug 27, 2022 at 8:56am
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!).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
include <iostream>
#include <iomanip>

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 Aug 27, 2022 at 9:11am
Aug 27, 2022 at 9:13am
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.
Aug 27, 2022 at 9:19am
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?
Aug 27, 2022 at 9:23am
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?
Aug 27, 2022 at 9:26am
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