[comp.lang.lisp] bit operator bug in Lucid/Sun lisp

kadie@cs.uiuc.edu (Carl M. Kadie) (11/05/90)

Arggg!! It took me a day to track this bug down.

--------------------------
lucid> (bit-ior #*0110000001100101 #*0001100000000010)
#*0000000000000000
--------------------------

It seems that some of the other bit operators are
also buggy on "long" arguments.


The compiler is Lucid/Sun 3.0.0.

I hope version 4.0 fixes this. In the mean time, here is a patch that
also fixes the Lucid's "(make-random-state t)" bug:

-------------------------------------------------
;;; Lucid bug fix - Carl Kadie, 1990
(when (string= (lisp-implementation-type) "Lucid Common Lisp")

   (make-random-state t) ;; initialize the random number generator
	      ;; in Lucid that doesn't work so ...
	      (dotimes (i (mod (get-universal-time) 500)) (random 10))
      )	

      
   (defun BIT-OP (bit-array-1 bit-array-2 table &optional result-bit-array)
	"
       (bit-op #*0011 #*0101 #*0001) ;=> #*0001
	     "
	(cond ((null result-bit-array)
	       (setf result-bit-array
		     (make-array
		       (array-dimensions bit-array-1)
		       :element-type (array-element-type bit-array-1))))
	      ((eq result-bit-array 't)
	       (setf result-bit-array bit-array-1)))
	(assert (vectorp bit-array-1) nil
		"Sorry, patch of bit-ior works only on vectors")
	(dotimes (n (array-total-size bit-array-1) result-bit-array)
		 (setf (bit result-bit-array n)
		       (bit table
			    (+ (* 2 (bit bit-array-1 n))(bit bit-array-2 n))))
		 ))
   (compile 'bit-op)

   ;; This version of lisp has problems with long bit vectors, for example,
   ;; it says that 
   ;;  (bit-andc2 #*1111111111111111111111111 #*1111111111111101111111111)
   ;;      ;=>  #*0000000000000000000000000, but it should
   ;;      be  #*0000000000000010000000000.
   ;; There are also problems in BIT-IOR, and maybe other operators.

   (defun BIT-AND   (b1 b2 &optional ra) (bit-op b1 b2 #*0001 ra))
   (defun BIT-IOR   (b1 b2 &optional ra) (bit-op b1 b2 #*0111 ra))
   (defun BIT-XOR   (b1 b2 &optional ra) (bit-op b1 b2 #*0110 ra))
   (defun BIT-EQV   (b1 b2 &optional ra) (bit-op b1 b2 #*1001 ra))
   (defun BIT-NAND  (b1 b2 &optional ra) (bit-op b1 b2 #*1110 ra))
   (defun BIT-NOR   (b1 b2 &optional ra) (bit-op b1 b2 #*1000 ra))
   (defun BIT-ANDC1 (b1 b2 &optional ra) (bit-op b1 b2 #*0100 ra))
   (defun BIT-ANDC2 (b1 b2 &optional ra) (bit-op b1 b2 #*0010 ra))
   (defun BIT-ORC1  (b1 b2 &optional ra) (bit-op b1 b2 #*1101 ra))
   (defun BIT-ORC2  (b1 b2 &optional ra) (bit-op b1 b2 #*1011 ra))
   )
--
Carl Kadie -- kadie@cs.uiuc.edu -- University of Illinois at Urbana-Champaign
Fourth Amendment (War-on-Drugs version): The right of the people to be secure 
in their persons shall not be violated but upon probable cause 
*or for random urine tests*