bit field usage question

A

ajcrm125

I'm trying to utilize bitfields to make my file easier. But for some
reason, I can't assign a value to this structure. I'm sure I must be
missing something:

typedef unsigned short WORD;
WORD data;

union uVCTR {
WORD Ypos : 10; // Y position
WORD Ysign : 1; // Y position sign bit
WORD temp : 1; //
WORD opCode : 4; // 0..100 (8 bits)
} VCTR;

VCTR = data;


"error C2679: binary '=' : no operator defined which takes a right-
hand operand of type 'unsigned short' (or there is no acceptable
conversion)"

Thanks for any info.




VCTR = data
 
V

Victor Bazarov

ajcrm125 said:
I'm trying to utilize bitfields to make my file easier. But for some
reason, I can't assign a value to this structure. I'm sure I must be
missing something:

typedef unsigned short WORD;
WORD data;

union uVCTR {
WORD Ypos : 10; // Y position
WORD Ysign : 1; // Y position sign bit
WORD temp : 1; //
WORD opCode : 4; // 0..100 (8 bits)
} VCTR;

VCTR = data;


"error C2679: binary '=' : no operator defined which takes a right-
hand operand of type 'unsigned short' (or there is no acceptable
conversion)"

First off, your 'uVCTR' should probably be a struct and not a union.
Second, you cannot assign an unsigned short to a union or a struct,
their default assignment operator takes only the same type. You might
try

union uVCTR {
struct bits {
WORD Ypos : 10; // Y position
WORD Ysign : 1; // Y position sign bit
WORD temp : 1; //
WORD opCode : 4; // 0..100 (8 bits)
};
WORD whole;
} VCTR;

and then do

VCTR.whole = data;

but using the bit fields after that can cause undefined behaviour.

V
 
V

Victor Bazarov

Victor said:
First off, your 'uVCTR' should probably be a struct and not a union.
Second, you cannot assign an unsigned short to a union or a struct,
their default assignment operator takes only the same type. You might
try

union uVCTR {
struct bits {
WORD Ypos : 10; // Y position
WORD Ysign : 1; // Y position sign bit
WORD temp : 1; //
WORD opCode : 4; // 0..100 (8 bits)
};

This is nonsense of course, should be

struct {
WORD Ypos : 10; // Y position
WORD Ysign : 1; // Y position sign bit
WORD temp : 1; //
WORD opCode : 4; // 0..100 (8 bits)
} bits;
WORD whole;
} VCTR;

and then do

VCTR.whole = data;

but using the bit fields after that can cause undefined behaviour.

V
 
J

James Kanze

I'm trying to utilize bitfields to make my file easier. But for some
reason, I can't assign a value to this structure. I'm sure I must be
missing something:

Several things, actually.
typedef unsigned short WORD;
WORD data;
union uVCTR {
WORD Ypos : 10; // Y position
WORD Ysign : 1; // Y position sign bit
WORD temp : 1; //
WORD opCode : 4; // 0..100 (8 bits)
} VCTR;

First, I'm not sure what bitfields are supposed to mean in a
union. I can't find anything offhand, but I would have assumed
that it was illegal. (There's a guarantee somewhere that all of
the elements of a union have the same address. Bitfields don't
have an address.)

Second, supposing you mean struct instead of union, you
mentioned files. Bitfield's define a purely internal format,
and can't be used when external formats are involved.
VCTR = data;
"error C2679: binary '=' : no operator defined which takes a right-
hand operand of type 'unsigned short' (or there is no acceptable
conversion)"

And finally, why should it work? On one side of the assignment,
you have a class type (uVCTR), on the other an unsigned short.
Two totally unrelated types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top