Thread: Why is bool 1 byte?

  1. #1
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190

    Why is bool 1 byte?

    Why is bool 1 byte and not 1 bit? Wouldn't it have been easier (and more efficient - less memory needed and so on) with one bit? In the end - it is either 1 or 0 (true or false).

    (Have also heard that some versions of Visual C++ define bool with 4 bytes - why did they do that at all??)
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    You can't allocate less than a byte.

    >>define bool with 4 bytes

    word alignment, your compiler will/can do this in different scenarios to make access faster. You can give pre-processor directives to force it to pack things though.

  3. #3
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    You can't allocate less than a byte.
    Omg, you are totally right Again, I think I must review some memory management basics. Thanx alot.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Its 1 bit 0 or .

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Its 1 bit 0 or .
    Pardon?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Bit access (ie bit fields) is slower then byte access. Mainly because it still has to access the bit and then figure which bit you are looking for.

    Also remember that everything has to be >= ( sizeof(char) == 1 )

  7. #7
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    word access is faster on word-aligned machines (most all you are likely to see). If you want to save space, you can do your own bit fiddling. If you are interested in a bitmap class, I created one here: http://sol-biotech.com/code/CPE/PrimeCPE.hpp. It is much slower, but uses much less space. The implementation I used it for was to find prime numbers and when compared to an STL vector it is much slower.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Also remember that everything has to be >= (
    Whoa... Anger... Tension...

  9. #9
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Quote Originally Posted by sean_mackrory
    Whoa... Anger... Tension...
    For what?

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Thantos inadvertently made an angry face out of his greater-than-or-equal-to equation.

  11. #11
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    If you have a lot of bools, you could use bitwise operations to operate on single bits, thereby make 8 flags out of 1 bool. Of course it might not be worth the trouble for all the good it does. Unless you've got about a hundred or more, which isn't likely.
    Code:
    void function(void)
     {
      function();
     }

  12. #12
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Thanks alot guys This really got me on the right track again...
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You could use a std::bitset, or a boost::dynamic_bitset if you don't know the size at compile time.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM