Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Bit Field to int

Tags:

c

bit-fields

I have bit field declared this way:

typedef struct morder {
    unsigned int targetRegister : 3;
    unsigned int targetMethodOfAddressing : 3;
    unsigned int originRegister : 3;
    unsigned int originMethodOfAddressing : 3;
    unsigned int oCode : 4;
} bitset;

I also have int array, and I want to get int value from this array, that represents the actual value of this bit field (which is actually some kind of machine word that I have the parts of it, and I want the int representation of the whole word).

like image 825
Shahar Gvirtz Avatar asked Mar 18 '10 09:03

Shahar Gvirtz


People also ask

Can we reference a bit field?

Because bit-fields do not necessarily begin at the beginning of a byte, address of a bit-field cannot be taken. Pointers and non-const references to bit-fields are not possible.

What is bit field data type?

These space-saving structure members are called bit fields, and their width in bits can be explicitly declared. Bit fields are used in programs that must force a data structure to correspond to a fixed hardware representation and are unlikely to be portable.

What is bit field with example?

Bit fields can be used to reduce memory consumption when a program requires a number of integer variables which always will have low values. For example, in many systems storing an integer value requires two bytes (16-bits) of memory; sometimes the values to be stored actually need only one or two bits.

What is bit field operator in C?

Bit Fields in C Language In programming terminology, a bit field is a data structure that allows the programmer to allocate memory to structures and unions in bits in order to utilize computer memory in an efficient manner.


2 Answers

Please, please, do not use a union. Or, rather, understand what you're doing by using a union--preferably before you use one.

As you can see in this answer, do not rely on bitfields to be portable. Specifically for your case, the ordering of the bitfields within a struct is implementation-dependent.

Now, if your question was, how can you print out the bitfield struct as an int, for occasional private review, sure, unions are great. But you seem to want the "actual value" of your bitfields.

So: if you only work on this one machine/compiler combination, and you don't need to rely on the mathematical value of the int, so long as it makes sense, you can use unions. But if you might port your code, or if you need the "actual value" of the int, you need to write bit-manipulation code to get the bit fields into the right int bits.

like image 175
JXG Avatar answered Sep 23 '22 08:09

JXG


You can use a union:

typedef union bitsetConvertor {
    bitset bs;
    uint16_t i;
} bitsetConvertor;

bitsetConvertor convertor;
convertor.i = myInt;
bitset bs = convertor.bs;

Or you can use a cast:

bitset bs = *(bitset *)&myInt;

Or you can use an anonymous struct within a union:

typedef union morder {
    struct {
        unsigned int targetRegister : 3;
        unsigned int targetMethodOfAddressing : 3;
        unsigned int originRegister : 3;
        unsigned int originMethodOfAddressing : 3;
        unsigned int oCode : 4;
    };

    uint16_t intRepresentation;
} bitset;

bitset bs;
bs.intRepresentation = myInt;
like image 22
strager Avatar answered Sep 19 '22 08:09

strager