As above post describe , bool is 8 bit long.
So is it possible to send value 2 in bool
variable.
i.e.
0000 0010 -> 2
(decimal representation)
eg: bool x;
How to send this '2' in above bool variable 'x' ?
Thanks
As above post describe , bool is 8 bit long.
So is it possible to send value 2 in bool
variable.
i.e.
0000 0010 -> 2
(decimal representation)
eg: bool x;
How to send this '2' in above bool variable 'x' ?
Thanks
Not in C++, no. A bool
can hold true
or false
. There is no way to store 2
in a bool
without first invoking undefined behaviour. Once you have invoked undefined behaviour, anything can happen. (Including what you expected except when demo'ing to important clients).
Also, a bool
is not necessarily 8 bits long. It must be at least as large as a char (because sizeof(bool)
must be at least 1), and the limits on the range of values which an unsigned char
can hold means that it must be at least 8 bits. OTOH, there is nothing to stop an implementation using a bool
which is larger than char
, and there actually are implementations where char
is 32 or 64 bits (DSP chips in the main).
bool is 8 bits long
Not necessarily true. All the standards say is that it has to be capable of holding true
and false
: its sizeof
is implementation defined. You can deduce that it must be at least 1 since the type of sizeof
must be an integral type and it cannot be zero else pointer arithmetic on an array of bool
s would break.
So don't attempt to send the value 2 - you're bound to render the behaviour of your program undefined.