Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Java unsigned numbers

I want to understand how to convert signed number into unsigned.

lets say I have this:

byte number = 127; // '1111111'

In order to make it unsigned I have to choose "bigger" data type 'short' and apply AND operator with value 0x00ff.

short number2;  
number2 = number & 0x00ff;

Why does it make the number unsigned?

like image 255
shlomjmi Avatar asked Aug 02 '10 13:08

shlomjmi


People also ask

What are unsigned values in Java?

Java does not have a datatype for unsigned integers. You can define a long instead of an int if you need to store large values. You can also use a signed integer as if it were unsigned.

Does Java support unsigned numbers?

It consist both negative and positive values but in different formats like (-1 to -128) or (0 to +127) . An unsigned integer can hold a larger positive value, and no negative value like (0 to 255) . Unlike C++ there is no unsigned integer in Java.

What does unsigned integer mean in Java?

A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0 to 4294967295].

Why does Java not support unsigned?

The Java language specification requires its signed integers to be represented in two's complement format. Because of this, many basic operations are exactly the same whether the integer type is signed or unsigned.


2 Answers

Java doesn't actually have unsigned primitives.

The value 127 is actually represented by '01111111' the first bit being the sign (0 is positive).

An unsigned byte would be able to hold values 0 to 255, but 127 is the maximum for a signed byte. Since a byte has 8 bits, and the signed one consumes one to hold the sign. So if you want to represent values larger than 127 you need to use a bigger type that has a greater number of bits. The greater type also has a reserved bit for sign, but it has at least 8 bits used for the actual values, so you can represent the value 255.

That being said, you should probably avoid using byte and short because there are issues with them. You'll notice i cast the result to short, since the operators actually return int. You should just stick to int and long in java since they are implemented better.

Edit: the AND operator makes it unsigned since the sign bit is the first bit of the short, and you copy the 8 bits holding the value of the byte to the last 8 bits of the short. So if you have a negative number the first bit which is 1 (that means it's negative) actually becomes part of the value. And the short will always be positive since its sign bit is at two high of a power of two to be affected by the short.

 byte:             10101101
                    ||||||| <- actual value
short:     0000000010101101
            ||||||||||||||| <- actual value

Edit 2: Take note though that since the negative values use two's complement representation the value might not be what you expect it. all the positive values remain the same.
But -128 = 0x10000000 will become 128
-127 = 0x10000001 will become 129
and so on until -1 = 0x11111111 which will become 255

like image 132
Andrei Fierbinteanu Avatar answered Sep 29 '22 07:09

Andrei Fierbinteanu


java does not have unsigned data types. You just make switch to "bigger" data type, that's all.

like image 28
foret Avatar answered Sep 29 '22 06:09

foret