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.
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.
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
8
u/Crispy1961 13d ago
It does work, but it's the most inefficient method. You DIV already computes no sulk.
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