Solved

what is this definition?

Posted on 2000-03-09
4
229 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
[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
  • 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

623 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