Is there an easy and elegant way to convert an unsigned byte value to a signed byte value in java? For example, if all I have is the int value 240 (in binary (24 bits + 11110000) = 32bits), how can I get the signed value for this int?
5 Answers
In Java all the primitive types, except for char
, are signed. You can't have an unsigned byte.
The only thing you may do is to cast the unsigned byte to an int so you can read its proper value:
int a = b & 0xff
If you want to store an unsigned byte value in a byte type, you obviously can, but every time you need to "process" it, just remember to cast it again as showed above.

- 3,286
- 1
- 24
- 36

- 496
- 1
- 5
- 10
-
What impact will this have? Does this simply mean that if b is a 32 bit int, then 'and' it with 11111111 will copy the lowest 8 bits? Thanks – Joeblackdev Aug 06 '11 at 11:57
-
Sorry, I forgot to mention that "b" is an unsigned byte saved in a signed byte type (the only available). You can always store unsigned bytes in signed byte types, but you need to perform the above mentioned conversion every time you need to do some math. If you just need to check its bits you don't need that. – ostefano Aug 06 '11 at 12:01
-
I'm still not with you on this. Why do you and the byte b with 0xff? – Joeblackdev Aug 06 '11 at 12:32
-
1It means you upcast the value to another type (you can't assign 0xFF to a byte variable in java), and discard all the bits but the 8 rightmost ones (where the value is stored). – ostefano Aug 07 '11 at 11:27
-
1"In Java all the primitive types are signed.": Not true. `char` is unsigned. – Nayuki Aug 11 '11 at 17:46
-
@NayukiMinase Correct, I forgot that. Thanks for pointing it out. – ostefano Jan 31 '12 at 18:24
Java does not have unsigned values, except for char
. Consider this snippet:
byte val = (byte)255;
System.out.println(String.valueOf(val));
The result will be -1, because the lowest 8 bits got copied over to the byte variable.

- 3,286
- 1
- 24
- 36

- 16,017
- 2
- 36
- 40
-
I see what you mean. An int in java is 32 bits, so the lowest 8 bits get copied, correct? – Joeblackdev Aug 06 '11 at 11:57
-
2Yes. By "lowest" I mean the bits with he lowest positional value (2^n). So bits with positional values 2^0 - 2^7 are copied over, but bits with values 2^8 or higher are discarded. Since it's java, you don't need to worry about endianness. – Tassos Bassoukos Aug 06 '11 at 12:24
-
Also, if you want to do processing with unsigned values, just do what @ostefano wrote - in any case, java does integer arithmetic internally for ints, shorts and bytes anyway. – Tassos Bassoukos Aug 06 '11 at 12:25
-
3
Here's how to store and convert an unsigned byte value:
byte b = (byte) 144; // binary value = 10010000
If you cast b
to an int
here's what happens:
int i = b;
System.out.println("value: "+i);
System.out.println("binary: " + Integer.toBinaryString(i));
Output:
value: -112 <- Incorrect value
binary: 11111111111111111111111110010000
Why does this happen? Bytes are signed in Java, and b
is negative so the cast operation fills the resulting int with 1s from the left. More on java data formats.
So how do we get back from -112 to the correct value of 144? We need a 32-bit bitmask, which we can create with an int
:
int mask = 0x000000FF; // This is often shortened to just 0xFF
Now we can use the bitwise &
operator to "zero out" the left most 24 bits, leaving only our original 8 bits.
int unsignedValue = b & mask; // value = 144
Our original value of 144 has been restored and can be safely cast back to a byte:
byte original = (byte) unsignedValue;
public int getUnsignedByte(byte[] bytes, int offset) {
return (bytes[offset] & 0xFF);
}
should do the work.

- 8,911
- 4
- 41
- 50
Java only supports signed bytes so whenever you place a value in a byte, its assumed to be signed.
byte b = (byte) 240;
However if you want to store an unsigned byte, you need to handle this yourself. (i.e. Java doesn't support it but you can do it)
For operations like +, -, *, <<, >>>, ==, !=, ~ you don't need to change anything, For operations like <, > you need to have make minor adjustments, and for operations like /, % you need to use a larger data type.
A common alternative is to use a larger data type like int
to store values 0 - 255. There is not much disadvantage in doing so.

- 525,659
- 79
- 751
- 1,130