Define BIT level Struct
-
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;
-
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;
// 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; }
-
// 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; }
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
-
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
Oops.
-
Oops.
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; };