/* * FunnyMath2 - signed vs. unsigned */ #include /* -Ian! D. Allen - idallen@idallen.ca - www.idallen.com * License: GPL Version 3 or later */ int main( void ){ signed int sx = 2000000000; signed int sy = 2000000001; signed int sz = sx + sy; unsigned int ux = 2000000000; unsigned int uy = 2000000001; unsigned int uz = ux + uy; printf("sz is 0x%X which could be %d or %u\n", sz, sz, sz); printf("uz is 0x%X which could be %d or %u\n", uz, uz, uz); /* * OUTPUT: * sz is 0xEE6B2801 which could be -294967295 or 4000000001 * uz is 0xEE6B2801 which could be -294967295 or 4000000001 */ /* * Both sz and uz contain the same bit pattern 0xEE6B2801. * Signed sz interprets the bits as -294967295(10). * Unsigned uz interprets the bits as 4000000001(10). */ if ( sz < 0 ) printf("sz is less than zero\n"); else printf("sz is not less than zero\n"); if ( uz < 0 ) printf("uz is less than zero\n"); else printf("uz is not less than zero\n"); /* * OUTPUT: * sz is less than zero * uz is not less than zero */ return 0; }