Re: Variable declaration followed by colon?



On 12 Apr 2008 at 15:17, Yunzhong wrote:
What does this represent?
unsigned int n : 6;

Inside a structure or union, it represents an unsigned integral
bitfield that occupies 6 bits;

Perhaps the OP will wonder why such a thing could be useful.

A couple of common examples where bitfields are used are device drivers,
because combinations of flags (i.e. bits) are often stored in a single
byte in hardware; and binary data formats, which frequently contain
fields that aren't aligned at byte boundaries. Whether you realize it or
not, you're relying on bitfields every time you send an IP packet!
Here's the IP header struct that's used in every packet:

struct iphdr {
unsigned int version:4;
unsigned int ihl:4;
u_int8_t tos;
u_int16_t tot_len;
u_int16_t id;
u_int16_t frag_off;
u_int8_t ttl;
u_int8_t protocol;
u_int16_t check;
u_int32_t saddr;
u_int32_t daddr;
};

.



Relevant Pages

  • Re: How to use bitfields?
    ... difference with other integer types beside their bit size. ... It's just for readability that bitfields are used. ... struct ARGB1555 { ... unsigned int green: 5; ...
    (comp.lang.c)
  • [EXPL] Ethereal AFP Protocol Dissector Remote Format String (Exploit)
    ... from the packet and proceeds to pass it to ... unsigned char version:4,ihl:4; ... unsigned int saddr; ... struct tcph{ ...
    (Securiteam)
  • Re: incorrect handling of bit fields
    ... >packet received from an external hardware device. ... >sends a binary packet using a defined field layout. ... >struct Packet ... >then added some "unsigned int" fields, ...
    (microsoft.public.vc.language)
  • Re: How to use bitfields?
    ... It's just for readability that bitfields are used. ... struct ARGB1555 { ... unsigned int green: 5; ... for the same target, ...
    (comp.lang.c)
  • Re: Variable declaration followed by colon?
    ... A couple of common examples where bitfields are used are device drivers, ... Here's the IP header struct that's used in every packet: ...   unsigned int version:4; ...
    (comp.lang.c)