Re: extracting bits from bytes [message #1532 is a reply to message #1527] |
Tue, 30 November 1993 10:16   |
chase
Messages: 62 Registered: May 1993
|
Member |
|
|
>>>> > "dean" == dean <dean@phobos.cira.colostate.edu> writes:
In article <Nov29.232946.48256@yuma.ACNS.ColoState.EDU> dean@phobos.cira.colostate.edu writes:
dean> I have a (graphic) file that is an 8-bit byte. Each of the 8
dean> bits in each byte indicates the graphic position is on or off.
dean> Is there a way with IDL to determine which of the 8 bits is
dean> on (1) or off (0)? Like it can be done in FORTRAN or C.
dean> Kelly Dean
You can do it just like in C, using a mask and the AND operator.
The IDL Boolean operators operate bitwise for any size integer
operands (byte, integer, long).
Example:
Suppose A is a byte variable containing data from your file. Then the
following will print 1 if bit 3 (numbering bits from 0 in least
significant order) is set and 0 if not set ('n'XB notation is a
hexidecimal byte constant for IDL):
print, (A and '08'XB) ne 0
or the following will print all the bits in succession:
for i=0,7 do print, ((2^i) and A) ne 0
Try it.
If you are checking a lot of data, you should store the masks in an
array:
mask = bytarr(8)
for i=0,7 do mask(i)=2^i
for i=0,7 do print, 'Bit',i,' = ', (mask(i) and A) ne 0
I hope this helps,
Chris
P.S.
Anyone using idl.el or idl-shell.el? Any comments, suggestions,
additional bug reports? Send them my way.
--
===============================
Bldg 24-E188
The Applied Physics Laboratory
The Johns Hopkins University
(301)953-6000 x8529
chris_chase@jhuapl.edu
|
|
|