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