views:

158

answers:

3

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?

A: 

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

foret
I asked how this AND operation makes it unsigned
shlomjmi
it doesn't make it unsigned type. Maximum you can achieve with bit shifting magic is to throw minus away. But that doesn't make it unsigned type. Anyway, how do you think that happens? Bitshifting accidentally creates new subclass or hacks in JVM?
foret
A: 

If you just move from a negative byte to int the int will be negative too. Sou you take the negative int (or short in your example) and set the highest 3 byte (int) to 0x00.

Christian Ullenboom
+2  A: 

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

Andrei Fierbinteanu
Any link for the issues about byte and short? I've used byte every once-in-awhile, but never really used short.
Dean J
@DeanJ: I think the issue is performance since you need more casts because many operations return int.
Cloudanger
The first example here demonstrates the point: http://blog.mpathirage.com/2010/06/27/java-puzzlers-phantom-reference-menace-attack-of-the-clone-revenge-of-the-shift/ If you use any operator on the short like + or - it actually transforms into an int and that may cause problems when calling overloaded methods.
Andrei Fierbinteanu
Java *does* jave one unsigned primitive type, it's called "char" :) Most of the time its range is a bit to limited though.
Jörn Horstmann
@Cloudanger; so, issues with byte?
Dean J
I still don't understand: -127 = 11111111 (7 times '1' + extra 1 for negative), but 11111111 AND 11111111 = 11111111 =128
shlomjmi
NO, see my edit2; -127 is 10000001; the first 1 means it's negative, and 0000001 since it's represented in two's complement (0000001 = 2^n - 127; where n is the number of bits of the type, in the case of byte n=8); so it's 11111111 AND 10000001 which is 129
Andrei Fierbinteanu
oh! I missed the two's complement part. Java represents in two's complement ?
shlomjmi
Dean J