Solved

what is this definition?

Posted on 2000-03-09
4
227 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

752 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