tags:

views:

99

answers:

3

I have a stream of bytes that is being read in from a socket (little endian). Can someone tell me why only the last one of the methods below gives the correct answer? I suspect it's to do with the carry bit but not sure. I've always found that when printing binary data in hex form.

e.g.

printf("%02X", data);

it sometimes prints out funny values with 0xff in front. The way to fix it seems to be doing this. This still occasionally happens when data is a char data type too:

printf("%02X", data & 0xff);

Heres a simplified example from what I was seeing with my byte stream. Where bytes would be the stream of bytes I've read from the socket.

int main(int argc, char* argv[])
{
    union {
        unsigned int num;
        char bytes[4];
    } x;

    x.num = 500;
    printf("x.num=%u\n", x.num);

    unsigned int method1 = x.bytes[0] | (x.bytes[1] << 8) | (x.bytes[2] << 16) | (x.bytes[3] << 24);
    printf("method1 = %u\n", method1);

    unsigned int method2 = x.bytes[0] + (x.bytes[1] << 8) + (x.bytes[2] << 16) + (x.bytes[3] << 24);
    printf("method2 = %u\n", method2);

    unsigned int method3 = (x.bytes[0] & 0xff | (x.bytes[1] & 0xff) << 8 
                            | (x.bytes[2] & 0xff) << 16 | (x.bytes[3] & 0xff) << 24);
    printf("method3 = %u\n", method3);

    return 0;
}

Which outputs:

x.num=500
method1 = 4294967284
method2 = 244
method3 = 500

Only the last extract is actually correct. Is my method of building up the numbers the most optimal? I also tried a memcpy to a variable but again that wasn't reliable.

+1  A: 

Why not use unsigned char bytes[4] in the union? Without a signedness specification, you don't know whether your chars are signed or unsigned (depends on the platform and compiler) so it's not surprising that arithmetic on them gives peculiar results.

The 0xFF you may have observed could likely be due to sign extension if your compiler does indeed decide to treat your "mystery char data" as signed rather than unsigned.

As a rule of thumb, when char is used to mean "a byte for further processing or display purposes" I would recommend always using unsigned char for definiteness -- I don't remember when the last time was that I really wanted a signed char!-)

Alex Martelli
+4  A: 

When signed datatypes are being cast into higher datatypes, the most significant bit is used as a sign bit. You should have unsigned char in your union. In your case 500 = 256 + 244 = 0x1f4 and the byte 244 has the most significant bit set, so when promoted becomes 0xfffffff4.

deinst
Thanks for the thorough explanation.
Matt H
A: 

In a little-endian architecture, the number 500 (256 + 244) will be stored as:

+-----------+-----------+-----------+-----------+
| 244(0xf4) |   1(0x01) |   0(0x00) |   0(0x00) |
+-----------+-----------+-----------+-----------+

and, because you're using char on its own, the C standard doesn't specify whether it's signed or unsigned (it's implementation defined). In your case, it appears to be signed.

Aside: sign extension happens in two's complement encoding when you load a "thin" data value into a wider one. Where the top bit of the thin number is 1 (indicating a negative number), this is extended to all higher bits in the wider type. The reason this is done is to preserve the nature of the number. For example, -12 in 8-bit is 0xf4, in 16-bit is 0xfff4 and in 256-bit is 0xfffffffffffffffffffffffffffffff4.

That means the 244 (-12 or 0xf4) will be sign-extended to 0xfffffff4. That's likely to screw up your | and + solutions quite badly.

The values you'll get are:

x0       0xfffffff4  
x1<<8    0x00000100
x2<<16   0x00000000
x3<<24   0x00000000

Method 1 uses | so you end up with 0xfffffff4 (the other one bit in x1 are already set in x0 so it remains unaffected, and x2/x3 are all zero) which is 4294967284 as an unsigned int.

Method 2 adds them so that you end up with 0x1000000f4 which of course wraps, discarding the high byte and leaving you with 0xf4 or 244.

In method 3, the sign extension still happens but before you & it with 0xff. It's that & operation that reverses the effect of the sign extension and turns your 0xfffffff4 back into 0xf4.

As others have already mentioned, use unsigned char explicitly. This will prevent sign extension happening when promoting to a larger integral type.

paxdiablo