60

In Java, there is no such thing as an unsigned byte.

Working with some low level code, occasionally you need to work with bytes that have unsigned values greater than 128, which causes Java to interpret them as a negative number due to the MSB being used for sign.

What's a good way to work around this? (Saying don't use Java is not an option)

Max
  • 1,528
  • 1
  • 11
  • 17

7 Answers7

82

It is actually possible to get rid of the if statement and the addition if you do it like this.

byte[] foobar = ..;
int value = (foobar[10] & 0xff);

This way Java doesn't interpret the byte as a negative number and flip the sign bit on the integer also.

gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
ejack
  • 1,132
  • 1
  • 9
  • 8
  • 6
    A small note on efficiency: When compiling an expression like the above into native machine instructions, Java's Hotspot Client VM will do a normal byte to int promotion (with [sign extension](https://en.wikipedia.org/wiki/Sign_extension)), followed by an AND operation. The Hotspot Server VM is smart enough to do a promotion with zero extension, skipping the AND. – Boann Feb 08 '13 at 14:56
20

When reading any single value from the array copy it into something like a short or an int and manually convert the negative number into the positive value it should be.

byte[] foobar = ..;
int value = foobar[10];
if (value < 0) value += 256 // Patch up the 'falsely' negative value

You can do a similar conversion when writing into the array.

pauldoo
  • 18,087
  • 20
  • 94
  • 116
  • 49
    Much easier and more efficient to do int value=(foobar[10]&0xFF); – Lawrence Dol May 02 '09 at 07:33
  • 9
    Maybe, but I wanted my code to be as clear and instructive as possible. Also, I don't know what '&' does for negative integers in Java (at least not without googling), so I played it safe. – pauldoo May 04 '09 at 10:47
  • 1
    why would And do anything different for negative numbers. And is a boolean operation it works on individual bits, it does know, check or care if a number is positive, negative, or any other value. – mP. Jun 16 '11 at 02:53
  • 4
    I thought that perhaps 0xFF would be treated as a signed byte (which would be -1), then sign extended to a 32-bit signed integer (which would give 0xFFFFFFFF aka -1) before being passed as an argument to '&'. This would give the incorrect result. It's clear from the discussion that Java will not do this, but I thought best to play it safe. – pauldoo Jul 05 '11 at 16:48
1

Using ints is generally better than using shorts because java uses 32-bit values internally anyway (Even for bytes, unless in an array) so using ints will avoid unnecessary conversion to/from short values in the bytecode.

izb
  • 50,101
  • 39
  • 117
  • 168
  • 1
    Well, if the value will always be a byte (i.e. 8 bit), then using `byte` is clearer and better conveys the intention of the code, so I'd use it for readability, even if it uses the same space as an `int`. – sleske Jan 17 '11 at 00:54
  • That's very true, though you get extra bytecode in there to coerce the values into and back out of bytes so there is a small overhead. Readability > tiny intangible benefits though. – izb Jan 17 '11 at 08:51
  • Also, realizing that `int` lends itself better to your intended purpose won’t help if you happen to rely on a library that uses `byte` somewhere (USB stack on Android comes to mind). – user149408 May 20 '17 at 22:50
0

Probably your best bet is to use an integer rather than a byte. It has the room to allow for numbers greater than 128 without the overhead of having to create a special object to replace byte.

This is also suggested by people smarter than me (everybody)

stimms
  • 42,945
  • 30
  • 96
  • 149
0

The best way to do bit manipulation/unsigned bytes is through using ints. Even though they are signed they have plenty of spare bits (32 total) to treat as an unsigned byte. Also, all of the mathematical operators will convert smaller fixed precision numbers to int. Example:

short a = 1s;
short b = 2s;
int c = a + b; // the result is up-converted
short small = (short)c; // must cast to get it back to short

Because of this it is best to just stick with integer and mask it to get the bits that you are interested in. Example:

int a = 32;
int b = 128;
int foo = (a + b) | 255;

Here is some more info on Java primitive types http://mindprod.com/jgloss/primitive.html

One last trivial note, there is one unsigned fixed precision number in Java. That is the char primitive.

martinatime
  • 2,468
  • 1
  • 17
  • 23
0

I know this is a very late response, but I came across this thread when trying to do the exact same thing. The issue is simply trying to determine if a Java byte is >127.

The simple solution is:

if((val & (byte)0x80) != 0) { ... }

If the real issue is >128 instead, just adding another condition to that if-statement will do the trick.

octo
  • 631
  • 3
  • 3
-1

I guess you could just use a short to store them. Not very efficient, but really the only option besides some herculean effort that I have seen.

Nick Berardi
  • 54,393
  • 15
  • 113
  • 135