Solved

what is this definition?

Posted on 2000-03-09
4
220 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

6 Experts available now in Live!

Get 1:1 Help Now