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

x
?
Solved

what is this definition?

Posted on 2000-03-09
4
Medium Priority
?
232 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 200 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

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…
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.

721 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