Is there a way to define a 3 bit or 12 bit data type in C/C++. I need these for defining my own packet having different bit length fields.
3 bit data type in C/C++
Collapse
X
-
Tags: None
-
In C++ you define a class with operator overloads that make the object appear to have 3 bits. Inside the class you probably have an unsigned int or char and the member functions just uses 3 bits.
Your class is a user-defined type.
In C you write a series of functions where you pass in, say an unsigned int, and the function operates on just 3 bits. Then you call the functions as needed in the program.
The most you can do in C is typedef the unsigned int to be your 3 bit type but you must never use this unsigned int outside the functions written to manage the 3 bits. Not a good solution but C is very limited.
Comment