Last active
August 29, 2015 14:08
-
-
Save mooman219/2d2e12839f3c3b69dc5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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