c - Divide by 9 without using division or multiplication operator -


this question have tried solve couldn't way. pointers appreciated.

regular subtraction way of doing division not intention here, ingenious way of using shifting operator done intention.

here's solution heavily inspired hacker's delight uses 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) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -