How bool data type is implemented in C++

A

a2z

Hi all,
Can someone throw some light on the implemetation of bool data
type in C++? I have heard that it is bit based, but want to confirm
this information. Is there any source code where I can look into?

~Thanks,
Ramesh.
 
N

Neelesh Bodas

Hi all,
Can someone throw some light on the implemetation of bool data
type in C++? I have heard that it is bit based, but want to confirm
this information. Is there any source code where I can look into?

~Thanks,
Ramesh.

Not dictated by the c++ standard. sizeof(bool) is also implementation
defined. Given that, implemenations are free to represent them in bit-
representation, as a char, or even as an int, or any other way as they
wish.

-N
 
V

Victor Bazarov

Neelesh said:
Not dictated by the c++ standard. sizeof(bool) is also implementation
defined. Given that, implemenations are free to represent them in bit-
representation, as a char, or even as an int, or any other way as they
wish.

Not sure what you mean by "bit-representation". Every object (even of
type 'bool') has to have its own address, so sizeof(bool) cannot be less
than 1, so a char is a minimal unit to represent a bool value. Please
elaborate if I understood you incorrectly.

V
 
N

Neelesh Bodas

Not sure what you mean by "bit-representation". Every object (even of
type 'bool') has to have its own address, so sizeof(bool) cannot be less
than 1, so a char is a minimal unit to represent a bool value. Please
elaborate if I understood you incorrectly.

Probably I used incorrect terminlogy. By "bit-representation" I was
mentioning the same thing that OP meant by "bit based". In other
words, something to the effect that true and false might be any
specific sequence of bits (say 24 bits = 3 bytes) and defined in any
manner - true is alternate one's and zero's and false is all zeros.

-N
 
V

Victor Bazarov

Neelesh said:
Probably I used incorrect terminlogy. By "bit-representation" I was
mentioning the same thing that OP meant by "bit based". In other
words, something to the effect that true and false might be any
specific sequence of bits (say 24 bits = 3 bytes) and defined in any
manner - true is alternate one's and zero's and false is all zeros.

Ah.. I get it. Yes, the representation in memory is not prescribed,
and sizeof(bool) can be anything the implementation wants. Whatever
the OP meant by "bit based" it can't be sharing bits of the same, say,
char, to represent different objects, however. While 'vector<bool>'
does most likely pack the elements in individual bits, a stand-alone
objects of 'bool' type cannot be packed similarly.

V
 
J

Juha Nieminen

Victor said:
Not sure what you mean by "bit-representation". Every object (even of
type 'bool') has to have its own address, so sizeof(bool) cannot be less
than 1, so a char is a minimal unit to represent a bool value.

struct A { int i:2, j:3; };

What are the addresses of i and j?
 
T

terminator

Ah.. I get it. Yes, the representation in memory is not prescribed,
and sizeof(bool) can be anything the implementation wants. Whatever
the OP meant by "bit based" it can't be sharing bits of the same, say,
char, to represent different objects, however. While 'vector<bool>'
does most likely pack the elements in individual bits, a stand-alone
objects of 'bool' type cannot be packed similarly.

V

since casting false to int results in zero(really? should I be
certain?)it is simpler to be represented with all bits zero,but for
true I guess it should be all ones:

bool a= false^true;//exert an int bitwise xor operator on bools and
cast to bool

what is the result of the above line?(unfortunately C++ lacks bool
xor)

regards,
FM.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

since casting false to int results in zero(really? should I be
certain?)it is simpler to be represented with all bits zero,but for
true I guess it should be all ones:

bool a= false^true;//exert an int bitwise xor operator on bools and
cast to bool

what is the result of the above line?(unfortunately C++ lacks bool
xor)

The standard specifies that a "zero value, null pointer value, or null
member pointer value is converted to false; any other value is converted
to true."
 
R

Rajesh S R

Why don't you try operator& on those, and you'll find out.

I guess you did not notice that struct members are bit fields. So even
if an instance of the struct object is created, u cannot *portably*
find the address of its members which are bit fields.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I guess you did not notice that struct members are bit fields. So even
if an instance of the struct object is created, u cannot *portably*
find the address of its members which are bit fields.

Actually you shall not apply the address-of operator to bit-fields at all.
 
J

Juha Nieminen

Erik said:
Actually you shall not apply the address-of operator to bit-fields at all.

It was just a note about the original claim "every object has to have
its own address". I was thinking it doesn't apply to bitfields, so they
are not the same type of "objects" as everything else...
 
O

Old Wolf

bool a= false^true;//exert an int bitwise xor operator on bools and
cast to bool

what is the result of the above line?

The operands of '^' are promoted to int before the
xor operation is evaluated, so it is the same as:

bool a = 0^1;

regardless of internal representation of bool.
(Which must result in 'a' == true).
 
T

terminator

The operands of '^' are promoted to int before the
xor operation is evaluated, so it is the same as:

bool a = 0^1;

regardless of internal representation of bool.
(Which must result in 'a' == true).

that is what I guess .but can I be sure?

thanks,
FM.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top