Re: bits from bytes --> DANGEROUS when used in IF [message #10652] |
Mon, 22 December 1997 00:00 |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
Robert Moss wrote:
>
> Kelly Dean wrote:
>>
>> I need to extract the first 3 bits from a byte.
>>
>> Any suggestions?
>>
[...]
> b = 11B ; a byte of data
[...] individual bit.
> If what you want is the first three bits together,
> it would simply be ( b AND 7 ) = 3
>
Be careful with this expression in IF statements !
This can be very dangerous, because of IDL's handling of TRUE and FALSE
values. Often, what you want to do is something like
if (bit_set(b)) then ...
Now try
if ( b AND 7 ) then print,"GREAT !"
This will work for b=11B, but not for b=10B !
The solution is :
if ((b AND 7) gt 0) then print,"GREATER !"
Martin.
------------------------------------------------------------ -------
Dr. Martin Schultz
Department for Earth&Planetary Sciences, Harvard University
186 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA
phone: (617)-496-8318
fax : (617)-495-4551
e-mail: mgs@io.harvard.edu
IDL-homepage: http://www-as.harvard.edu/people/staff/mgs/idl/
------------------------------------------------------------ -------
|
|
|