Re: Extracting bits with IDL [message #2592 is a reply to message #2591] |
Mon, 18 July 1994 06:51   |
gumley
Messages: 10 Registered: May 1993
|
Junior Member |
|
|
In article <Ct2GM1.1780@yuma.ACNS.ColoState.EDU>,
dean@phobos.cira.colostate.edu wrote:
> We are set up to routinely collect GOES-8 GVAR data. Like with most data
> from satellites, they put information in the individual bits. Below is a C
> structure that extracts the individual bits from part of the data header which
> contains some time information. Extracting individual bits from data with IDL
> is difficult. At first I consider building an IDL structure, but IDL is
> unable to break down to bits.
IDL can actually extract bits quite easily. You just need to use the ISHFT
and AND functions. ISHFT shifts byte, integer, or longword arguments to
the left or right, and fills any vacant positions with zeros. AND performs
a bitwise AND on byte, integer, or longword arguments. For example, let's
say I have an eight bit word, and I want to extract the first 4 bits and
the last 4 bits as two separate values. To extract the leftmost 4 bits,
you would use
value = ishft( bytval, -4 )
which shifts to the right 4 positions, and fills in the space created on
the left with zeros. To get the rightmost 4 bits, you would use
value = bytval and 15
which does a bitwise AND with any bits to set to 1 in the rightmost 4
positions. You can also use shift/mask(and) operations to extract other
bit fields. For example if you wanted to extract bits 4 and 3 (bits are
typically numbered left to right in descending order i.e. bit 7 to bit 0 in
an 8 bit word), you would use
value = ishft( bytval, -3 ) and 3
I hope this helps. It is usually instructive to write a short test program
to convince yourself that it works.
Cheers,
Liam.
--
Liam E. Gumley
NASA/GSFC Climate and Radiation Branch
Greenbelt MD, USA
|
|
|