The processing of unsigned integers in C in Java
* Because integers in Java are signed, this means that integers in Java have a valid number less than unsigned integers in C.
* For example: 32768 with the C unsigned integer int16 is normal, but with Java short (two bytes) represented by a negative number
* So you should look for a type with a larger number of digits in Java to represent
* For example: Unsigned int16 should be represented by an int after taking 2 bytes
* Unsigned int32 should be represented by a long after taking 4 bytes
*
Signed integers in C can be handled normally in Java
Public classShortoverflow { Public Static voidMain (string[] args) {System.out.println ("There are no unsigned integers in Java, so"); System.out.println ("Short Max in Java:" +short.max_value); System.out.println ("Its 16 binary number:" +integer.tohexstring (Short.max_value)); System.out.println ("Its 2 binary number:" +integer.tobinarystring (Short.max_value)); System.out.println ("16-bit maximum value:" +0xffff); System.out.println ("==============================="); Show (32769); } Private Static voidShowintnum) {System.out.println ("============= Conversion Demo ============="); System.out.println ("Original:" +num); System.out.println ("Binary:" +integer.tobinarystring (num)); System.out.println ("Strong turn short:" + ( Short) (num)); if(num>65535) {System.out.println ("The number exceeds 16 bits, and the strong turn will only retain the latter 16 bits"); } System.out.println ("Binary:" +integer.tobinarystring ( Short(num))); System.out.println ("Strong to short after the original int number is equal:" + (( Short) (num) = =num)); System.out.println ("Restore:" + (( Short) (num) & 0xFFFF)); System.out.println ("Strong turn char:" + (Char) (num)); }}
The processing of unsigned integers in C in Java