r/TuringComplete • • 13d ago

How to do modulo

Post image
10 Upvotes

11 comments sorted by

8

u/Crispy1961 13d ago

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

2

u/Pretty-Potential4733 13d ago

I have a really fast DIV.

7

u/Crispy1961 13d 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 6d ago

whatever

2

u/Crispy1961 6d ago

Whatever? Am I bothering you by trying to help? Sorry.

1

u/Fastpast93 6d ago

He didn't ask for help. He was showing an easy method to do modulo.

1

u/Crispy1961 6d ago

And I improved upon it. The easiest way to do modulo is to copy paste your sub solution.

If for some reason the dub doesn't compute mod, then this is a nice approach, but the NEG is unnecessary. Using NOT and carry-in of adder to subtract is a nifty little trick.

Are we not here to share and learn together?

1

u/Fastpast93 6d ago

It's very good you helped, I just wouldn't be offended if the person discarded it.

4

u/Pretty-Potential4733 13d ago

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

4

u/Lagfoundry 13d ago edited 13d 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

1

u/Pretty-Potential4733 4d ago

Non powers of two.