defining data types in C

Soldato
Joined
18 Oct 2002
Posts
8,444
Location
Leamington Spa
Is it possible to define a data type that is a value, n bits big? For what I'm doing I basically want an unsigned 5 bit integer.
 
Inquisitor said:
Why do you need it to be exactly 5 bits? Why not just use a 16-bit integer? :confused:
I'm writing an assembler and the instruction set takes a 5 bit register number.

Just use a char and use a bitmask to get the 5 least significant bits:

char b = something ;
;
int five_bit_value = b & 0x1F ;
This is probably the best idea. Better not use something platform dependent as the lecturers seem to have a habit of not telling us what they're going to compile on.
 
Yep the bitmask things works great. Even works with negative numbers as long as I check they are in the right range.
 
Back
Top Bottom