4 bit datatype?
-
Hi, I need to define a datatype with a size of 4 bit within a struct. char has a size of 8 bit and thats the smallest type I know ... So what else should I do? I really dont know how to search for this, sorry. btw: I think I saw something like "unsigned short varName:4" in a code-snipet. Does this mean the varName holds 4bit of data? Thanks in advance!
-
Hi, I need to define a datatype with a size of 4 bit within a struct. char has a size of 8 bit and thats the smallest type I know ... So what else should I do? I really dont know how to search for this, sorry. btw: I think I saw something like "unsigned short varName:4" in a code-snipet. Does this mean the varName holds 4bit of data? Thanks in advance!
_NielsB wrote:
So what else should I do?
Something like:
struct
{
char c:4;
};
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
_NielsB wrote:
So what else should I do?
Something like:
struct
{
char c:4;
};
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
DavidCrow wrote:
Something like: struct{ char c:4;};
This is indeed a respected method inkeeping with the language. However it also depends on what the four bit data type is to be used for, Is packing on a Four Bit boundary required.If a lot of manipulating code is to be written, or an array of nibbles needs to be addressed, maybe a more transparent way is Masking and Shifting, and a Raft of Macro's to cover it. e.g.: #define LO_NIBBLE(x) ((x)&0xFF) #define HI_NIBBLE(x) (((x)>4))&0xFF) #define GET_ARR_ITEM(Arr,N) ((((N)&1)==0)?LO_NIBBLE((Arr)[(N)/2]:HI_NIBBLE((Arr)[(N)/2]) And so on ad infinitum Even further, depending on the importance and complexity of the grander problem to be solved, (and the Time and resources available)one could conceive of a new class, CNibble with an entire gammet of overloaded operators. Come to think of it, why not also a Class CHalfNibble and CBit. Hope this is of help, :) regards
LateNightsInNewry