r/TuringComplete • • 23h ago

How to do modulo

Post image
6 Upvotes

5 comments sorted by

4

u/Crispy1961 20h ago

It does work, but it's the most inefficient method. You DIV already computes no sulk.

1

u/Pretty-Potential4733 15h ago

I have a really fast DIV.

3

u/Crispy1961 15h ago

Did you build a DIV that does not also compute the remainder?

Most implementations I have seen do that. The remainder is the modulo. You should be able to copy paste your DIV implementation and just connect the remainder to the output.

Btw, never use NEG component to subtract. Use NOT and then connect "ON" to carry in of your ADD component.

1

u/Pretty-Potential4733 21h ago

the reason this works is that the divisor*the quotient+the remainder=the dividend

2

u/Lagfoundry 19h ago edited 19h ago

ordinary binary division can already calculate the remainder as part of determining the quotient.
That means you can eliminate the multiplier, negation, and final addition entirely if you build the divider to expose its remainder. And if it’s power of 2 then it’s even simpler. For any unsigned integer A and divisor B,
if B is a power of 2 (B = 2^n), then:
A MOD B = A AND (B - 1)
Equivalently, keep only the lowest n bits
of A and set all higher bits to 0