Logiweb(TM)

7.5.3 Bit operations

Prev Up Next Page 188 of 800 Search internet


The following five operations are central in defining integer operations.

evenp ( x )
oddp ( x )
half ( x )
small ( x )
double ( b , i )

The first is true if x is even, the second is true if x is odd, the third computes x div 2, the fourth is true if x is zero or minus one, and the fifth computes 2 * i if b is false and 2 * i + 1 if b is true. If one applies the halving operation repeatedly to an integer, one eventually ends up with a 'small' integer (zero or minus one). The other way round, all integers can be reached by applying the double operation repeatedly, starting with zero or minus one.

The following operations (name as well as semantics) is taken from the Common Lisp standard.

lognot ( x )          Bitwise complement
logior ( x , y )      Bitwise inclusive or
logxor ( x , y )      Bitwise exclusive or
logand ( x , y )      Bitwise and
logeqv ( x , y )      Bitwise exclusive nor
lognand ( x , y )     Bitwise nand
lognor ( x , y )      Bitwise nor
logandc1 ( x , y )    Bitwise and of y and complement of x
logandc2 ( x , y )    Bitwise and of x and complement of y
logorc1 ( x , y )     Bitwise or of y and complement of x
logorc2 ( x , y )     Bitwise or of x and complement of y
logtest ( x , y )     True if bitwise and is nonzero
ash ( i , c )         Arithmetic shift
logbitp ( x , i )     True if bit x of i is one
logcount ( x )        Return number of one/zero bits
integer-length ( x )  Return size of integer

For the sake of bit operations, integers are treated as infinite series of bits. Positive integers have an infinite number of zero bits and a finite number of one bits. The opposite holds for negative numbers.

For the arithmetic shift, a positive value of c causes i to be multiplied by "2^c". For a negative value of c, i is halved -c times.

The logcount function returns the number of one-bits in x for positive x and the number of zero bits in x for negative x.

The integer-length of small integers (zero and minus one) is zero. In general, the integer-length indicates the number of times one must halve the integer to get a small integer.

Prev Up Next Page 188 of 800 Search logiweb.eu

Copyright © 2010 Klaus Grue, GRD-2010-01-05