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. Define BIT level Struct

Define BIT level Struct

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialcode-review
5 Posts 4 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.
  • P Offline
    P Offline
    Paul Farry
    wrote on last edited by
    #1

    I've got a datafile to read from a mainframe system. It's got data store in the bits of each byte. I want to be able to define a struct to extract the bits so I can optimize the bitshiting operations I have to do (It's a big file). I can't find anything that shows how to define a struct to anything lower than a BYTE. Please help. typedef struct ffStockData { BYTE fBucketCount; // I need to be able to expand this to 2 values per 1 byte BYTE ffFiller[104]; } FFSTOCKDATA, *PFFSTOCKDATA;

    S 1 Reply Last reply
    0
    • P Paul Farry

      I've got a datafile to read from a mainframe system. It's got data store in the bits of each byte. I want to be able to define a struct to extract the bits so I can optimize the bitshiting operations I have to do (It's a big file). I can't find anything that shows how to define a struct to anything lower than a BYTE. Please help. typedef struct ffStockData { BYTE fBucketCount; // I need to be able to expand this to 2 values per 1 byte BYTE ffFiller[104]; } FFSTOCKDATA, *PFFSTOCKDATA;

      S Offline
      S Offline
      Serge Krynine
      wrote on last edited by
      #2

      // Try this: #include "stdafx.h" struct Bits { unsigned m_1stBit:1; unsigned m_2ndBit:2; unsigned m_3rdBit:3; }; int main(int argc, char* argv[]) { Bits myBits; myBits.m_1stBit = 1; myBits.m_2ndBit = 0; myBits.m_3rdBit = 1; printf("size of the myBits is: %d\n", sizeof(myBits)); printf("myBits.m_1stBit = %d\n", myBits.m_1stBit); printf("myBits.m_2ndBit = %d\n", myBits.m_2ndBit); printf("myBits.m_3rdBit = %d\n", myBits.m_3rdBit); return 0; }

      M 1 Reply Last reply
      0
      • S Serge Krynine

        // Try this: #include "stdafx.h" struct Bits { unsigned m_1stBit:1; unsigned m_2ndBit:2; unsigned m_3rdBit:3; }; int main(int argc, char* argv[]) { Bits myBits; myBits.m_1stBit = 1; myBits.m_2ndBit = 0; myBits.m_3rdBit = 1; printf("size of the myBits is: %d\n", sizeof(myBits)); printf("myBits.m_1stBit = %d\n", myBits.m_1stBit); printf("myBits.m_2ndBit = %d\n", myBits.m_2ndBit); printf("myBits.m_3rdBit = %d\n", myBits.m_3rdBit); return 0; }

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        Serge Krynine wrote: struct Bits { unsigned m_1stBit:1; unsigned m_2ndBit:2; unsigned m_3rdBit:3; }; That is incorrect, the number after the colon is how many bits to use for that variable, so that struct uses 6 bits total, not 3. The correct code is:

        struct Bits
        {
        unsigned m_1stBit:1;
        unsigned m_2ndBit:1;
        unsigned m_3rdBit:1;
        };

        --Mike-- Latest blog entry: *drool* (Alyson) [May 10] Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber "You have Erica on the brain" - Jon Sagara to me

        S 1 Reply Last reply
        0
        • M Michael Dunn

          Serge Krynine wrote: struct Bits { unsigned m_1stBit:1; unsigned m_2ndBit:2; unsigned m_3rdBit:3; }; That is incorrect, the number after the colon is how many bits to use for that variable, so that struct uses 6 bits total, not 3. The correct code is:

          struct Bits
          {
          unsigned m_1stBit:1;
          unsigned m_2ndBit:1;
          unsigned m_3rdBit:1;
          };

          --Mike-- Latest blog entry: *drool* (Alyson) [May 10] Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber "You have Erica on the brain" - Jon Sagara to me

          S Offline
          S Offline
          Serge Krynine
          wrote on last edited by
          #4

          Oops.

          B 1 Reply Last reply
          0
          • S Serge Krynine

            Oops.

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

            Actually, you should declare your struct with padding out to the width of the datatype that you declare as the bits. EX: struct Bitflags { unsigned char Bit1 : 1; unsigned char Bit1 : 1; unsigned char Bit1 : 1; unsigned char Padding : 5; };

            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