Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.
This post has been edited 1 times, last edit by "grellsworth" (May 16th 2008, 4:35pm)
Quoted
Originally posted by morecowbell
you need to be pretty aware of your types in terms of bit sizes. are u using 16, 32, or 64 bits?
1, in terms of hex as a signed char, is 0xFF, which, if it was unsigned happens to be 255. the MSB for signed int's, commonly known as 2's complement notation, uses 0 for positive and 1 for negative values. that leaves you with seven bits to express magnitude. so, an 8 bit value, you max out at -128 (0x80) and 127 (0x7F) ... so, once you work through 1s and 2s complement, on a 32bit int -1 convert to 0xFFFFFFFF. based on your output, you ought to be on 32 bits. this is _the_output. you can't just truncate that. 0xFFFFFFFF _represents -1 for a signed int; of course, 0xFF would correspond to the _signed_ char. since usually the compiler default is unsigned, you expect to get what you got.
|
|
Source code |
1 2 3 4 5 6 7 8 9 |
void hexDump()
{
signed char four_chars[4] = { 1, -1, -2, -3 };
for(unsigned int x = 0; x < 4; ++x)
{
printf("%02x %02d\n", four_chars[x], four_chars[x]);
}
}
|