Java Byte&0xff

Source: Internet
Author: User

When doing serial port communication, the data is sent in byte type.

The normal byte range is-128-127, while Java's byte range is 0-255

So when the byte of the data is converted to Java byte, it needs to do a & operation with 0xFF (11111111), converting 128-127 to Java 0-255

That is, with 0xFF do & operations will change the data byte value into Java byte type value, also will -128~0 between the negative values are converted to positive values.

System.out.println ("1 Code:" +integer.tobinarystring (1)); // the negative numbers in Java are complement +1 System.out.println ("-1 Code:" + (integer.tobinarystring ( -1))); System.out.println ("0XFF Code:" +integer.tobinarystring (integer.valueof ("FF", +)); System.out.print ("-1 & 0xFF:"); System.out.println (-1 & 0xFF); System.out.print ("1 & 0xFF:"); System.out.println (1 & 0xFF);

Print output

1 Encoding: 1-1 encoding: 111111111111111111111111111111110XFF Encoding:11111111-1 & 0xff:2551 & 0xff:1

Java Byte&0xff

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.