[fitsbits] Unsigned Integers

William Pence pence at tetra.gsfc.nasa.gov
Wed Nov 24 16:36:24 EST 1999


Steve Willner wrote:
> 
> Some while ago, somebody posted a trick (or anyway a method) for
> handling unsigned, 16-bit integers in FITS images.  Could somebody
> please repost or point me to a copy of the method?  I seem to recall
> some clever use of BZERO, but I can't figure out how to make it work.
> Thanks.

The method is to subtract 32768 (2**15) from each unsigned 16-bit integer
value (which converts it into the range of a signed 16-bit integer) before
writing it to the FITS file. Then add the keyword BZERO = 32768 to the FITS
header to tell the reading software that this zero point offset must be added
back to the raw signed integer values in the FITS file to regenerate the
actual image pixel values.

The 'trick' is that subtracting 32768 from a 16-bit unsigned integer is
equivalent to flipping the most significant bit of the number, which is
computationally much faster.  To add 32768 to the value when reading, you just
flip the most significant bit again, then interprete the bits as an unsigned
16-bit integer.

The same thing can be done with unsigned 32-bit integers, using BZERO =
2147483648 (i.e., 2**31).

-Bill Pence



More information about the fitsbits mailing list