Byte order question


 
Thread Tools Search this Thread
Top Forums Programming Byte order question
# 1  
Old 08-27-2009
Byte order question

Hi,

The structure that will follow is supposed to hold the following RTP header field

Code:
  0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |V=2|P|X|  CC   |M|     PT      |       sequence number         |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                           timestamp                           |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Code:
typedef struct rtp_header
{
#ifdef BIGENDIAN
    uint16_t version:2;
    uint16_t padbit:1;
    uint16_t extbit:1;
    uint16_t cc:4;
    uint16_t markbit:1;
    uint16_t paytype:7;
#else
    uint16_t cc:4;
    uint16_t extbit:1;
    uint16_t padbit:1;
    uint16_t version:2;
    uint16_t paytype:7;
    uint16_t markbit:1;
#endif
    uint16_t seq_number;

Shouldn't it be like the following

Code:
typedef struct rtp_header
{
#ifdef BIGENDIAN
    uint16_t version:2;
    uint16_t padbit:1;
    uint16_t extbit:1;
    uint16_t cc:4;
    uint16_t markbit:1;
    uint16_t paytype:7;
#else
    uint16_t markbit:1;
    uint16_t paytype:7;
    uint16_t version:2;
     uint16_t padbit:1;
     uint16_t extbit:1;
     uint16_t cc:4;
#endif
    uint16_t seq_number;

Or am I missing something?

Thanks in advance,
S.
# 2  
Old 08-27-2009
[edit] this just gets stranger the more I look at it. I'm going to have to change my mind and say I don't know.

---------- Post updated at 10:12 AM ---------- Previous update was at 09:38 AM ----------

Just done some more research here, comparing bitfields on big and little endian machines I have access to, and learned something new. Bitfields reverse direction on big endian platforms. Considering the following code:
Code:
#include <stdio.h>
#include <string.h>

typedef union
{
        struct
        {
                unsigned int a:3;
                unsigned int b:3;
                unsigned int c:3;
                unsigned int d:2;
                unsigned int e:7;
                unsigned int f:8;
                unsigned int g:6;
        };
        /* Occupies the same memory as a through g, since this is a union */
        unsigned int z;
} bitmongler;

void print32(unsigned int q)
{
        int n;
        for(n=0; n<32; n++)
        {
                if(q & (1<<n))  printf("1");
                else            printf("0");
        }
        printf("\n");
}

#define INVERT(X)       (X)--

int main(void)
{
        bitmongler w;

        int n;
        for(n=0; n<7; n++)
        {
                memset(&w, 0, sizeof(w));

                switch(n)
                {
                case 0: INVERT(w.a);    break;
                case 1: INVERT(w.b);    break;
                case 2: INVERT(w.c);    break;
                case 3: INVERT(w.d);    break;
                case 4: INVERT(w.e);    break;
                case 5: INVERT(w.f);    break;
                case 6: INVERT(w.g);    break;
                }

                print32(w.z);
        }
        return(0);
}

On a little-endian platform it prints:
Code:
11100000000000000000000000000000
00011100000000000000000000000000
00000011100000000000000000000000
00000000011000000000000000000000
00000000000111111100000000000000
00000000000000000011111111000000
00000000000000000000000000111111

On a big-endian platform it prints:
Code:
00000000000000000000000000000111
00000000000000000000000000111000
00000000000000000000000111000000
00000000000000000000011000000000
00000000000000111111100000000000
00000011111111000000000000000000
11111100000000000000000000000000

So yes, it does need to reverse the direction of bits, not just bytes, because the compiler chooses blocks of bits in the reverse direction.

And yet, if we make INVERT subtract two instead of one, we get:

Code:
BE 00000000000000000000000000000011
vs
LE 01100000000000000000000000000000

so the blocks of bits are treated in the correct, normal direction, even if the direction they are grouped in is reversed. Weird! But I suspect there is a rational reason -- that being, when a grouping of bits crosses a byte or word boundary, to make sure the two halves split by the boundary are combined in the right order.

So the code as given was correct, strange as it looks.

Last edited by Corona688; 08-27-2009 at 01:18 PM..
# 3  
Old 08-27-2009
I'm not so sure bit fields are portable.

You'd probably be better off using a uint32_t, bit-masking out the fields, and making sure you always use hton() to write the data and ntoh() to read it.
# 4  
Old 08-27-2009
They may not work on some embedded platforms, but beyond that, they're fairly portable in the "compiles" sense, though where the bits actually end up depends a lot on your architecture. I'd tend to agree they're more trouble than just using binary logic operations.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Oracle 11g optimizer best join order question

Hi team Optimizer max permutations and search limit are set to 3.5 million and 10 respectively.The parameter instruct optimizer to evaluate up to 3.5 million permutations for max 10 table joins I have query with 9 tables joined. How many combinations will optimizer perform to find best... (1 Reply)
Discussion started by: Perlbaby
1 Replies

2. UNIX for Advanced & Expert Users

Problem With UTF8 Byte Order Make

Hi Im migrating a few websites from my old webserver (CentOS-5) to a new server (CentOS6) , one of these websites is multilingual and has a lot of utf8 files(html,php) with different languages (i.e arabic, persian, russian ,etc). In old server when i do: file mailer.php I get : ... (6 Replies)
Discussion started by: mohs3n
6 Replies

3. IP Networking

Total byte

Hi I've a pkts trace and I'm performing some test on it. I'd like to figure out also the numbers of total byte in that trace. Any idea? thanks in advance D. (0 Replies)
Discussion started by: Dedalus
0 Replies

4. Shell Programming and Scripting

Remove a byte(Last byte from the last line)

Hi All Can anyone please suggest me how to remove the last byte from a falt file .This is from the last line's last BYTE. Please suggest me something. Thank's and regards Vinay (1 Reply)
Discussion started by: vinayrao
1 Replies

5. Shell Programming and Scripting

Check if 2 files are identical byte-to-byte?

In my server migration requirement, I need to compare if one file on old server is exactly the same as the corresponding file on the new server. For diff and comm, the inputs need to be sorted. But I do not want to disturb the content of the file and need to find byte-to-byte match. Please... (4 Replies)
Discussion started by: krishmaths
4 Replies

6. Programming

Printing out Byte in C

Hi all, Can anyone advise on how to display the data in a byte variable, i.e can i use printf("%s", vairable_name);? Cheers (2 Replies)
Discussion started by: dwgi32
2 Replies

7. Shell Programming and Scripting

0 byte file with no name????

Please help me in removing the 2nd file : -rw-rw-rw- 1 fup03a fup03a 9216 Aug 16 00:45 med_delay_log -rw-rw-rw- 1 fup03a fup03a 0 Aug 16 18:04 (5 Replies)
Discussion started by: miltonkeynesguy
5 Replies

8. Programming

Question on order of headers and WEXITSTATUS

In one of the Unix Programming FAQ's they have the following headers in the program to catch SIGCHLD #include <sys/types.h> /* include this before any other sys headers */ #include <sys/wait.h> /* header for waitpid() and various macros */ #include <signal.h> /* header for signal... (5 Replies)
Discussion started by: frequency8
5 Replies

9. Programming

Byte Padding

Hi, Can someone explain what is byte padding? For ex: struct emp{ char s; int b; char s1; int b1; long b3; char s3; } What will be the size of this structure? Thanks (6 Replies)
Discussion started by: naan
6 Replies

10. Shell Programming and Scripting

zero byte with String

My program would be creating a file, incase of non data from database it would only able to produce 'END, logically it would be file created by oracle in unix. this file is showing zero byte but actully there are 4 char in file.This is not required, therefore what is command line should be... (0 Replies)
Discussion started by: u263066
0 Replies
Login or Register to Ask a Question