Here’s a useful routine bits I wrote for the uLisp RISC-V assembler, but it has applications in programming for microcontroller boards such as the Arduino. It extracts a single bit, or a bit field of several bits, from the number provided as its first parameter:
(defun bits (x a &optional b) (if b (logand (ash x (- b)) (1- (ash 1 (- a b -1)))) (logand (ash x (- a)) 1)))
To extract a single bit provide the bit position of the bit as the second argument; for example:
> (bits #b1001011001010011 4) 1
To extract a bitfield, specify the starting and ending bit positions, inclusive; for example:
> (format t "~b" (bits #b1001011001010011 6 2)) 10100
Note that I’m taking advantage of the ~b format directive added in uLisp 3.5 to show the result in binary.