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/
|
|
|