C Syntax I've never seen before

Started by
7 comments, last by Shannon Barber 22 years, 4 months ago
Ok, why I have I never seen this before? And why didn''t I learn it in school? And why isn''t it in any of my C nor C++ books!? xps considering how friggin'' useful it is!
  
   typedef struct {
       unsigned int version:2;   /* protocol version */
       unsigned int p:1;         /* padding flag */
       unsigned int x:1;         /* header extension flag */
       unsigned int cc:4;        /* CSRC count */
       unsigned int m:1;         /* marker bit */
       unsigned int pt:7;        /* payload type */
       u16 seq;              /* sequence number */
       u32 ts;               /* timestamp */
       u32 ssrc;             /* synchronization source */
       u32 csrc[1];          /* optional CSRC list */
   } rtp_hdr_t;
  
Magmai Kai Holmlor "Oh, like you''ve never written buggy code" - Lee "What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
Those : (colons) things set the bit width in the structure instead of using the default generated compiler bit widths. I think that''s what you didn''t understand.
quote:Original post by Magmai Kai Holmlor
Ok, why I have I never seen this before?
And why didn''t I learn it in school?
And why isn''t it in any of my C nor C++ books!?
xps considering how friggin'' useful it is!

       typedef struct {       unsigned int version:2;   /* protocol version */       unsigned int p:1;         /* padding flag */       unsigned int x:1;         /* header extension flag */       unsigned int cc:4;        /* CSRC count */       unsigned int m:1;         /* marker bit */       unsigned int pt:7;        /* payload type */       u16 seq;              /* sequence number */       u32 ts;               /* timestamp */       u32 ssrc;             /* synchronization source */       u32 csrc[1];          /* optional CSRC list */   } rtp_hdr_t;    


Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

You''re talking about the Width??? ( ex. int value:2 ) I''ve seen it before but I''ve never used it personally though... But it''s even mentionned in my old Borland Turbo C++ v2.0 books. So I guess it''s probably old knowledge...



"And that''s the bottom line cause I said so!"

** I WANT TO BE THE MODERATOR FOR THE LINUX FORUM **

Cyberdrek

Resist Windows XP''s Invasive Production Activation Technology!

"gitty up" -- Kramer
/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash
[Cyberdrek | ]
You must have bad C books. Try the K&R book.

Also beware of memory alignment issues. Doing your own bit-specification for memory sizes can be helpful in network protocols and when storing things to disk, but if you''re not careful it can hurt your CPU efficiency at runtime as most good compilers will try to pad structures out for best memory alignment and a programmer could possibly screw this up if they aren''t careful.
I immediately recognized what they did, I had just never seen code that used them before.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
That''s why I always recommend Stroustrup''s book. I knew absolutely no C++, just java, and within a week (I read pretty quickly) I knew more than the average person on this forum, and far more than the people in my new school who have taken 2 semesters of C and 1 semester of C++. Just buy that book and read it straight through, that''s all you need to do. (assuming you already have some programming experience)
Yep: reading and understanding the Stroustrup book will put you in the top 25% of C++ programmers on this board. Pretty much everything the language can do is explained and justified, with some nifty examples along the way.

Regarding bitfields, it''s important to remember that what you save on structure space, you lose in execution time (added shifts and masking), so it''s not a win/win situation.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
quote:Original post by gmcbay
You must have bad C books. Try the K&R book.

Also beware of memory alignment issues. Doing your own bit-specification for memory sizes can be helpful in network protocols and when storing things to disk, but if you''re not careful it can hurt your CPU efficiency at runtime as most good compilers will try to pad structures out for best memory alignment and a programmer could possibly screw this up if they aren''t careful.


Also be aware of endianees issues with bit-fields if your code is going to be multi-platform. Especially so when you''re using bitfields in conjunction with unions.
I first leart about bitfields right here at gamedev from droid on the networking forum. It''s also in my C++ in 21 days book by jesse Liberty.

Chris Brodie
http:\\fourth.flipcode.com
Chris Brodie

This topic is closed to new replies.

Advertisement