Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Couldn't bool be better in C++??

Couldn't bool be better in C++??

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
8 Posts 7 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tom_dx
    wrote on last edited by
    #1

    can you just write a class to manipulate bits in an int, so it would contain 32 bool values instead of wasting that one number for 1 or a char for 8 values or double for 64 diff. values, wouldn't this be a better solution? IM PROUD TO BE A GMAIL;

    M C T B B 5 Replies Last reply
    0
    • T tom_dx

      can you just write a class to manipulate bits in an int, so it would contain 32 bool values instead of wasting that one number for 1 or a char for 8 values or double for 64 diff. values, wouldn't this be a better solution? IM PROUD TO BE A GMAIL;

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      no. at best it's confusing.


      Maximilien Lincourt Your Head A Splode - Strong Bad

      1 Reply Last reply
      0
      • T tom_dx

        can you just write a class to manipulate bits in an int, so it would contain 32 bool values instead of wasting that one number for 1 or a char for 8 values or double for 64 diff. values, wouldn't this be a better solution? IM PROUD TO BE A GMAIL;

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        tom_dx wrote: wouldn't this be a better solution? not really. it would be a lot slower, since CPUs can't access bits directly. Cleek | Image Toolkits | Thumbnail maker

        1 Reply Last reply
        0
        • T tom_dx

          can you just write a class to manipulate bits in an int, so it would contain 32 bool values instead of wasting that one number for 1 or a char for 8 values or double for 64 diff. values, wouldn't this be a better solution? IM PROUD TO BE A GMAIL;

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          why don't you do it yourself ? is it so hard ? look at this[^]...


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          1 Reply Last reply
          0
          • T tom_dx

            can you just write a class to manipulate bits in an int, so it would contain 32 bool values instead of wasting that one number for 1 or a char for 8 values or double for 64 diff. values, wouldn't this be a better solution? IM PROUD TO BE A GMAIL;

            B Offline
            B Offline
            basementman
            wrote on last edited by
            #5

            Back in the old days of C when every byte of memory counted, we used to define a structure of bits so that it would be clearly referenced in code and only use a byte of memory. So, to declare six flags in a byte, you would define a struct like this:

            typedef struct _tagBitFlags
            {
            unsigned char Flag1 : 1;
            unsigned char Flag2 : 1;
            unsigned char Flag3 : 1;
            unsigned char Flag4 : 1;
            unsigned char Flag5 : 1;
            unsigned char Flag6 : 1;
            unsigned char unused : 2;
            } BitFlags;

            Then to access the flags, you could write something like this:

            if (BitFlags.Flag1)
            printf("Flag1 is set");
            else
            printf("Flag1 is cleared");

            Note that the value you can set a flag to must be in the bit range that the structure member can hold. This means that if, as in the above example, Flag1 is declared as one bit, it can only hold the values 0 and 1, whereas unused is declared as two bits and can be assigned the values 0, 1, 2, or 3. Hope this doesn't date me too badly....:~  onwards and upwards...

            B 1 Reply Last reply
            0
            • T tom_dx

              can you just write a class to manipulate bits in an int, so it would contain 32 bool values instead of wasting that one number for 1 or a char for 8 values or double for 64 diff. values, wouldn't this be a better solution? IM PROUD TO BE A GMAIL;

              B Offline
              B Offline
              Bob Stanneveld
              wrote on last edited by
              #6

              Why don't you try the bitset from STL[^]? You can easely do the following things:

              typedef std::bitset<8> Flag8;
              typedef std::bitset<11> Flag11;
              // etc..

              Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

              1 Reply Last reply
              0
              • B basementman

                Back in the old days of C when every byte of memory counted, we used to define a structure of bits so that it would be clearly referenced in code and only use a byte of memory. So, to declare six flags in a byte, you would define a struct like this:

                typedef struct _tagBitFlags
                {
                unsigned char Flag1 : 1;
                unsigned char Flag2 : 1;
                unsigned char Flag3 : 1;
                unsigned char Flag4 : 1;
                unsigned char Flag5 : 1;
                unsigned char Flag6 : 1;
                unsigned char unused : 2;
                } BitFlags;

                Then to access the flags, you could write something like this:

                if (BitFlags.Flag1)
                printf("Flag1 is set");
                else
                printf("Flag1 is cleared");

                Note that the value you can set a flag to must be in the bit range that the structure member can hold. This means that if, as in the above example, Flag1 is declared as one bit, it can only hold the values 0 and 1, whereas unused is declared as two bits and can be assigned the values 0, 1, 2, or 3. Hope this doesn't date me too badly....:~  onwards and upwards...

                B Offline
                B Offline
                Blake Miller
                wrote on last edited by
                #7

                Not at all. I am glad you typed it in. Saved me some work :~

                B 1 Reply Last reply
                0
                • B Blake Miller

                  Not at all. I am glad you typed it in. Saved me some work :~

                  B Offline
                  B Offline
                  basementman
                  wrote on last edited by
                  #8

                  Glad to be of service... and glad that I can remember back that far...:doh:  onwards and upwards...

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups