Thursday, February 17, 2011

least significant bt

Obtaining a least signigicant bit of x is very easy: x & (-x). Since x + (-x) = 0 = 2^(word size) the number -x must have the same value of lsb as x. Addition of these two bits yeilds zero and a carry, and this carry is passed all the way to the right leaving zeros in the result. But the latter can only happen if other bits of x and -x are inverse of each other. This completes an explanation

Wednesday, February 16, 2011

mean for unsigned integers

from http://spamsink.livejournal.com/360680.html:

 floor((a+b)/2) = (a^b)/2 + a&b

negation without multiplying

To negate an integer x one can simply do ~x+1. To see why this works consider ~x = -1U - x which is equivalent to ~x+1 = -x

Tuesday, February 8, 2011

Spurious interrupts

8259A programmable interrupt controller is famous for it's spurious interrupts. I haven't caught any on core i5 with intel chipset, but on a machine with AMD processor and GeForce 7025 chipset one is constantly bugging me right after I enable interrupts. It happens only once, so just having a spurious handler for it is fine.