Dear all: Would somebody can tell me how to declare a 12bits variable in KeilC ? Now I only know use struct to do this. just like
struct OneHalf { unsigned int a: 12; };
but I remember that some easy way exist. So anyone can tell me? Many thanks.
No, you don't - because there isn't one.
You just have to use a 16-bit type, and ignore 4 bits
John,
Just to elaborate to Andy's correct response...
You should shy away from using bit-field declarations at all due to potential code portability issues in the future. Some compilers will treat your declaration:
with a being the lower 12 bits, while some will treat 'a' as the upper 12 bits. Some compilers require either a full 8 or 16 bit to be declared before the declaration will be accepted without warning or error.
In short, it is not a preferred method for handling bit fields, although if declared properly, according to the compiler you are using, is allowed.
For another 'opinion' on the use of bit-fields, see the following link (number 9 - Bit Manipulation):
www.embedded.com/.../0005feat2.htm
'Easy' ways are usually easy for a reason. They usually come with some type of 'baggage' penalty.