# Thread: Struggling to get binary multiplication working

1. ## Struggling to get binary multiplication working

Here's the segment of code (all safety checks have been done before this)
Code:
for ( v = val.init; v.b < val.width; v = bit_op_inc(v) ) {
if ( val.value[v.byte] & v.bit )
#if 0
num.init = bit_op_inc( num.init );
num.init.b = 0;
num.width--;
}
num.init = cpy.init;
num.width = cpy.width;
#else
bnum_op_shl_z( cpy, 1 );
}
#endif
Both variants return the same result (just with an obvious cycle difference:
bnum_op_shl_z() method produces
Code:
...
operation2(-861021530,'<',5) call 4
rstarted 603, rstopped 603, nstarted 603, nstopped 607
num << val -1782885184, -1782885184
...
num + val 679910964, 679910964
operation2(-1125898167,'*',1369133069) call 9
rstarted 630, rstopped 630, nstarted 631, nstopped 680
num * val 1633672117, 2147483647
r(int)		= 0b01100001010111111101101110110101
t(bnum_t)	= 0b01111111111111111111111111111111
num(int)	= 0b10111100111001000010100001001001
val(int)	= 0b01010001100110110101000000001101
Numerical argument out of domain
../makefile:16: recipe for target 'run' failed
make: *** [run] Error 1
Compilation failed.
Other method produces:
Code:
operation2(-861021530,'<',5) call 4
rstarted 936, rstopped 937, nstarted 937, nstopped 945
num << val -1782885184, -1782885184
...
operation2(-635723058,'+',1315634022) call 8
rstarted 987, rstopped 988, nstarted 988, nstopped 992
num + val 679910964, 679910964
operation2(-1125898167,'*',1369133069) call 9
rstarted 995, rstopped 996, nstarted 997, nstopped 1024
num * val 1633672117, 2147483647
r(int)		= 0b01100001010111111101101110110101
t(bnum_t)	= 0b01111111111111111111111111111111
num(int)	= 0b10111100111001000010100001001001
val(int)	= 0b01010001100110110101000000001101
Numerical argument out of domain
../makefile:16: recipe for target 'run' failed
make: *** [run] Error 1
Compilation failed.
Incedently operation2() is called like so:
Code:
for ( i = 0; operations2[i]; ++i ) {
ret = operation2( -(rand()), operations2[i], rand() );
if ( ret != 0 ) return printerr(ret);
}

2. I managed to fix it somehow, the results got screwed up when I tried to call bnum_op_add() apparently,
I copied & pasted the code from that function into bnum_op_mul(), changed the val related variables to cpy equivalants
and everything started going hunky doory ('''一_一)

3. Copy paste gets you every time

4. Yeah but this time instead of handling variables wrong or different than intended it actually did the same thing but skipped a few cycles worth of checking (plus the overhead of calling the function), the problem only persists when I call the bnum_op_add() function which does the same thing but checks the parameters before hand and clamps at MIN/MAX the number can hold afterwards