Skip to content

Instantly share code, notes, and snippets.

@morganwilde
Last active August 29, 2015 13:57
Show Gist options
  • Save morganwilde/9438602 to your computer and use it in GitHub Desktop.
Save morganwilde/9438602 to your computer and use it in GitHub Desktop.
#include <limits.h>
#include <stdio.h>
int main(void) {
int value = -1;
printf("UINT_MAX: %u\n", UINT_MAX);
printf("value: %d\n", value);
unsigned int uvalue = UINT_MAX - (unsigned int)value + 1U;
printf("(unsigned int)value: %u\n", (unsigned int)value);
printf("-(unsigned int)value: %u\n", -(unsigned int)value); // Does a 2's complement conversion
printf("uvalue: %u\n", uvalue);
return 0;
}
// Run with value = -1
//
// UINT_MAX: 4294967295
// value: -1
// (unsigned int)value: 4294967295
// -(unsigned int)value: 1
// uvalue: 1
// Run with value = INT_MIN
//
// UINT_MAX: 4294967295
// value: -2147483648
// (unsigned int)value: 2147483648
// -(unsigned int)value: 2147483648
// uvalue: 2147483648
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment