Skip to content

Instantly share code, notes, and snippets.

@mooman219
Last active August 29, 2015 14:08
Show Gist options
  • Save mooman219/2d2e12839f3c3b69dc5e to your computer and use it in GitHub Desktop.
Save mooman219/2d2e12839f3c3b69dc5e to your computer and use it in GitHub Desktop.
/**
* @author Joseph Cumbo (mooman219)
*/
public class Unsigned {
public static void main(String[] args) {
int test = Integer.MAX_VALUE;
System.out.printf("0x%1$8X or %1$d\n", test);
test++;
System.out.printf("0x%1$8X or %1$d\n", test);
System.out.printf("0x%1$8X or %1$d\n", asUnsignedInt(test));
}
public static long asUnsignedInt(int value) {
return (value & Integer.MIN_VALUE) == Integer.MIN_VALUE // If the top bit is set
? (value & Integer.MAX_VALUE) + (0x80000000l) // Then, unset the top bit, then add Integer.MIN_VALUE as a long
: value; // Else, just retun the value
}
}
/*
run:
0x7FFFFFFF or 2147483647
0x80000000 or -2147483648
0x80000000 or 2147483648
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment