bit operation example from K+R vs. newlisp

Started by maq, September 30, 2006, 04:00:46 PM

Previous topic - Next topic

maq

I was perusing through K+R "The C Programming Language" and came across this tidbit on bit operations:



http://www.angrypacket.com/~maq/getbits.png">



And the (p + 1 - n) doesn't make sense, as it would seem you actually want to do (p - n). I wrote the same function in newlisp both ways:



(p - n):



http://www.angrypacket.com/~maq/p-n.png">



(p + 1 - n):



http://www.angrypacket.com/~maq/p+1-n.png">



And the (p - n) is the one behaving as I would expect. What am I missing here? Am I not understanding K+R properly in their example? I couldn't imagine that this is a typo.



Any insights to clear my confusion would be greatly appreciated.



--maq

Lutz

#1
I guess this is a offset 0 versus offset 1 confusion:


10 0001 1100 ; binary 540
98 7654 3210


7 picks from bits 432 and

3 from bits 542



The 'C' language normally works with 0 offset so I believe the K&R text is  correct starting at the 6th bit, which is at offset 5, thats why they are adding 1, because they have to shift out 6 bits in total. So 3 is the correct result with shifting (>> bfield (- (+ pos 1) n)) .



Lutz