Hacker News new | ask | show | jobs
by traverseda 1147 days ago
I can never remember how modulus works, I use it so infrequently.
4 comments

Just use n & 1 then. An odd number ends in binary 1; and’ing it with 1 yields 1 if its odd.
You don't even have to get that cute. Do integer division, multiply the result by the divisor, and subtract the product from the original dividend. Surely you can't say you don't use multiplication, division, and subtraction.
Sure, but you probably remember division.

"I can't remember of n % 2 returns 0 or 1 on even, so in place of that I wrote (if ((n/2)* 2 == n) for even, assuming integral division"

highly recommend learning how division works within assembly. If you can understand sending this data into registers, you will never forget how mod works. Honestly, I dont even care if you use modulus or not. Solve the problem within a function demonstrating you know CS101 concepts we are good. like, really. No judgement, this is just an idiot filter.
I think implementing a function that does what a modulo operator would is itself a fairly straightforward question.
Which kind? For example, can you name a difference between Python and C semantics for % op (if any).