Smaller than a byte
-
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
-
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
How are you packing the structure?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
How are you packing the structure?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
standard
#pragma pack ( push, 1 )
Waldermort
-
standard
#pragma pack ( push, 1 )
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
-
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
-
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
Doesn't Upper and Lower use the same bits? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Doesn't Upper and Lower use the same bits? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Yeah, I just realised, I'm still trying to get it...
Waldermort
-
Yeah, I just realised, I'm still trying to get it...
Waldermort
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:
-
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
have you looked at bitfields ?
-
have you looked at bitfields ?
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
-
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
Original struct
struct aablock { BYTE Flag : 1; BYTE State : 3; };
Original array of size 512MAXSIZE = 512 aablock aa[MAXSIZE];
Modified Structstruct 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 structbbblock 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; }
-
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:
-
Original struct
struct aablock { BYTE Flag : 1; BYTE State : 3; };
Original array of size 512MAXSIZE = 512 aablock aa[MAXSIZE];
Modified Structstruct 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 structbbblock 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; }
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
-
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
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.