wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Convert short to int with & 0xffff Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert short to int with & 0xffff" Watch "Convert short to int with & 0xffff" New topic
Author

Convert short to int with & 0xffff

Bernard Pearson
Greenhorn

Joined: Jun 08, 2010
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
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bernard Pearson
Greenhorn

Joined: Jun 08, 2010
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

Joined: Sep 29, 2008
Posts: 12617

No. You might want to check out some info on numeric formats, though, just for background information.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

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

Joined: Jan 09, 2008
Posts: 525
Also, the char primitive type works well as an unsigned
16-bit integer and may come in handy at some point.

Jim ... ...


BEE MBA PMP SCJP-6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert short to int with & 0xffff
 
Similar Threads
Play video file frame by frame
Image Operations
Another one from "Art and Science of Java"
question on loop logic
double precision floating point