Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How bool data type is implemented in C++

Reply
Thread Tools

How bool data type is implemented in C++

 
 
a2z
Guest
Posts: n/a
 
      08-22-2007
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.

 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      08-22-2007
On Aug 22, 3:34 pm, a2z <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-22-2007
Neelesh Bodas wrote:
> On Aug 22, 3:34 pm, a2z <(E-Mail Removed)> wrote:
>> 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


 
Reply With Quote
 
Neelesh Bodas
Guest
Posts: n/a
 
      08-22-2007
On Aug 22, 4:43 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> Neelesh Bodas wrote:
> > On Aug 22, 3:34 pm, a2z <(E-Mail Removed)> wrote:
> >> 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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-22-2007
Neelesh Bodas wrote:
> On Aug 22, 4:43 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> Neelesh Bodas wrote:
>>> On Aug 22, 3:34 pm, a2z <(E-Mail Removed)> wrote:
>>>> 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


 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      08-23-2007
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?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-23-2007
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


 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      08-23-2007
On Aug 22, 3:19 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> Neelesh Bodas wrote:
> > On Aug 22, 4:43 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> >> Neelesh Bodas wrote:
> >>> On Aug 22, 3:34 pm, a2z <(E-Mail Removed)> wrote:
> >>>> 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.

 
Reply With Quote
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      08-23-2007
On 2007-08-23 16:38, terminator wrote:
> On Aug 22, 3:19 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> Neelesh Bodas wrote:
>> > On Aug 22, 4:43 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> >> Neelesh Bodas wrote:
>> >>> On Aug 22, 3:34 pm, a2z <(E-Mail Removed)> wrote:
>> >>>> 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
 
Reply With Quote
 
Rajesh S R
Guest
Posts: n/a
 
      08-23-2007
On Aug 23, 6:53 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
enum promote to bool type rather than Integer type? FE C++ 6 08-04-2009 03:21 PM
non-aggregate type bool Christian Christmann C++ 3 03-25-2005 07:14 PM
ldap DirectoryServices.DirectoryEntry System.NotImplementedException: Handling of this ADSVALUE type is not yet implemented (type = 0xb). m96 ASP .Net Web Services 4 02-22-2005 06:11 AM
Cannot implicitly convert type 'object' to 'bool' Error Patrick Olurotimi Ige ASP .Net 2 01-25-2005 02:13 AM



Advertisments