tags:

views:

185

answers:

6

i have a struct that contains an integer and a char..how do i access the last 2 bytes of my int??

+1  A: 

Supposing you are using C/C++, you have something like:

struct {
  int myInt;
  char myChar;
} myStruct;

and want this:

lastTwoBytesOfInt = myStruct.myInt & 0x0000FFFF;

Having a look at Wikipedia might help you understand bitwise operations if you don't know them yet.

Also, you might have a look at Endianness because the "last" bytes of an integer is a somewhat ambigious term.

schnaader
+10  A: 

The term "last byte" in an integer is not clear. There are two things that you might be thinking of:

  • The most / least significant bytes.
  • The last bytes when the integer is encoded in either little or big endian form. Endianness is often called byte order.

In the first case the least significant bytes can be accessed in most languages by using (x & 0xffff).

In the second case you will need to check what byte order you need. For example if you are using network byte order the last byte is the least significant byte. In other cases it might be the most significant byte that is the last byte.

Mark Byers
Ugh, ignore my previous comment, it's the end of the day and my brain is tired...
Nick Meyer
+3  A: 
int i = // ?? get your int somehow
int lastTwoBytes = i & 0x0000FFFF;
int firstTwoBytes = (i >> 16) & 0x0000FFFF; // maybe you really want the first two?
glowcoder
+4  A: 

Using a union like this should let you get at the individual bytes of an integer:

union {
    int           i;
    unsigned char bytes[sizeof(int)];
} int_bytes;

Exactly how many bytes compose an integer is implementation dependent.

bta
no, you're not supposed to use unions in the way you're implying.
Cirno de Bergerac
@Cirno- In what way? I'm not invoking undefined behavior. This is essentially no different than declaring `int i; char* ptr = (char*)` and using `ptr` like an array to sequentially access bytes. This is a common technique for dumping bytes of a block of data in debugging code.
bta
In C99, "the value of a union member other than the last one stored into" is "unspecified behavior" (J.1). Unspecified, not undefined - so it's not an error, but the implementation can choose to do what it likes. So, technically not portable, but you'd have to implement unions in a strange way for this *not* to give you the bytes of the representation of `i`.
Matthew Slattery
@Matthew, @Cirno: The C standard makes an exception for `unsigned char` case for doing this. So, if `bytes` was `unsigned char`, it would be fine. I am not completely sure if plain `char` is allowed as well.
Alok
@Alok- Actually, that was supposed to have been `unsigned char` (copy/paste error on my part). Thanks for catching that.
bta
+1  A: 

Here's another way to access the upper and lower 16-bits (assuming short is 16-bits on your system):

unsigned int x = 0x12345678;
cout << hex << ((unsigned short*)&x)[0] << endl; // prints 5678
cout << hex << ((unsigned short*)&x)[1] << endl; // prints 1234

Note that the result depends on the endianness of your system. http://en.wikipedia.org/wiki/Endianness

Tom Sirgedas
A: 

On windows, you can use HiWord()/LoWord() to access the first or last 2 bytes.

Mike