Next: , Previous: Primitive Numerics, Up: Numbers


21.2.14 Bitwise Operations

— Scheme Procedure: logand n1 n2
— C Function: scm_logand (n1, n2)

Return the integer which is the bitwise and of the two integer arguments n1, n2.

Example:

          (number->string (logand #b1100 #b1010) 2)
             ⇒ "1000"
— Scheme Procedure: logior n1 n2
— C Function: scm_logior (n1, n2)

Return the integer which is the bitwise or of the two integer arguments n1, n2.

Example:

          (number->string (logior #b1100 #b1010) 2)
             ⇒ "1110"
— Scheme Procedure: logxor n1 n2
— C Function: scm_logxor (n1, n2)

Return the integer which is the bitwise xor of the two integer arguments n1, n2.

Example:

          (number->string (logxor #b1100 #b1010) 2)
             ⇒ "110"
— Scheme Procedure: lognot n
— C Function: scm_lognot (n)

Return the integer which is the 2's-complement of the integer argument n.

Example:

          (number->string (lognot #b10000000) 2)
             ⇒ "-10000001"
          (number->string (lognot #b0) 2)
             ⇒ "-1"
— Scheme Procedure: logtest n1 n2
— C Function: scm_logtest (n1, n2)

Return #t if any 1-bit in n1 has a corresponding (same position) 1-bit in n2, #f otherwise. Both n1 and n2 are integers.

          (logtest j k) == (not (zero? (logand j k)))
          
          (logtest #b0100 #b1011) ⇒ #f
          (logtest #b0100 #b0111) ⇒ #t
— Scheme Procedure: logbit? idx j
— C Function: scm_logbit_p (idx, j)

Return #t if the idxth bit in j is set, #f otherwise. Both idx and j are integers.

          (logbit? idx j) == (logtest (integer-expt 2 idx) j)
          
          (logbit? 0 #b1101) ⇒ #t
          (logbit? 1 #b1101) ⇒ #f
          (logbit? 2 #b1101) ⇒ #t
          (logbit? 3 #b1101) ⇒ #t
          (logbit? 4 #b1101) ⇒ #f
— Scheme Procedure: ash n cnt
— C Function: scm_ash (n, cnt)

Perform an arithmetic shift of n 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)))).

Example:

          (number->string (ash #b1 3) 2)
             ⇒ "1000"
          (number->string (ash #b1010 -1) 2)
             ⇒ "101"
— Scheme Procedure: logcount n
— C Function: scm_logcount (n)

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.

Example:

          (logcount #b10101010)
             ⇒ 4
          (logcount 0)
             ⇒ 0
          (logcount -2)
             ⇒ 1
— Scheme Procedure: integer-length n
— C Function: scm_integer_length (n)

Return the number of bits neccessary to represent n.

Example:

          (integer-length #b10101010)
             ⇒ 8
          (integer-length 0)
             ⇒ 0
          (integer-length #b1111)
             ⇒ 4
— Scheme Procedure: integer-expt n k
— C Function: scm_integer_expt (n, k)

Return n raised to the non-negative integer exponent k.

Example:

          (integer-expt 2 5)
             ⇒ 32
          (integer-expt -3 3)
             ⇒ -27
— Scheme Procedure: bit-extract n start end
— C Function: scm_bit_extract (n, start, end)

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. For example:

          (number->string (bit-extract #b1101101010 0 4) 2)
             ⇒ "1010"
          (number->string (bit-extract #b1101101010 4 9) 2)
             ⇒ "10110"

Signal out-of-range error if n is negative.