|
-
February 6th, 2006, 12:47 AM
#1
declaration with width
Can any one explain me this declaration,
unsigned int is_key : 1;
And where can this be used?
What operations can't be performed and what opeations can be done using this variable?operations like +,-,&,&&..
Thanks in advance,
masg
Last edited by masg; February 6th, 2006 at 12:48 AM.
Reason: more info
-
February 6th, 2006, 02:40 AM
#2
Re: declaration with width
That is a bitfied.
And where can this be used?
You can use them when a whole byte might be a waste. You might find them very usefull when dealing with various communication protocols.
What operations can't be performed and what opeations can be done using this variable?operations like +,-,&,&&
You cannot take the address of the bitfield. You can use any of the +,-,&,&&.
Har Har
-
February 6th, 2006, 02:48 AM
#3
Re: declaration with width
This declares a "C" Bitfield e.g. in a struct. AFAIK only integral types (signed, unsigned, int) are allowed. You basically say that the is_key variable should use 1 bit. Example:
Code:
struct SFoo {
unsigned short hand : 1;
unsigned short finger : 3;
unsigned short age : 7;
};
This structure uses 2 bytes. hand ranges from [0;1], finger from [0;7] and age from [0;127].
You can use at least +/- operators as usual and the values will be kept in range. [edit]I'm not sure about other operators.[/edit]
It is generally not recommended to use bitfields except if it really helps and you can make assumptions about the data types, but it can truely be a porting nightmare. Moreover, although less memory is used, you can expect that access to these data members is somewhat slower.
-
February 6th, 2006, 02:59 AM
#4
Re: declaration with width
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|