这个问题我试图解决这个问题,但不能得到任何办法。任何指针将AP preciated。
This question I have tried to solve it but couldn't get any way. Any pointers would be appreciated.
在做除法的定期减法的方式是不是这里的意图,利用移动运营商来完成这件事是意图巧妙的方法。
Regular subtraction way of doing division is not the intention here, ingenious way of using shifting operator to get this done is the intention.
推荐答案下面是一个解决方案,通过黑客的喜悦很大程度上启发真的只使用移位:
Here's a solution heavily inspired by Hacker's Delight that really uses only bit shifts:
def divu9(n): q = n - (n >> 3) q = q + (q >> 6) q = q + (q>>12) + (q>>24); q = q >> 3 r = n - (((q << 2) << 1) + q) return q + ((r + 7) >> 4) #return q + (r > 8)