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

node.js - Using Node without global install -

java - Null response to php query in android, even though php works properly -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -