473,396 Members | 2,050 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,396 software developers and data experts.

Can this be done with "Union"

Id like to group 16 booleans into a u16 such that I can either set all
16 variables at once with just a single u16 assigment, or I can
individually change a bit by setting that boolean manually. Either
way, the same 16 bits is occupied in memory.

Something like...

typedef struct
{
bool bSetLed15;
bool bSetLed15;
bool bSetLed14;
bool bSetLed13;
bool bSetLed12;
bool bSetLed11;
bool bSetLed10;
bool bSetLed09;
bool bSetLed08;
bool bSetLed07;
bool bSetLed06;
bool bSetLed05;
bool bSetLed04;
bool bSetLed03;
bool bSetLed02;
bool bSetLed01;
bool bSetLed00;
} sLEDS;

typedef struct
{
union
{
alt_u16 uAllLeds;
sLEDS sSetSingleLed;
} uSelection;

} CONTROL;

The problem is when I set the 16 bit variable, uAllLeds, the
individual bits in the sLEDs structure (sSetSingleLed) arent set
correctly. For example, ideally uAllLeds = 0x0300; would also set
bSetLed09 = 1 and bSetLed08 = 1, with all other values to 0.

Alternatively, if all leds are 0, and I set bSetLed15 = 1, I would
like uAllLeds to automatically change to 0x8000.

Is what Im trying to do possible using Union?
Dec 7 '07 #1
1 1680
be*****@hotmail.com writes:
If not by union, how is this kind of thing normally done?
uint16_t foo;
foo = 0x1234; /* Set the whole thing */
foo |= 1u << 8; /* Set bit 8. */
foo &= ~(1u << 3); /* Unset bit 3. */
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons
Dec 8 '07 #2

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

Similar topics

10
by: KENNY L. CHEN | last post by:
Dear experts, I have two tables in my Oracle 8i database: TEST (COL1,COl2,REC_NO) and TEST1 (COL1,COL2,REC_NO). Both tables are unique-indexed on (COL1,COL2,REC_NO). I think the following...
0
by: Marek Lewczyk | last post by:
Hello, Currently I'm testing my app using MySQL 4.1.0 version, and I have a strange error during execution a union query. (SELECT IF(_DAT.pri <=> null, null, ROUND(_DAT.pri/1.22)) AS pri_net,...
2
by: thomasfj | last post by:
Hi, Accroding to MS documentation the use of union and list elements in XSD simpleType (valid according to W3C standards) is valid used as an dataset schema...but it's not!! When loading the...
0
by: Codex Twin | last post by:
hello group: The following is a fragment from a schema which defines the EWethnicCategoryStructure type. As you can see, its type is defined by the SimpleType enumeration EWethnicCategoryType....
1
by: CrystalDBA | last post by:
I usually design applications in SQL Server and Crystal Reports. I now need to create a crystal report on an MS Access database. I have two tables: Services: Date datetime Entry text...
3
by: usr.root | last post by:
hi,who knows how does the compiler work after it see the "union" word in your code .
4
by: Yair | last post by:
Hi, I need to shift the bits of a float type. In order to do so, I declared the following struct: private struct unionIntFloatType { public float m_asFloat;
3
by: John Sasso | last post by:
In my Yacc .y file I defined: %union { int value; struct Symbol Sym; } The Symbol struct I defined in a header file I #included in the Prologue section of the .y file as:
7
by: php_mysql_beginer911 | last post by:
Hi .. hope someone will help i am trying to figure it out why i cannot post string "union select" every time i try to post data which content union and select .. the page doesn't get posted and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.