Suppose I have an integer such as 109, 1101101 in binary. How do I iterate over bits of this number, eg: [64, 32, 8, 4, 1]? What would be a good way of doing that in lisp? Should I modify the for macro a bit by adding a case or should I just convert the integer into a bit-vector or a list?
Share
If you want to process only “ones” then looping over all bits is not efficient if the ones are few. This is what I’d do in this case
It uses the nice 2-complement facts that bit-anding a number and its opposite returns the least significant set bit and that bit-anding a number and one less than the number zeroes this least significant set bit.
Note that this processing works from the least significant set bit to the most significant (in your example you used the opposite ordering)