473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

bit field usage question

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
Nov 15 '07 #1
3 2283
ajcrm125 wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '07 #2
Victor Bazarov wrote:
ajcrm125 wrote:
>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)
};
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '07 #3
On Nov 15, 4:35 pm, ajcrm125 <ajcrm...@gmail.comwrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jim Gerland | last post by:
When I access my DB2 table from a W2K machine running PHP using ODBC with the SQL statement: SELECT DOUBLE(BGROUP.PYHGPT.GPPRAM) I get: 2376.92 but when I do the same from my iSeries...
20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
3
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database...
3
by: Dan | last post by:
First, I'm sorry if this question has been asked too many times. I'm new to this news group. The question has to do with the use of popup windows in a web page. I have heard that popup windows...
0
by: Clemens Hintze | last post by:
Hello, I have a question concerning the usage of default constructed std::slice instances. Our company currently validate the GNU-G++ 3.4 compiler against the ISO/IEC 14882:2003 standard for...
2
by: Nu2ASP.NET | last post by:
What I am trying to do is essentially 'flip' the bits, when the user clicks in the checkbox. For example, if the CheckBox appears checked, and the user un-checks it, I want the underlying data...
24
by: funkyj | last post by:
PROBLEM STATEMENT: I want to calculate the byte offset of a field with a struct at compile time without instantiating an instance of the struct at runtime AND I want to do this in an ANSI standard...
5
by: nephish | last post by:
hey there, can anyone tell me if this is legal php/mysql ? i am trying to write a simple function to get a single stat from a single table. eg.. $field = 'phone_number'; $customer = 'fred';
17
by: emma.sax | last post by:
Hi all, I have a form where we would like the user to input their email address twice, to ensure they've typed it correctly, as is found on most sign-ups I'm looking for a solution to the...
26
by: rao | last post by:
On some of the compilers integer size is 2 and on some other it is 4 bytes. My doubt is who decides the size of the integer? is it plainly the compiler? Does OS or Processor also has any control...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.