Skip to main content
deleted 46 characters in body
Source Link
Dirk
  • 31.1k
  • 8
  • 86
  • 103

The "literal" way to do this would be something like

(defvar *array* (make-array 4 :element-type 'bit :initial-element 0))
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0010
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0000

or as self-contained function:

(defun nbit-flip (bitset index)
  (setf (bit bitset index) (mod (1+ (bit bitset index)) 2))
  bitset)

You could use (logandlogxor 1 ...) instead of (mod ... 2) and lognot to replacedo the 1+ giving:flipping like

(defun nbit-flip (bitset index)
  (setf (bit bitset index) (logandlogxor 1 (lognot (bit bitset index))))
  bitset)

Just stick to that which may or may not signalsignals your intention moremost clearly.

The "literal" way to do this would be something like

(defvar *array* (make-array 4 :element-type 'bit :initial-element 0))
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0010
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0000

or as self-contained function:

(defun nbit-flip (bitset index)
  (setf (bit bitset index) (mod (1+ (bit bitset index)) 2))
  bitset)

You could use (logand 1 ...) instead of (mod ... 2) and lognot to replace the 1+ giving:

(defun nbit-flip (bitset index)
  (setf (bit bitset index) (logand 1 (lognot (bit bitset index))))
  bitset)

which may or may not signal your intention more clearly.

The "literal" way to do this would be something like

(defvar *array* (make-array 4 :element-type 'bit :initial-element 0))
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0010
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0000

or as self-contained function:

(defun nbit-flip (bitset index)
  (setf (bit bitset index) (mod (1+ (bit bitset index)) 2))
  bitset)

You could use (logxor 1 ...) to do the flipping like

(defun nbit-flip (bitset index)
  (setf (bit bitset index) (logxor 1 (bit bitset index)))
  bitset)

Just stick to that which signals your intention most clearly.

added 159 characters in body
Source Link
Dirk
  • 31.1k
  • 8
  • 86
  • 103

The "literal" way to do this would be something like

(defvar *array* (make-array 4 :element-type 'bit :initial-element 0))
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0010
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0000

or as self-contained function:

(defun nbit-flip (bitset index)
  (setf (bit bitset index) (mod (1+ (bit bitset index)) 2))
  bitset)

You could use (logand 1 ...) instead of (mod ... 2) and lognot to replace the 1+ giving:

(defun nbit-flip (bitset index)
  (setf (bit bitset index) (logand 1 (lognot (bit bitset index))))
  bitset)

which may or may not signal your intention more clearly.

The "literal" way to do this would be something like

(defvar *array* (make-array 4 :element-type 'bit :initial-element 0))
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0010
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0000

The "literal" way to do this would be something like

(defvar *array* (make-array 4 :element-type 'bit :initial-element 0))
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0010
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0000

or as self-contained function:

(defun nbit-flip (bitset index)
  (setf (bit bitset index) (mod (1+ (bit bitset index)) 2))
  bitset)

You could use (logand 1 ...) instead of (mod ... 2) and lognot to replace the 1+ giving:

(defun nbit-flip (bitset index)
  (setf (bit bitset index) (logand 1 (lognot (bit bitset index))))
  bitset)

which may or may not signal your intention more clearly.

Source Link
Dirk
  • 31.1k
  • 8
  • 86
  • 103

The "literal" way to do this would be something like

(defvar *array* (make-array 4 :element-type 'bit :initial-element 0))
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0010
(setf (bit *array* 2) (mod (1+ (bit *array* 2)) 2))
(print *array*) ;; ==> #*0000