How bool data type is implemented in C++

Discussion in 'C++' started by a2z, Aug 22, 2007.

  1. a2z

    a2z Guest

    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.
     
    a2z, Aug 22, 2007
    #1
    1. Advertising

  2. On Aug 22, 3:34 pm, a2z <> 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
     
    Neelesh Bodas, Aug 22, 2007
    #2
    1. Advertising

  3. Neelesh Bodas wrote:
    > On Aug 22, 3:34 pm, a2z <> 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
     
    Victor Bazarov, Aug 22, 2007
    #3
  4. On Aug 22, 4:43 pm, "Victor Bazarov" <> wrote:
    > Neelesh Bodas wrote:
    > > On Aug 22, 3:34 pm, a2z <> 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
     
    Neelesh Bodas, Aug 22, 2007
    #4
  5. Neelesh Bodas wrote:
    > On Aug 22, 4:43 pm, "Victor Bazarov" <> wrote:
    >> Neelesh Bodas wrote:
    >>> On Aug 22, 3:34 pm, a2z <> 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
     
    Victor Bazarov, Aug 22, 2007
    #5
  6. 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?
     
    Juha Nieminen, Aug 23, 2007
    #6
  7. 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
     
    Victor Bazarov, Aug 23, 2007
    #7
  8. a2z

    terminator Guest

    On Aug 22, 3:19 pm, "Victor Bazarov" <> wrote:
    > Neelesh Bodas wrote:
    > > On Aug 22, 4:43 pm, "Victor Bazarov" <> wrote:
    > >> Neelesh Bodas wrote:
    > >>> On Aug 22, 3:34 pm, a2z <> 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.
     
    terminator, Aug 23, 2007
    #8
  9. On 2007-08-23 16:38, terminator wrote:
    > On Aug 22, 3:19 pm, "Victor Bazarov" <> wrote:
    >> Neelesh Bodas wrote:
    >> > On Aug 22, 4:43 pm, "Victor Bazarov" <> wrote:
    >> >> Neelesh Bodas wrote:
    >> >>> On Aug 22, 3:34 pm, a2z <> 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
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 23, 2007
    #9
  10. a2z

    Rajesh S R Guest

    On Aug 23, 6:53 pm, "Victor Bazarov" <> 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.
     
    Rajesh S R, Aug 23, 2007
    #10
  11. On 2007-08-23 17:02, Rajesh S R wrote:
    > On Aug 23, 6:53 pm, "Victor Bazarov" <> 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.


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

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 23, 2007
    #11
  12. Erik Wikström wrote:
    > On 2007-08-23 17:02, Rajesh S R wrote:
    >> On Aug 23, 6:53 pm, "Victor Bazarov" <> 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.

    >
    > 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...
     
    Juha Nieminen, Aug 23, 2007
    #12
  13. a2z

    Old Wolf Guest

    On Aug 24, 2:38 am, terminator <> wrote:
    > 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).
     
    Old Wolf, Aug 28, 2007
    #13
  14. a2z

    terminator Guest

    On Aug 28, 2:38 am, Old Wolf <> wrote:
    > On Aug 24, 2:38 am, terminator <> wrote:
    >
    > > 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.
     
    terminator, Aug 29, 2007
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Olurotimi Ige

    Cannot implicitly convert type 'object' to 'bool' Error

    Patrick Olurotimi Ige, Jan 25, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    12,216
    Patrick Olurotimi Ige
    Jan 25, 2005
  2. Christian Christmann

    non-aggregate type bool

    Christian Christmann, Mar 25, 2005, in forum: C++
    Replies:
    3
    Views:
    1,755
    Donovan Rebbechi
    Mar 25, 2005
  3. Jan Decaluwe

    Checking whether bool is a type

    Jan Decaluwe, Jul 27, 2003, in forum: Python
    Replies:
    2
    Views:
    279
    Christos TZOTZIOY Georgiou
    Jul 27, 2003
  4. ssylee

    getting around lack of bool type support

    ssylee, Dec 29, 2007, in forum: C Programming
    Replies:
    25
    Views:
    2,555
  5. FE
    Replies:
    6
    Views:
    524
    Balog Pal
    Aug 4, 2009
Loading...

Share This Page