Node:Bitwise Operations, Next:, Previous:Primitive Numerics, Up:Numbers

#### 21.2.14 Bitwise Operations

 logand n1 n2 Scheme Procedure
 Return the bitwise AND of the integer arguments. ```(logand) => -1 (logand 7) => 7 (logand #b111 #b011 #b001) => 1 ```

 logior n1 n2 Scheme Procedure
 Return the bitwise OR of the integer arguments. ```(logior) => 0 (logior 7) => 7 (logior #b000 #b001 #b011) => 3 ```

 logxor n1 n2 Scheme Procedure
 Return the bitwise XOR of the integer arguments. A bit is set in the result if it is set in an odd number of arguments. ```(logxor) => 0 (logxor 7) => 7 (logxor #b000 #b001 #b011) => 2 (logxor #b000 #b001 #b011 #b011) => 1 ```

 lognot n Scheme Procedure scm_lognot (n) C Function
 Return the integer which is the 2s-complement of the integer argument. ```(number->string (lognot #b10000000) 2) => "-10000001" (number->string (lognot #b0) 2) => "-1" ```

 logtest j k Scheme Procedure scm_logtest (j, k) C Function
 ```(logtest j k) == (not (zero? (logand j k))) (logtest #b0100 #b1011) => #f (logtest #b0100 #b0111) => #t ```

 logbit? index j Scheme Procedure scm_logbit_p (index, j) C Function
 ```(logbit? index j) == (logtest (integer-expt 2 index) j) (logbit? 0 #b1101) => #t (logbit? 1 #b1101) => #f (logbit? 2 #b1101) => #t (logbit? 3 #b1101) => #t (logbit? 4 #b1101) => #f ```

 ash n cnt Scheme Procedure scm_ash (n, cnt) C Function
 The function ash performs an arithmetic shift left by cnt bits (or shift right, if cnt is negative). 'Arithmetic' means, that the function does not guarantee to keep the bit structure of n, but rather guarantees that the result will always be rounded towards minus infinity. Therefore, the results of ash and a corresponding bitwise shift will differ if n is negative. Formally, the function returns an integer equivalent to `(inexact->exact (floor (* n (expt 2 cnt))))`. ```(number->string (ash #b1 3) 2) => "1000" (number->string (ash #b1010 -1) 2) => "101" ```

 logcount n Scheme Procedure scm_logcount (n) C Function
 Return the number of bits in integer n. If integer is positive, the 1-bits in its binary representation are counted. If negative, the 0-bits in its two's-complement binary representation are counted. If 0, 0 is returned. ```(logcount #b10101010) => 4 (logcount 0) => 0 (logcount -2) => 1 ```

 integer-length n Scheme Procedure scm_integer_length (n) C Function
 Return the number of bits necessary to represent n. ```(integer-length #b10101010) => 8 (integer-length 0) => 0 (integer-length #b1111) => 4 ```

 integer-expt n k Scheme Procedure scm_integer_expt (n, k) C Function
 Return n raised to the non-negative integer exponent k. ```(integer-expt 2 5) => 32 (integer-expt -3 3) => -27 ```

 bit-extract n start end Scheme Procedure scm_bit_extract (n, start, end) C Function
 Return the integer composed of the start (inclusive) through end (exclusive) bits of n. The startth bit becomes the 0-th bit in the result. ```(number->string (bit-extract #b1101101010 0 4) 2) => "1010" (number->string (bit-extract #b1101101010 4 9) 2) => "10110" ```