Solved

what is this definition?

Posted on 2000-03-09
4
223 Views
Last Modified: 2010-04-02
Hi:

What is the meaning of following definition?  I mean
unsigned int used:1,
                  defined: 1,
                   unused_flags : 30;

is it equal to:
unsigned int used = 1;
unsigned int defined = 1;
unsigned int unused_flags = 30;


   class operand {
   
   private:
     unsigned int used    :  1,    
                  defined      :  1,    
                  unused_flags : 30;
   };  // operand

By the way, what are the guidelines to read the appendix GRAMMAR in Bajane Str... 's << C++ programming lanuage>>

thanks,

0
Comment
Question by:tao_shaobin
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
cdepetris earned 50 total points
ID: 2601670
They are bit fields

the class members above would have the following attributes

used is 1 bit in size
deifined is 1 bit in size
unused_flags is 30 bits in size

so the operand class requires 4 bytes (on intel 32 bit machines w/vc6) rather than 12 if all three fields were defined as normal members.

So
used can hold values 0 & 1
defined can hold 0 & 1
unused_flags can hold 0 - 2^30

Note: the sizes allocated for bit fields is completly up to the compiler.

HTH,
Chris


0
 
LVL 1

Expert Comment

by:cdepetris
ID: 2601672
They are bit fields

the class members above would have the following attributes

used is 1 bit in size
deifined is 1 bit in size
unused_flags is 30 bits in size

so the operand class requires 4 bytes (on intel 32 bit machines w/vc6) rather than 12 if all three fields were defined as normal members.

So
used can hold values 0 & 1
defined can hold 0 & 1
unused_flags can hold 0 - 2^30

Note: the sizes allocated for bit fields is completly up to the compiler.

HTH,
Chris


0
 
LVL 1

Expert Comment

by:nutsnuts
ID: 2601677
It is used for flag definiton mostly. The definition you have mentioned means 1 bit for "used", 1 bit for "defined" and 30 bits for unsed_flags. It can reduce some memory usage especially for simple flag definition. You can use them just like normal variables, but remember, it has some length limitation.
0
 

Expert Comment

by:nisa
ID: 2607727
Hi,
  Yes those are bitfields,a comment from cdepetris is acceptable but I will add a few things here.. they(BitField) are used to hold the value of "smaller granularity"(normally it is byte). For example if you declare:
...
operand someOperand;

someOperand.used = 1; //ok;
someOperand.used = 0; //ok;
someOperand.used = 10; //opps the value is not 10 since "used"  can only store value not larger then 1(since it is one bit size.).

The code that is written in this way is not portable (or you have to write a special macro to make it portable) since it is target dependent. ie Intel and motorola used totally different addressing style(Little Endian & Big Endian kind of thing). So, if you target is intel then the bit is filled in the lower byte first.So, "used" is filled in the first bit of the first byte and so forth..

Among other restriction in using BitField is that you can't take the address of bitfield member or make it as a pointer.


Regards,

Nisa
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

864 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

19 Experts available now in Live!

Get 1:1 Help Now