Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# Convert short to int with & 0xffff

Bernard Pearson
Greenhorn
Posts: 2
I'm very new to Java, so please bear with me. I am trying to make a small plugin for the ImageJ program. I've successfully used some sample code to read the pixels of a 16-bit image into a short array.

short[] pixels = (short[])ip.getPixels();

I need to do some calculations on the pixels, and I would like to convert them to integers so the range is 0-65,535 instead of -32,768 to 32,767.
I found a tutorial that recommends the following to convert the short array into int:

int pix = pixels[i] & 0xffff;

However, when I sum the pixels:

int sumPix = 0;
sumPix = sumPix + pix;

I still end up with a negative number. I thought the " & 0xffff " should remove the sign and leave me with only positive integers.

I've attached the full .java file. Please focus on how to obtain a positive integer value for the sum of the array. I realize some of the other code may not be the ideal way to approach this, but I am trying to learn one thing at a time.

I should mention I've also tried pixels[i].intValue() - this raises an error in the compiler. I believe it is because pixels[i] is a primitive and not an object.

Thanks,
Bernard

Henry Wong
author
Marshal
Posts: 21112
78
Bernard Pearson wrote:
int pix = pixels[i] & 0xffff;

However, when I sum the pixels:

int sumPix = 0;
sumPix = sumPix + pix;

I still end up with a negative number. I thought the " & 0xffff " should remove the sign and leave me with only positive integers.

If you print out each individual conversion, you'll see that it indeed, does work. Negative short numbers are converted to their bit pattern equivalents -- and hence, positive.

However, this doesn't stop the integer from overflowing when taking a sum. If you add enough positive numbers, sooner or later, it will overflow, and become negative.

Henry

Bernard Pearson
Greenhorn
Posts: 2
Thank you so much for the quick and helpful response. I didn't consider overflowing int, but I see that is what's happening. My calculations will include quotients that will introduce decimals, so I am planning on using double to store these. Is there any reason not to convert the short directly into a double even though it won't have a decimal? Should I use long?

Sun's tutorial (http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html) does not provide a simple range for float, but mentions it's 32-bit. Will float overflow as soon as int?

David Newton
Author
Rancher
Posts: 12617
No. You might want to check out some info on numeric formats, though, just for background information.

Henry Wong
author
Marshal
Posts: 21112
78
Bernard Pearson wrote:
Sun's tutorial (http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html) does not provide a simple range for float, but mentions it's 32-bit. Will float overflow as soon as int?

Believe it or not, the absolute highest number that can be represented by a float is... Infinity. I am not kidding. The next highest value is... 3.40282346638528860 times 10 to the 38th power. Regardless, it means that it is not possible to overflow a float, by adding a small (short) number, and get a negative sum.

You can, however, lose precision. It is possible to have the sum be so large, that adding a small number to it, will have no effect.

Henry

Jim Hoglund
Ranch Hand
Posts: 525
Also, the char primitive type works well as an unsigned
16-bit integer and may come in handy at some point.

Jim ... ...