Re: bit operations [message #30551] |
Thu, 02 May 2002 11:17 |
Liam E. Gumley
Messages: 378 Registered: January 2000
|
Senior Member |
|
|
Dominik Paul wrote:
> are there any other bit operations in IDL except shift and ishift?
> I would like to know, if a bit is set and I would like to set a bit by
> myself in a variable.
The Boolean operators in IDL (AND, NOT, OR, XOR) perform bit-wise
operations on integer arguments (BYTE, INT, LONG, etc.). To check if a
particular bit is set,
(a) SHIFT the bit into position zero
(b) Use AND to test if the bit is set
For example:
IDL> word = 35B ; bits 5, 1,and 0 are set
IDL> bit = 5
IDL> print, ishft(word, -bit) and 1B
1
IDL> bit = 4
IDL> print, ishft(word, -bit) and 1B
0
To set a particular bit, simply add the appropriate power of two:
IDL> bit = 6
IDL> tmp = word ; this variable protects the type of WORD
IDL> tmp[0] = 2
IDL> word = word + tmp ^ bit
IDL> print, ishft(word, -bit) and 1B
1
For more information, see section 2.6 of my book "Practical IDL
Programming". To earn extra credit, try coding a pair of functions named
GETBIT and SETBIT that operate on integer arguments. Better still, try
the IDL Astronomy library, because someone has probably done it already.
Cheers,
Liam.
Practical IDL Programming
http://www.gumley.com/
|
|
|
Re: bit operations [message #30552 is a reply to message #30551] |
Thu, 02 May 2002 11:01  |
thompson
Messages: 584 Registered: August 1991
|
Senior Member |
|
|
"Dominik Paul" <dpaul@ukl.uni-freiburg.de> writes:
> Hi there,
> are there any other bit operations in IDL except shift and ishift?
> I would like to know, if a bit is set and I would like to set a bit by
> myself in a variable.
Dom:
The logical operators AND, OR, XOR and NOT are bitwise operators. These can be
used to both set and interrogate bits in integer values.
For example, suppose that J=5b, which is '0101' in bit notation. One can turn
on the second bit from the right with the command
IDL> J = J OR 2b
This converts J to '0111', i.e. 7b.
On the other hand, if you want to know if the 2nd bit is turned on or not, you
can find out with
IDL> IF (J AND 2B) NE 0 THEN PRINT, 'Second bit is turned on'
Bill Thompson
|
|
|