The processing of unsigned integers in C in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.