Heaps of booleans

This is a discussion on Heaps of booleans within the C++ Programming forums, part of the General Programming Boards category; I've stumbled upon a bit of a problem. I have a struct that contains lots of binary properties (more than ...

  1. #1
    Registered User
    Join Date
    Mar 2002

    Heaps of booleans

    I've stumbled upon a bit of a problem.
    I have a struct that contains lots of binary properties (more than 32) most of which will be false. I want to make a global const array of this struct. Problem: I don't care to write down "false" hundreds of times and count them to know which ones should in fact be a "true".
    If I had less than 32 of these suckers, I could just make an enum with the names of all the properties like so:
    enum Variablenames 
    And make the whole bunch of the bools a single 32 bits (unsigned) integer so I can just jot down a 0 if none of the properties are true, and a (1u<<Variable5 | 1u<<Variable9) if only the fifth and ninth variable should be true and, for example, (0 ~ 1u<<Variable4) if all variables except the fourth should be true. However, there's more than 32 of them so I can't use a single int, and even if I use an array of ints 1u<<Variable33 would equate to 0 so I can't go over 32 variables.
    Is there some simple solution to this problem or will I have to do it the hard way using multiple ints that contain 32 bools each? (which is a pain to read because I need to remember which property is located in which int)
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  2. #2
    Registered User
    Join Date
    Feb 2006
    There is a bitset class in the STL: http://www.cppreference.com/cppbitset/index.html

  3. #3
    Registered User
    Join Date
    Oct 2001


    #include <iostream>
    using namespace std;
    int main()
       unsigned __int64 flags = 1;
       cout << hex << flags << endl;
       flags <<= 32;
       cout << hex << flags << endl;
       return 0;

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    vector<bool> is specialized to be space efficient.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    I think I understand what you're trying to do... The WinAPI uses lots of variables like this... where one bit represents the true / false status of something. The way it's done in the API, all 32 variables are pre-defined in the header. So, the programmer doesn't even have to worry about which bit he's working with! Still, you need to be fairly comfortable with bitwise operators.

    In your case, you'll have to initialize the variables (constants, actually). You can use bit shifting (recommended), or initialize them manually. If you do it manually, use hex... it's a pain with decimal.

    const int ConditionFlag1 = 1; // bit 0
    const int ConditionFlag2 = 2; // bit 1
    const int ConditionFlag3 = 4; // bit 2
    const int ConditionFlag4 = 8; // bit 3
    ... etc.

    Note that bits are counted starting at "bit 0". So in the above scheme, bit 0 represents Condition 1.

    //Set Condition 1 and Condition 3 true, all others false
    int Status = ConditionFlag1 | ConditionFlag3;   // Status = 5, bits 0, 2 are high
    // Set Condition 2 true without affecting other flags
    Status = Status | ConditionFlag2;  // Status = 7, bits 0, 1, 2 high
    // Test Condition 2
    if (Status & ConditionFlag2)
         cout << "Condition 2 is TRUE!"   // Bit 1 is high
    //Clear Condition 2 without affecting others
    Status = Status & (~ContitionFlag2);  //  Force bit 1 low.
    Be careful. An int is not always 32 bits. In order to write good portable code, you have to make use of sizeof() or <limits>.
    Last edited by DougDbug; 02-07-2006 at 01:03 PM.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Thanks for the replies people, especially joni: that was exactly what I was looking for! (although it took me a while to figure out it's part of the std namespace...)

    DougDbug: I had that figured out, but my problem was that I want to fit more than 32 booleans in a single variable which doesn't work very well with 32-bit integers. In fact, you can't even use an explicit constant int bigger than 2^32-1, meaning I HAVE to use bitshifting. (on bitsets, even. Bitshifting a 32-bit integer by 32 spaces just empties the whole thing)

    anonytmouse: a bitset<64> seems to take up 8 bytes of memory. That's plenty of efficient to me. Do vectors use compression?
    Last edited by Boksha; 02-07-2006 at 01:13 PM.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  7. #7
    Registered User
    Join Date
    Jan 2005
    A vector<bool> would probably take up about the same amount of memory, 1 bit per bit. It is specialized for bools just for that reason.

    I think the bitset might make more sense though if you have a specific set of bits you are manipulating. A vector<bool> might be better if you were adding, inserting, and erasing boolean values from the vector.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    bitset and vector<bool> have the same space requirements, with the vector having a bit of constant overhead.

    The difference is that bitset is compile-time sized, while the vector can change size at runtime. This makes the vector more flexible, but the bitset is a bit faster.
    All the buzzt!

    "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. Buffers , heaps , stacks ...
    By BlaX in forum Tech Board
    Replies: 9
    Last Post: 02-17-2009, 02:09 PM
  2. Heaps!
    By Leojeen in forum C Programming
    Replies: 18
    Last Post: 11-26-2008, 12:31 AM
  3. 2d array of booleans
    By eklavya8 in forum C++ Programming
    Replies: 9
    Last Post: 06-27-2008, 02:36 PM
  4. Booleans in C
    By KneeLess in forum C Programming
    Replies: 6
    Last Post: 09-09-2004, 09:47 AM
  5. FAQ heaps
    By face_master in forum FAQ Board
    Replies: 2
    Last Post: 10-06-2001, 11:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21