Suppose I had the following code:
unsigned int a = 0xb07b6fde;
unsigned long b = htonl(a);
b = b >> 4;
printf("int: %d\n", b);
printf("hex: %x\n", b);
printf("\n");
unsigned int c = 0xb07b6fde;
unsigned long d = htonl(c) >> 4;
printf("int: %d\n", d);
printf("hex: %x\n", d);
The output would be:
int: 233240507
hex: de6f7bb
int: -35194949
hex: fde6f7bb
Why is the second result different? As far as I know, >> for signed numbers uses arithmetic shift while that for unsigned uses logical shift. htonl() supposedly returns uint32_t which is unsigned.
Also if <netinet/in.h> is included, the second result would be the same as the first....