Re: Variable declaration followed by colon?
- From: Antoninus Twink <nospam@xxxxxxxxxxxxxx>
- Date: Sat, 12 Apr 2008 19:11:07 +0200 (CEST)
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;
};
.
- Follow-Ups:
- Re: Variable declaration followed by colon?
- From: robertwessel2@xxxxxxxxx
- Re: Variable declaration followed by colon?
- References:
- Variable declaration followed by colon?
- From: saverio . post
- Re: Variable declaration followed by colon?
- From: Richard Heathfield
- Re: Variable declaration followed by colon?
- From: Yunzhong
- Variable declaration followed by colon?
- Prev by Date: Re: case label does not reduce to an integer constant
- Next by Date: local variables in a recursive program
- Previous by thread: Re: Variable declaration followed by colon?
- Next by thread: Re: Variable declaration followed by colon?
- Index(es):
Relevant Pages
|