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.
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.
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.
@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
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
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
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';
}
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.
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'.
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.