Well, with the transitions that you have in mind, if you go through the lines 0-s and 1-s, you count every second when a 0 follows 1 or 1, follows 0.
This is easy by shifting bits and counting changes:
transitions(n)
result = 0
prev = n mod 2
n = n div 2
while n<>0
if n mod 2 <> prev then
result++
prev = n mod 2
fi
n = n div 2
elihw
return result
you can replace mod and div with shifts.
source
share