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. Smaller than a byte

Smaller than a byte

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelp
14 Posts 9 Posters 0 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.
  • W Waldermort

    I have aa array of struct in my app, each struct is the size of a byte. My problem is that this array is very large, up to 256mb if the machine can cope with it. I know each struct is small, but I can half that size. I need 1 bit for a flag and 3 bits for a state. How can I declare the stuct to effectivly have two struct members each of which is 4 bits in size?

    struct block
    {
    BYTE Flag : 1;
    BYTE State : 3;
    };

    This is what I currently have, but it's size is still 1 byte even though only 4 bits are being used.

    Waldermort

    L Offline
    L Offline
    led mike
    wrote on last edited by
    #5

    WalderMort wrote:

    This is what I currently have, but it's size is still 1 byte even though only 4 bits are being used.

    I believe the minimum size of your structure is the size of the base type, in this case BYTE.

    1 Reply Last reply
    0
    • W Waldermort

      This seems to work, but accessing the members is a pain

      struct block
      {
      union
      {
      struct Upper
      {
      BYTE State : 1;
      BYTE Repaint : 3;
      };
      struct Lower
      {
      BYTE State : 1;
      BYTE Repaint : 3;
      };
      } U;
      }

      Waldermort

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #6

      Doesn't Upper and Lower use the same bits? Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      W 1 Reply Last reply
      0
      • M Mark Salsbery

        Doesn't Upper and Lower use the same bits? Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        W Offline
        W Offline
        Waldermort
        wrote on last edited by
        #7

        Yeah, I just realised, I'm still trying to get it...

        Waldermort

        M 1 Reply Last reply
        0
        • W Waldermort

          Yeah, I just realised, I'm still trying to get it...

          Waldermort

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #8

          AFAIK, byte granularity is all you're going to get unless you pack two pairs in each struct:

          struct block
          {
          BYTE State : 1;
          BYTE Repaint : 3;
          BYTE State2 : 1;
          BYTE Repaint2 : 3;
          };

          Do you have to have a struct type?  Can you wrap the array in a class and provide accessors to packed State/Repaint bits instead?

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          T 1 Reply Last reply
          0
          • W Waldermort

            I have aa array of struct in my app, each struct is the size of a byte. My problem is that this array is very large, up to 256mb if the machine can cope with it. I know each struct is small, but I can half that size. I need 1 bit for a flag and 3 bits for a state. How can I declare the stuct to effectivly have two struct members each of which is 4 bits in size?

            struct block
            {
            BYTE Flag : 1;
            BYTE State : 3;
            };

            This is what I currently have, but it's size is still 1 byte even though only 4 bits are being used.

            Waldermort

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

            have you looked at bitfields ?

            image processing toolkits | batch image processing

            W 1 Reply Last reply
            0
            • C Chris Losinger

              have you looked at bitfields ?

              image processing toolkits | batch image processing

              W Offline
              W Offline
              Waldermort
              wrote on last edited by
              #10

              Funny you say that, I was just playing around with the bitfields and found my solution. Though I'm not sure of how portable it may be.

              struct block
              {
              union
              {
              struct Upper
              {
              BYTE Pack : 4;
              BYTE State : 3;
              BYTE Repaint : 1;
              };
              struct Lower
              {
              BYTE State : 3;
              BYTE Repaint : 1;
              };

                  BYTE Total;
              } U;
              

              };

              Waldermort

              1 Reply Last reply
              0
              • W Waldermort

                I have aa array of struct in my app, each struct is the size of a byte. My problem is that this array is very large, up to 256mb if the machine can cope with it. I know each struct is small, but I can half that size. I need 1 bit for a flag and 3 bits for a state. How can I declare the stuct to effectivly have two struct members each of which is 4 bits in size?

                struct block
                {
                BYTE Flag : 1;
                BYTE State : 3;
                };

                This is what I currently have, but it's size is still 1 byte even though only 4 bits are being used.

                Waldermort

                S Offline
                S Offline
                Sameerkumar Namdeo
                wrote on last edited by
                #11

                Original struct struct aablock { BYTE Flag : 1; BYTE State : 3; }; Original array of size 512 MAXSIZE = 512 aablock aa[MAXSIZE]; Modified Struct struct bbblock { BYTE FlagOdd : 1; BYTE StateOdd : 3; BYTE FlagEven : 1; BYTE StateEven : 3; }; try to suffle the elements if size of this struct > sizeof(BYTE) if you are having an array of size say 512 then you may declare array with half of that size. Each element will store 2 elements of original struct bbblock bb[MAXSIZE/2]; j = -1; for(i = 0; i < MAXSIZE/2; ++i) { j++ // j = 0, 2, 4, 6... bb[i].FlagEven = aa[j].Flag; bb[i].StateEven = aa[j].State; j++; // j = 1, 3, 5..... bb[i].FlagOdd = aa[j].Flag; bb[i].StateOdd = aa[j].State; }

                Z 1 Reply Last reply
                0
                • M Mark Salsbery

                  AFAIK, byte granularity is all you're going to get unless you pack two pairs in each struct:

                  struct block
                  {
                  BYTE State : 1;
                  BYTE Repaint : 3;
                  BYTE State2 : 1;
                  BYTE Repaint2 : 3;
                  };

                  Do you have to have a struct type?  Can you wrap the array in a class and provide accessors to packed State/Repaint bits instead?

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  T Offline
                  T Offline
                  thestrat
                  wrote on last edited by
                  #12

                  I think struct is creatin a problem. try doing it by using union inside a struct. i think it will work. tHeStRat

                  1 Reply Last reply
                  0
                  • S Sameerkumar Namdeo

                    Original struct struct aablock { BYTE Flag : 1; BYTE State : 3; }; Original array of size 512 MAXSIZE = 512 aablock aa[MAXSIZE]; Modified Struct struct bbblock { BYTE FlagOdd : 1; BYTE StateOdd : 3; BYTE FlagEven : 1; BYTE StateEven : 3; }; try to suffle the elements if size of this struct > sizeof(BYTE) if you are having an array of size say 512 then you may declare array with half of that size. Each element will store 2 elements of original struct bbblock bb[MAXSIZE/2]; j = -1; for(i = 0; i < MAXSIZE/2; ++i) { j++ // j = 0, 2, 4, 6... bb[i].FlagEven = aa[j].Flag; bb[i].StateEven = aa[j].State; j++; // j = 1, 3, 5..... bb[i].FlagOdd = aa[j].Flag; bb[i].StateOdd = aa[j].State; }

                    Z Offline
                    Z Offline
                    zakkas2483
                    wrote on last edited by
                    #13

                    Good Ans.... But i have some confusion. In That code what is the meaning of This BYTE Flag : 1; Line. How we can use one bit from that Byte? i got the error on that each line.. Please solve my doubt. :) hiren Thakkar

                    I 1 Reply Last reply
                    0
                    • Z zakkas2483

                      Good Ans.... But i have some confusion. In That code what is the meaning of This BYTE Flag : 1; Line. How we can use one bit from that Byte? i got the error on that each line.. Please solve my doubt. :) hiren Thakkar

                      I Offline
                      I Offline
                      Iain Clarke Warrior Programmer
                      wrote on last edited by
                      #14

                      If you read the other answers, you'll see someone refers to bitfields. That's the important clue - you can look that up in your c++ book, as the full answer is too long for me to bother typing here! Iain.

                      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