Solved

How to initialize bitwise parameter ?

Posted on 2014-01-03
4
288 Views
Last Modified: 2014-01-03
struct ParameterDef
{
   ....
   /* bit 0 indicates if the parameter is special or not.  It it's special read remaining bit(s)
       to find the specialty. */
   /* bit 0 -   special 1, not special 0
       bit 1 -   map modbus register value to whole number
       bits 2 - 15    -   reserved for future use
  */    
   unsigned short special_param_bits;
}

Open in new window


There is array of above structure.  

How to initialize special_param_bits variable with symbols instead of numbers ?
For special array entires, I want to indicate that the paramter is special and what specialty it has.  i.e. SPECIAL & MAP_MODBUS_REG
0
Comment
Question by:naseeam
  • 2
  • 2
4 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 200 total points
ID: 39754594
You already have it, the bitwise AND (&) will help you here

#define SPECIAL 1 // LSB == bit 0, or '1 << 0'
#define MAP_MODBUS_REG (1 << 5) // assuming it is bit 5
ParameterDef pd;

pd.special_param_bits = SPECIAL & MAP_MODBUS_REG;

Open in new window

0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
ID: 39754601
Use the bitwise operators with the appropriate flags. In this case, to set it you will use bitwise OR if you have multiple things to set.

e.g.

const short FLAG_SPECIAL =    0x80000000;
const short FLAG_MAP_MODBUS = 0x40000000;

struct ParameterDef instance;
instance.special_param_bits = FLAG_SPECIAL | FLAG_MAP_MODBUS;

Open in new window


Each flag needs to be a power of 2 in its value, since each flag's value represents a bit position within the short. Speaking in bits, your flags look like this:

const short FLAG_SPECIAL =    1000 0000 0000 0000;
const short FLAG_MAP_MODBUS = 0100 0000 0000 0000;

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39754620
For correctness, jkr's suggestion of bitwise AND is actually incorrect. AND is for querying if a bit has been set; OR is used for setting the bit. In other words, 1 & 0 == 0 whereas 1 | 0 == 1.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39755587
*Argh*, it of course has to be 'OR', this is embarassing ;o)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now