Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of the : operator in C [duplicate]

Tags:

c

colon

Possible Duplicates:
What does ‘: number’ after a struct field mean?
What does ‘unsigned temp:3’ means

I hate to ask this type of question, but it's really bugging me, so I will ask:

What is the function of the : operator in the code below?

#include <stdio.h>  struct microFields {   unsigned int addr:9;   unsigned int cond:2;   unsigned int wr:1;   unsigned int rd:1;   unsigned int mar:1;   unsigned int alu:3;   unsigned int b:5;   unsigned int a:5;   unsigned int c:5; };  union micro {   unsigned int microCode;   microFields code; };  int main(int argc, char* argv[]) {   micro test;   return 0; }  

If anyone cares at all, I pulled this code from the link below: http://www.cplusplus.com/forum/beginner/15843/

I would really like to know because I know I have seen this before somewhere, and I want to understand it for when I see it again.

like image 527
Russel Avatar asked Jul 22 '10 05:07

Russel


People also ask

What is the purpose of a copy assignment operator?

A trivial copy assignment operator makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially copy-assignable.

What is a copy assignment operator C++?

Copy constructor. Assignment operator. It is called when a new object is created from an existing object, as a copy of the existing object. This operator is called when an already initialized object is assigned a new value from another existing object. It creates a separate memory block for the new object.

Is == an assignment operator?

What is the difference between = (Assignment) and == (Equal to) operators. The “=” is an assignment operator is used to assign the value on the right to the variable on the left. The '==' operator checks whether the two given operands are equal or not. If so, it returns true.

What is the use of & operator in case of copy constructor in C++?

The Copy constructor and the assignment operators are used to initializing one object to another object. The main difference between them is that the copy constructor creates a separate memory block for the new object. But the assignment operator does not make new memory space.


1 Answers

They're bit-fields, an example being that unsigned int addr:9; creates an addr field 9 bits long.

It's commonly used to pack lots of values into an integral type. In your particular case, it defining the structure of a 32-bit microcode instruction for a (possibly) hypothetical CPU (if you add up all the bit-field lengths, they sum to 32).

The union allows you to load in a single 32-bit value and then access the individual fields with code like (minor problems fixed as well, specifically the declarations of code and test):

#include <stdio.h>  struct microFields {     unsigned int addr:9;     unsigned int cond:2;     unsigned int wr:1;     unsigned int rd:1;     unsigned int mar:1;     unsigned int alu:3;     unsigned int b:5;     unsigned int a:5;     unsigned int c:5; };  union micro {     unsigned int microCode;     struct microFields code; };  int main (void) {     int myAlu;     union micro test;     test.microCode = 0x0001c000;     myAlu = test.code.alu;     printf("%d\n",myAlu);     return 0; } 

This prints out 7, which is the three bits making up the alu bit-field.

like image 100
paxdiablo Avatar answered Sep 22 '22 19:09

paxdiablo