-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplier.java
More file actions
34 lines (32 loc) · 1.59 KB
/
Multiplier.java
File metadata and controls
34 lines (32 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Multiplier {
// Multiply, like subtract, is built ontop of add's core functionality. In this code, it's
// repeatedly adding to a by b utilizing leftshift Every time b has a bit value set to true.
public static void multiply(Word32 a, Word32 b, Word32 result) {
clearWord(result);
// Ignore any cases where a or b is negative, return result cleared as is.
if (!a.equals(new Word32()) || !b.equals(new Word32())) {
// temp is used to calculate the added result of a after each iteration.
Word32 temp = new Word32();
for (int i = 31; i >= 0; i--) {
// bBit initialized and set to false, each iteration checks to see if its true.
// If it's found to be true, then make a new shifted A, and then leftShift dpeneding on how far
// the bit is from the right.
Bit bBit = new Bit(false);
b.getBitN(i, bBit);
if (bBit.getValue() == Bit.boolValues.TRUE) {
Word32 shiftedA = new Word32();
Shifter.LeftShift(a, 31 - i, shiftedA);
// Add result by shiftedA and put it into temp, since a product of two binary numbers is the
// sum of all the leftshifted binaries at b bits set to 1.
Adder.add(result, shiftedA, temp);
temp.copy(result);
}
}
}
}
public static void clearWord(Word32 input){
for(int i = 0; i < 32; i++){
input.setBitN(i, new Bit(false));
}
}
}