I’ve been trying to understand an error I get with the following code
bytes2bits(p,q,pixels)
u_char *p, *q;
register u_int pixels;
{
register u_char *r, a;
register u_long *l;
...
switch (*l++) {
case 0x00000000: a = 0x00; break;
case 0x00000001: a = 0x10; break;
case 0x00000100: a = 0x20; break;
case 0x00000101: a = 0x30; break;
case 0x00010000: a = 0x40; break;
case 0x00010001: a = 0x50; break;
case 0x00010100: a = 0x60; break;
case 0x00010101: a = 0x70; break;
case 0x01000000: a = 0x80; break;
case 0x01000001: a = 0x90; break;
case 0x01000100: a = 0xa0; break;
case 0x01000101: a = 0xb0; break;
case 0x01010000: a = 0xc0; break;
case 0x01010001: a = 0xd0; break;
case 0x01010100: a = 0xe0; break;
case 0x01010101: a = 0xf0; break;
default:
(void) fprintf(stderr,"bytes2bits: bad value %x\n",*--l);
exit(1);
}
......
}
My problem lies in the fact that this block of code exits with the error message
bytes2bits: bad value 1010100
I would’ve thought that 0x01010100 == 1010100 (Note: the fprintf is using %x as output format, so I am looking at a hexadecimal number. Also, when I test and use a printf with %d output format, I see the value 16843008 (= 16^6 + 16^4 + 16^2), which is the equivalent decimal representation of 0x01010100. The ‘bad value’ 1010100 output is not affected by the presence of my printf checking statement).
bytes2bits: bad value 1010100
How can I make sense of the fact that the switch statement does not recognize 1010100 as the next to last case (i.e.case 0x01010100: a = 0xe0)?
The only reasonable guess here is that on your platform the
u_longthat you analyze withswitchis longer thanint/unsigned intthat you are actually printing.For example, if
u_longis a 64 bit type andintis a 32-bit type, then your*l++might evaluate to0xBAADF00D01010100. This obviously will not match thecase 0x01010100:label. But when you are doing yourprintfs with%xspecifier, you are only printing the trailing0x01010100portion.Use
lprefix in format specifiers (%lx,%ldetc.) to printlongvalues to avoid such confusions in the future (assuming thatu_longis based onlong).