Question

While looking through the source package for QEMU, I found in the exec.c file:

struct PhysPageEntry {
    /* How many bits skip to next level (in units of L2_SIZE). 0 for a leaf. */
    uint32_t skip : 6;
     /* index into phys_sections (!skip) or phys_map_nodes (skip) */
    uint32_t ptr : 26;
};

I was wondering what the : operator means. I could not find it in a list of syntax definitions for C.

Was it helpful?

Solution

This is a structure declared with bit fields and the structure members are called bit fields: A bit field is set up with a structure declaration that labels each field and determines its width. The above definition causes PhysPageEntry to contain one 6-bit field and one 26 bit field members namely skip and ptr respectively. Its signature is

struct
{
      type [member_name] : width ;
};  

Here width is the number of bits in the bit-field. The width must be less than or equal to the bit width of the specified type.

OTHER TIPS

struct PhysPageEntry declares two bit fields skip and ptr. This is basically to allow the struct to pack these odd lengths (in terms of bits) efficiently. If the author didn't do this, the struct would likely be 8 bytes long (on a 32-bit architecture).

It represents number of bits. It's mostly used for unions:

struct byte {
  unsigned a : 1;
  unsigned b : 1;
  unsigned c : 1;
  unsigned d : 1;
  unsigned e : 1;
  unsigned f : 1;
  unsigned g : 1;
  unsigned h : 1;
};

You can read this too for better understanding.

Its called a bitfield. Within a structure or union declaration, this declares 'skip' to be a "bit field" of 6 bits width. They are to be used inside of structures. If it helped please vote as right!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top