Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What is this syntax?

Posted on 1999-07-12
11
Medium Priority
?
248 Views
Last Modified: 2010-08-05
typedef struct
     {
     unsigned int fAdvise:1 ;  //what is this syntax with the ":1"
     unsigned int fDeferUpd:1 ;  //here too
     unsigned int fAckReq:1 ;   //and here
     unsigned int dummy:13 ;   //and here ":13"
     long         lPopPrev ;
     }
     POPADVISE ;

What does the syntax above with the semicolon mean?
A good explanation please (it may be simple, I just haven't seen it before :)

Laminamia
0
Comment
Question by:Laminamia063099
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 3

Accepted Solution

by:
Iexpert earned 60 total points
ID: 1263515
You mean the colon ?

This is known as a bitfield.
You specify the number of bits in an int for example
"belong" to a variable, so that when you do
StructVar.fAdvise = 1;

the compiler behind the scenes does:
StructVar.fAdvise = (StructVar.fAdvise & 0xFFFE) | 0x0001;

Note bitfields are highly non portable and shouldn't
really be used if there's any chance you may need
to use the code on a different platform.

0
 
LVL 3

Author Comment

by:Laminamia063099
ID: 1263516
So, the bitfield states how many bits are to be allocated for that field of the structure?  

Thus, lDummy has 13 bits, and fDeferUpd has 1 bit?

(I'm modifying some code, and for this reason I came across this syntax.  Thanks for the note about it's low portability).
0
 
LVL 3

Expert Comment

by:marcjb
ID: 1263517
It looks like whoever wrote this structure was trying to use dummy to align the variable lPopPrev on a word boundry (1+1+1+13 = 16).  You may find what follows to be easier to work with, and it takes the same amount of memory.  Use the #defines with the variable theFlag to keep track of what you need.

#define  fAdvise    0x0001
#define  fDeferUpd  0x0002
#define  fAckReq    0x0004

typedef struct
  {
  int theFlag;
  long         lPopPrev ;
  } POPADVISE ;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:Laminamia063099
ID: 1263518
>>marcjb
How would you use those defines in the case you state?
0
 
LVL 3

Expert Comment

by:marcjb
ID: 1263519
If you want to set a condition, use OR.
If you want to test a condition, use AND.

For example:

To set the variable theFlag to fAckReq

theFlag |= fAckReq;
/* Same as  theFlag = theFlag | fAckReq; */

This has the effect of turing on the bit that represents fAckReq, and not changing the other bits.

To test, use AND.

if (1 == (theFlag & fAckReq)) /* Don't really need == 1 */
    THEN THE FLAG IS SET

You can now set or test any of the conditions without effecting any of the other bits.  Also, you can have as many conditions as there are bits in the variable.  Assuming the int is 16 bits, that is 16 conditions.

#define  COND1  0x0001
#define  COND2  0x0002
#define  COND3  0x0004
#define  COND4  0x0008
#define  COND5  0x0010
#define  COND6  0x0020
and so on ...
0
 
LVL 3

Author Comment

by:Laminamia063099
ID: 1263520

#define  COND1  0x0001
#define  COND2  0x0002
#define  COND3  0x0004
#define  COND4  0x0008
#define  COND5  0x0010
#define  COND6  0x0020

Why do the conditions go doubling in offset?  If it is a bit-by-bit check, why not:

#define  COND1  0x0001
#define  COND2  0x0002
#define  COND3  0x0003
#define  COND4  0x0004
#define  COND5  0x0005
#define  COND6  0x0006
...
#define  COND16 0x0010
0
 
LVL 3

Expert Comment

by:marcjb
ID: 1263521
 HEX          Binary
0x0001      0000000000000001
0x0002      0000000000000010
0x0004      0000000000000100
0x0008      0000000000001000
0x0010      0000000000010000
Note how only one bit is set.

If you use
0x0003      0000000000000011  <- Two bits are set!
same with
0x0005      0000000000000101

You are simply using the powers of 2 (doubling, like you said), because that is the way the bits are stored.
0
 
LVL 3

Author Comment

by:Laminamia063099
ID: 1263522
I understand hex representation, just was lost in this case.  Now I understand why they are doubled, to preserve all other bits when changing one.  

Also, how do the & AND and the | OR operators work.  I understand logic, but how does:

myFlag = myFlag | COND1;

change the first bit in this integer to 1?

0
 
LVL 3

Expert Comment

by:marcjb
ID: 1263523
let myFlag = 0
let COND1 be defined as 0x0001

myFlag = myFlag | COND1;
myFlag =  0x0000000000000000
        | 0x0000000000000001
          ------------------
          0x0000000000000001

So, myFlag now has bit one set.

As another example, assume myFlag = 4

myFlag = 0x0000000000000100

Now, let's OR it with COND1

myFlag =  0x0000000000000100
        | 0x0000000000000001
          ------------------
          0x0000000000000101

myFlag now equals 5 (0x0005), which means two of the bits are set.

If you now tested to see if bit 3 was set, you would get the following

if ( myFlag & COND3 )
 WHICH IS
if (  0x0000000000000101
    & 0x0000000000000100 )
    ---------------------
      0x0000000000000100
 This is not equal to zero, so it passes the if clause.
 See below about a mistake I made earlier when describing &


*****
I made one mistake earlier when describing the & function.

if (1 == (theFlag & fAckReq)) /* Don't really need == 1 */
  THIS IS NOT CORRECT

SHOULD BE
if (theFlag & fAckReq)
 OR
if ((theFlag & fAckReq) > 0)
 OR
if ((theFlag & fAckReq) != 0)
0
 
LVL 3

Author Comment

by:Laminamia063099
ID: 1263524
It was incorrect because what you said "== 1".  It could equal any value (in this case a power of 2) not equal to zero to be true.  I completely understand it now.  I don't know why it took me so long to see where you where coming from.  

Thanks a lot for your help.  I would like to give you some points, but lexpert answered my question that i did ask and I'm at zero points right now.  How can I repay you?
0
 
LVL 3

Expert Comment

by:marcjb
ID: 1263525
Don't worry about it.  Just glad to help out :)  Good luck,
marcjb
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

722 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