1

Possible Duplicate:
What does this C++ code mean?
What does ‘unsigned temp:3’ mean?

I have seen one small c program recently.There in that program,the structure was declared in this manner which I could not understand.

struct
{
mynode *node;
unsigned vleft :1; 
unsigned vright :1; 
}save[100];

Here node is pointer to some other structure.

Can some one please explain what unsigned vleft :1; unsigned vright :1; are? And I could not find any datatype assigned to vleft and vright.What is the reason for that?

Thanks.

Community
  • 1
  • 1
starkk92
  • 5,754
  • 9
  • 43
  • 59

3 Answers3

5

The default type assumed here is unsigned int, this is assumed by the compiler when you specify just unsigned.

The bitfield syntax unsigned vleft : 1 specifies the width in bits of the data field, in this situation it means that it's a single bit flag (which can be either 0 or 1). This is used to pack many fields of the structure in less bits (when you don't need to waste, like in this case, a whole char or int for just storing a flag).

Jack
  • 131,802
  • 30
  • 241
  • 343
  • I am new to this as well, but if the value will be represented by only one bit, wouldn't that make the value impossible to be negative? Why would one specify unsigned in this case? – Kevin Jan 15 '13 at 18:28
  • @Kevin if you specified `signed int` the value could be either `0` or `-1`. – ouah Jan 15 '13 at 18:29
  • @Kevin: in two's complement representation the most significant bit contributes as a negative power and in a 1-bit wide field the only bit is the most significant. – Jack Jan 15 '13 at 18:40
1

The int datatype is implied and the :1 part means these members are only 1-bit values.

James
  • 9,064
  • 3
  • 31
  • 49
1

vleft and vright can hold only 1 bit int data (ie. 0 or 1).

unsigined is the short form of unsigned int. Below are the short forms of some of the C data types.

short = short int = signed short = signed short int
unsigned short = unsigned short int
int = signed int
unsigned = unsigned int
long = long int = signed long = signed long int
unsigned long = unsigned long int
long long = long long int = signed long long = signed long long int
unsigned long = unsigned long int
rashok
  • 12,790
  • 16
  • 88
  • 100