Skip to main content
GameDev.net gamedev.net
🔒 Locked

syntax question

Started by jharkey Nov 24, 2001 at 2:02 PM 7 replies 350+ views
Original Post
jharkey
jharkey
hello, i ran across some c/c++ systax i have never seen before, i need some help. it looks like this unsigned int Integer:5; i dont understand the :5 part, is it just initializing it? ive seen stuff like this somevar ? 5 : 2 and i understand that, but not the other one. please help.
Fruny
Fruny
It''s a bitfield of width 5 bits.

It is used when your structure contains flags or when specific bits have a particular meaning. They also allow you to pack a bunch of booleans within a single int. Or a number of variables of size between 1 and 32 bits.

They actually use shifts and logic operators, but hide them from the programmer.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
jharkey
jharkey
so let me see if i understand this, a struct like this

typedef struct {
unsigned short Red:5; // Red
unsigned short Green:5; // Green
unsigned short Blue:5; // Blue
unsigned short Dummy_15:1;
} RGB16;

would only be 16 bits in length, even though you define them all as unsigned shorts, which would normally be 64 bits?
Fruny
Fruny
They''re not unsigned shorts, they''re bitfields, but yoy got the idea. The syntax may be confusing but hey, I didn''t invent it

Note though that the global size will be rounded up (no fractions of a byte), IIRC, it rounds to the smallest 32 bits multiple capable of holding your data (data aligment issues).

And you may pay performancewise for the hidden access logic.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
jharkey
jharkey
thanks for the help, i get it now, sort of. but i still dont understand why you put the unsigned short there, why not like bitfield red:5 or something like that. i know you didnt design it, wierd syntax. it is a pretty cool thing though. thanks for explaining it.
Serge K
Serge K
quote:
Original post by jharkey
but i still dont understand why you put the unsigned short there, why not like bitfield red:5 or something like that.


Because you need to specify some integer type which should contain those bitfields.
By default it uses "int", so red:1 is just 1 bit in 32bit variable (with a lot of unused place).

In your case :
  
typedef struct {
unsigned short Red:5; // Red

unsigned short Green:5; // Green

unsigned short Blue:5; // Blue

unsigned short Dummy_15:1;
} RGB16;


all of these bitfields should be allocated in 16bit continueously (if possible).

  
typedef struct {
unsigned short a:6; // first 6 bit

unsigned short b:6; // next 6 bit (a+b -> 12bit long)

unsigned short c:6; // first 6 bit in the next "short"

} STRUCT;

Fruny
Fruny
Yet, you cannot have a bitfield larger than the supporting integer types. No 33 bits bitfield...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Zeusel
Zeusel
so: 32 int red:1; are 32 booleans stored in a 32 int?
"be fair! ... always"Zeusel
Fruny
Fruny
Yeah, but you''ll have to give each of them a different name.
And I''m pretty sure you cannot have an array of bitfields

However, if you actually want to store bits, have a look at the std::bitset. (as opposed to std::vector which is not a vector of bool ;( ).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.