• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Working with data that was created as "unsigned short"

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have special cameras that output a stream of "unsigned shorts" (as in C) to a file. There are only 12 valid bits and I had assumed that they were stuffed in the LSB so that Java would see them all as positive. I'm at the point where I can display an image with my own code but it's not quite right compared to ImageJ. My calculation of maximum and minimum values are too large, don't match that of ImageJ, and the minimum value is negative. I suspect that the valid bits are the MSB
I'd like to shift the bits to the right but suspect that there will be a conversion to an int which will conserve the sign.

How can I convert these shorts?

TIA
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nate Lockwood wrote:I have special cameras that output a stream of "unsigned shorts" (as in C) to a file. There are only 12 valid bits and I had assumed that they were stuffed in the LSB so that Java would see them all as positive. I'm at the point where I can display an image with my own code but it's not quite right compared to ImageJ. My calculation of maximum and minimum values are too large, don't match that of ImageJ, and the minimum value is negative. I suspect that the valid bits are the MSB
I'd like to shift the bits to the right but suspect that there will be a conversion to an int which will conserve the sign.

How can I convert these shorts?



A conversion from short to int will "sign extend" the value. So, to avoid that you need to mask out the higher order bits.... Something like...



Henry
 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that preserves the data. Now to see what happens in my code!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic