Java Byte&0xff

Source: Internet
Author: User

Label:

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

Related Article

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.

Tags Index: