Java and unsigned values

I am parsing unsigned bits from a DatagramSocket. I have a total of 24 bits (or 3 bytes) that are in number: 1 unsigned 8bit integer, followed by an integer with a 16-bit sign. But java never stores anything but a signed byte in an array of bytes / bytes? When java takes these values, do you lose this last 8th bit?

DatagramSocket serverSocket = new DatagramSocket(666); byte[] receiveData = new byte[3]; <--Now at this moment I lost my 8th bit System.out.println("Binary Server Listing on Port: "+port); while (true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); byte[] bArray = receivePacket.getData(); byte b = bArray[0]; } 

enter image description here

I lost this 8th bit since I converted it to byte? Am I incorrectly initializing a byte array of 3 bytes?

+8
java bit-manipulation
source share
3 answers

When java takes these values, do you lose this last 8th bit?

Not. In the end you will get a negative value.

So, to get a value from 0 to 255, the easiest way is to use something like this:

 int b = bArray[0] & 0xff; 

First, byte advances to int , which will sign its extension, which will lead to 25 leading 1 bits if the most significant bit is 1 in the original value. Then & 0xff gets rid of the first 24 bits again :)

+11
source share

No, you will not lose the 8th bit. But unfortunately, Java has two β€œfeatures” that make it more difficult than wise to deal with such values:

  • all its primitive types are signed;
  • when the primitive type is "decompressed" into another primitive type with a large size (for example, reading byte to int , as is the case here), the sign bit of the "lower type" expands.

This means that, for example, if you read byte 0x80 , which is converted in binary format as:

 1000 0000 

when you read it as an integer, you get:

 1111 1111 1111 1111 1111 1111 1000 0000 ^ This freaking bit gets expanded! 

while you really wanted:

 0000 0000 0000 0000 0000 0000 1000 0000 

those. the integer value is 128. Therefore, you MUST mask it:

 int b = array[0] & 0xff; 1111 1111 1111 1111 1111 1111 1000 0000 <-- byte read as an int, your original value of b 0000 0000 0000 0000 0000 0000 1111 1111 <-- mask (0xff) --------------------------------------- <-- anded, give 0000 0000 0000 0000 0000 0000 1000 0000 <-- expected result 

Sad but true.

More generally: if you want to manipulate a lot of byte-oriented data, I suggest you take a look at ByteBuffer , it can help a lot. But, unfortunately, this will not save you from manipulating the bitmask, but simply simplifies reading a certain number of bytes as time (in the form of primitive types).

+7
source share

In Java, bytes (as well as short, int, and long) are only signed numeric data types. However, this does not mean data loss when processing them as unsigned binary data. As your illustration shows, 10000000 is -128 as a decimal sign. If you are dealing with binary data, just treat it as your binary form and everything will be fine.

+2
source share

All Articles