PDA

View Full Version : Unravelling bitcodes



Ferroequine
2005-02-18, 05:22 PM
I've got this suspicion that I should know how to do this, but alas, I'm having a blond week. Does anyone know a quick & easy way to unravel (break down) a sum of bit codes? ie taking the number 475 and figuring out that it contains bitcodes 1, 2, 8, 16, 64, 128 & 256.

msretenovic
2005-02-18, 06:21 PM
I've got this suspicion that I should know how to do this, but alas, I'm having a blond week. Does anyone know a quick & easy way to unravel (break down) a sum of bit codes? ie taking the number 475 and figuring out that it contains bitcodes 1, 2, 8, 16, 64, 128 & 256.
This will return a list of all the bitcodes in a bit. It was written by Stig Madsen.


(defun findBit(bit / bitList expn pat)
(setq expn 0
pat 1
)
(while (>= bit pat)
(if (= (logand bit pat) pat)
(setq bitList (cons pat bitList))
)
(setq pat (expt 2 (setq expn (1+ expn))))
)
bitList
)


To check to see if a bit is in a code do something like this:


(if (member 2 (findBit 475))
;;do something
)


HTH,