   char type problem
  char c[4];c[0] = 0xff;c[1] = 0xaa;c[2] = 0xff;c[3] = 0x0;unsigned int n = (c[0] << 24) + (c[1] << 16) + (c[2] << 8) + c[3];cout << hex << n;ok! so this is the code. Now before pasting it in your ide think for a sec what result will be shown on the screen. Then check to see if you were right and reply with what you think the problem might have been.   
  I'd say the result depends on wether char is signed or unsigned.   ;  The type "char" may be either signed or unsigned. While I didn't bother to think what would be displayed, I knew it might be incorrect and that the declaration of "c" should have been "unsigned char".   ;  Correct! char is signed unless specified otherwise. Thus in assembly it would sign extend(movsx) instead of zero extend(movzx) so the addition of 0xff << 24 with 0xff << 16 would be 0xfeff0000 instead of 0xffff0000. This could be useful when receiving bytes, storing them in a char array and then trying to create an int from 4 bytes. Make sure char is defined as unsigned char or you will be in trouble if one byte makes it over 127, as it will be treated as a negative number and you will end up with a wrong int.   ;Quote:Original post by Hotshot5000Correct! char is signed unless specified otherwise.I don't think that's true.   ;Quote:Original post by Hotshot5000Correct! char is signed unless specified otherwise.Nope. The type "char" is a distinct type, separate from both "signed char" and "unsigned char".   ;Quote:Hotshot5000Correct! char is signed unless specified otherwise. This is not true. Consult section 3.9.1 of the C++ standard. DevFred and rip-off are correct.   ;  Or 6.2.5 of the C standard.   ;  The funny thing is that the result also depends on whether sizeof(char) = sizeof(int) or not.Sign extension and value extension make any expression a mess in C or C++.   ;  In any particular implementation, a plain charobject can take on either the same values as a signed char or an unsigned char; which one isimplementation defined.So I guess that the default must be signed char, at least with the default settings in visual c++ 2008.   