Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Zoomby

fit ip header into Bit Fields

This topic is 5580 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi I have a simple struct which I want to use for IP headers. But when I copy a buffer (with the ip header) to the struct''s memory, the both values a and b are flipped. a is not "4" and b is not "5" like it should be. What''s the problem? struct test { unsigned char a: 4; unsigned char b: 4; }; int main() { test t; unsigned char value=69; memcpy(&t,&value,1); cout<<(int)t.a<

Share this post


Link to post
Share on other sites
Advertisement
t.a should be equal to 69 and t.b will be undefined. The reason is that you are only copying 1 byte into your 2 byte structure (assuming padding is off here.)

Are you trying to put 6 & 9 into your struct?
If that is what you are trying to do then you need an unsigned short variable.


int main()
{
test t;
unsigned short value = 1545; // value 0000110 00001001
// value 6 9

// this assumes memory padding/alignment is "off"!
memcpy(&t, &value, sizeof(value));

cout << (int)t.a << cout << (int)t.b << endl;
}


Hope this helps.




[edited by - Digitalfiend on March 6, 2003 12:23:16 PM]

Share this post


Link to post
Share on other sites
Cool problem. I am not a bit field expert, but here is my theory.
Member a of struct test is declared to use 4 bits of an unsigned char. It uses the lowest 4 bits. Member b also uses 4 bits of the same unsigned char and gets the upper 4 bits.
69 = 0x45 = 01000101b
When this is memcpy''d into t, b gets 0100b and a gets 0101b.

Shawn

Share this post


Link to post
Share on other sites
@Digitalfiend
take a closer look at the structure. it''s not a 2 byte structure, it''s a one byte strucure with two 4 bit vars.

@ShawnO
your right. but that means there''s a kind of endianism within the byte. I thought big and little endian only appears if you got a number with more than one byte?

who does exactly know whats going on?

ciao
chris

Share this post


Link to post
Share on other sites
K&R says this about bit-fields:
quote:

Fields are assigned left to right on some machines and right to left on others.


In other words, the ordering of the data is implementation defined.

MSDN C Bit Field documentation says this:
quote:

Bit fields are allocated within an integer from least-significant to most-significant bit.


MSDN C++ Bit Field documentation says this:
quote:

The ordering of data declared as bit fields is from low to high bit.


FYI - The C99 standard does not require an implemenation to provide bit fields of type unsigned char.

Shawn

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I have no idea what this stuff means

Share this post


Link to post
Share on other sites
A good question is why are you doing this? What is the point of using bit fields for network I/O? It isn''t really saving you that much data, is it?

Share this post


Link to post
Share on other sites
First of all, it _is_ saving space, second he''s apparently trying to store an IP packet header in this struct, and he can''t just change the layout of IP packets. Now, why he''s trying to mess with IP headers at all is a mystery to me.

cu,
Prefect

Share this post


Link to post
Share on other sites
@Digitalfiend:

It doesn''t have to do anything with saving space. Imagine you received a buffer with an IP header (and maybe more). The IP header (and many more protocols) have a lot of flags and less than 8 bit-datatypes in it (take a look at RFC:791). So when I have a struct (with bit fields) that has the same layout as the ip header all I have to is a memcpy() from the receive buffer to the IP header-struct (and don''t iterate throught the buffer by hand and extract the parameter or so). Now I can access every parameter in the header easiely.

@Prefect:
I don''t want to change the layout of the IP packets. The struct I use in the first post is just an example of the main problem (the problem was how exactly bit fields are saved in memory)

You need to take a look at IP headers when you are:
-just interested in it (would you believe that? :-) )
-receiving ICMP echo replies.
-programing a packet sniffer.

bye
chris

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!