473,395 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How bool data type is implemented in C++

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.

Aug 22 '07 #1
13 6999
On Aug 22, 3:34 pm, a2z <ramesh.hem...@gmail.comwrote:
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

Aug 22 '07 #2
Neelesh Bodas wrote:
On Aug 22, 3:34 pm, a2z <ramesh.hem...@gmail.comwrote:
>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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 22 '07 #3
On Aug 22, 4:43 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Neelesh Bodas wrote:
On Aug 22, 3:34 pm, a2z <ramesh.hem...@gmail.comwrote:
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.

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
Aug 22 '07 #4
Neelesh Bodas wrote:
On Aug 22, 4:43 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Neelesh Bodas wrote:
>>On Aug 22, 3:34 pm, a2z <ramesh.hem...@gmail.comwrote:
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.

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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 22 '07 #5
Victor Bazarov wrote:
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?
Aug 23 '07 #6
Juha Nieminen wrote:
Victor Bazarov wrote:
>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?
Why don't you try operator& on those, and you'll find out.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 23 '07 #7
On Aug 22, 3:19 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Neelesh Bodas wrote:
On Aug 22, 4:43 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Neelesh Bodas wrote:
On Aug 22, 3:34 pm, a2z <ramesh.hem...@gmail.comwrote:
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.
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.

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -
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.

Aug 23 '07 #8
On 2007-08-23 16:38, terminator wrote:
On Aug 22, 3:19 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Neelesh Bodas wrote:
On Aug 22, 4:43 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Neelesh Bodas wrote:
On Aug 22, 3:34 pm, a2z <ramesh.hem...@gmail.comwrote:
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.
>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.

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

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."

--
Erik Wikström
Aug 23 '07 #9
On Aug 23, 6:53 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Juha Nieminen wrote:
Victor Bazarov wrote:
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?

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.
Aug 23 '07 #10
On 2007-08-23 17:02, Rajesh S R wrote:
On Aug 23, 6:53 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Juha Nieminen wrote:
Victor Bazarov wrote:
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?

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.
Actually you shall not apply the address-of operator to bit-fields at all.

--
Erik Wikström
Aug 23 '07 #11
Erik Wikström wrote:
On 2007-08-23 17:02, Rajesh S R wrote:
>On Aug 23, 6:53 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>>Juha Nieminen wrote:
Victor Bazarov wrote:
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?

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.

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...
Aug 23 '07 #12
On Aug 24, 2:38 am, terminator <farid.mehr...@gmail.comwrote:
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).

Aug 27 '07 #13
On Aug 28, 2:38 am, Old Wolf <oldw...@inspire.net.nzwrote:
On Aug 24, 2:38 am, terminator <farid.mehr...@gmail.comwrote:
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).
that is what I guess .but can I be sure?

thanks,
FM.

Aug 29 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nomak | last post by:
Hello, With this code: $ cat -n ifs.cc 1 #include <vector> 2 #include <iostream> 3 4 using std::vector; 5 using std::cin;
3
by: Christian Christmann | last post by:
Hi, I'm working on my first STL program. My class BitSet has three private attributes size, curbit and the STL bit_vector data which contains some bits. Here is the code for the constructors: ...
4
by: ORC | last post by:
Is the bool type actually an Int32 with 0 as false and non zero as true? The reason for my question is that I've seen a lot of API calls that return a Int32 implemented in C# as an bool like: ...
2
by: Chris Wood | last post by:
In C#, I am calling a method implemented in Managed C++ that returns an array of booleans. This method in turn calls unto unmanaged C++ code that returns an unsigned byte array, which is...
5
by: per9000 | last post by:
Hi all, I have a question I am unable solve. I have an inheritance graph like this: // CAR ----- MUSICAR ---- NICECAR // \ \ // \ +------ OLDCAR // \ // ...
57
by: Alan Isaac | last post by:
Is there any discussion of having real booleans in Python 3000? Say something along the line of the numpy implementation for arrays of type 'bool'? Hoping the bool type will be fixed will be...
6
by: John | last post by:
The following code: int test = 1; bool isTrue = (bool)test; results in a compiler error: Cannot convert type 'int' to 'bool' wtf, any ideas on how to work around this?
51
by: AommiK | last post by:
First of all: I love C and think that it's beautiful. However, there is at least one MAJOR flaw: the lack of a boolean type. OK. Some of you might refer to C99 and its _Bool (what's up with the...
15
by: ssylee | last post by:
I am using mikroC to program some microcontroller code in C. However I have come across of potential problem of bool type not being supported from the compiler complaint on how I declared type bool...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.