Skip to content

Instantly share code, notes, and snippets.

@ironhouzi
Created June 12, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ironhouzi/aede4ec004dbd74fca3f to your computer and use it in GitHub Desktop.
Save ironhouzi/aede4ec004dbd74fca3f to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <limits.h>
int main(void) {
int a = ((INT_MAX) / 2);
int b = ((INT_MAX) / 2) + 2;
long x = (long)(a + b);
printf("x = %d\na = %d\tb = %d\n", x, a, b);
printf("(x^a) >= 0: %d\n", ((x^a) >= 0));
printf("(x^b) >= 0: %d\n", ((x^b) >= 0));
x = (long)((unsigned long)a + b);
printf("(x^a) >= 0: %d\n", ((x^a) >= 0));
printf("(x^b) >= 0: %d\n", ((x^b) >= 0));
a = ((INT_MAX) / 2) + 2;
b = ((INT_MAX) / 2);
x = (long)(a + b);
printf("x = %d\na = %d\tb = %d\n", x, a, b);
printf("(x^a) >= 0: %d\n", ((x^a) >= 0));
printf("(x^b) >= 0: %d\n", ((x^b) >= 0));
x = (long)((unsigned long)a + b);
printf("(x^a) >= 0: %d\n", ((x^a) >= 0));
printf("(x^b) >= 0: %d\n", ((x^b) >= 0));
long c = ((LONG_MAX) / 2);
long d = ((LONG_MAX) / 2) + 2;
x = (long)(c + d);
printf("x = %ld\nc = %ld\td = %ld\n", x, c, d);
printf("(x^c) >= 0: %ld\n", ((x^c) >= 0));
printf("(x^d) >= 0: %ld\n", ((x^d) >= 0));
x = (long)((unsigned long)c + d);
printf("(x^c) >= 0: %ld\n", ((x^c) >= 0));
printf("(x^d) >= 0: %ld\n", ((x^d) >= 0));
c = ((LONG_MAX) / 2) + 2;
d = ((LONG_MAX) / 2);
x = (long)(c + d);
printf("x = %ld\nc = %ld\td = %ld\n", x, c, d);
printf("(x^c) >= 0: %ld\n", ((x^c) >= 0));
printf("(x^d) >= 0: %ld\n", ((x^d) >= 0));
x = (long)((unsigned long)c + d);
printf("(x^c) >= 0: %ld\n", ((x^c) >= 0));
printf("(x^d) >= 0: %ld\n", ((x^d) >= 0));
return 0;
}
/*
output:
x = -2147483648
a = 1073741823 b = 1073741825
(x^a) >= 0: 0
(x^b) >= 0: 0
(x^a) >= 0: 1
(x^b) >= 0: 1
x = -2147483648
a = 1073741825 b = 1073741823
(x^a) >= 0: 0
(x^b) >= 0: 0
(x^a) >= 0: 1
(x^b) >= 0: 1
x = -9223372036854775808
c = 4611686018427387903 d = 4611686018427387905
(x^c) >= 0: 0
(x^d) >= 0: 0
(x^c) >= 0: 0
(x^d) >= 0: 0
x = -9223372036854775808
c = 4611686018427387905 d = 4611686018427387903
(x^c) >= 0: 0
(x^d) >= 0: 0
(x^c) >= 0: 0
(x^d) >= 0: 0
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment