473,606 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7020
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...@com Acast.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...@com Acast.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
implementatio n 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...@com Acast.netwrote:
Neelesh Bodas wrote:
On Aug 22, 4:43 pm, "Victor Bazarov" <v.Abaza...@com Acast.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
implementati on 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?(unfortuna tely 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...@com Acast.netwrote:
>Neelesh Bodas wrote:
On Aug 22, 4:43 pm, "Victor Bazarov" <v.Abaza...@com Acast.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
implementatio n 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?(unfortuna tely 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...@com Acast.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

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

Similar topics

4
2943
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
2859
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: BitSet::BitSet() :last(-1)
4
10744
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: private static extern bool PurgeComm( Int32 hFile, UInt32 dwFlags); Thanks Ole
2
1382
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 implicitly casted to a bool __gc . However, in my client C# app, when I examine this array in the locals window, it shows "bool" as the type of the array and "byte" as the type of each element. If I try this in Whidbey beta, it shows "bool {byte}" and...
5
3968
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 // \ // +---- HOMEMADECAR
57
3361
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 fixed, Alan Isaac
6
30163
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
2887
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 uppercase 'B' anyway?) and the header you can include (apparently) to get a real "bool". This isn't my point, however -- it should have been there from the beginning. Char is a small int. We all know that. However, "char some_bool = 0;" simply...
15
4331
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 variable. From a forum thread that I've pulled out from mikroBasic, I realized that bool type isn't fully supported. When programming in mikroC, I have been getting an error message of "invalid expression" when I declared a bool type variable,...
0
7959
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8105
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8310
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5466
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3942
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3987
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2451
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1305
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.