Learn To Code: 16bit Binary Multiply and Division operations

Started by kevin, March 19, 2022, 08:32:48 PM

Previous topic - Next topic

kevin

   Learn To Code:  16bit Binary Multiply and Division operations

   Ever wonder how your computers performs Multiply or even Division in a fixed time ?    

   

     OPerators Used:  + Addition  - Subtraction  << Shit Left/Up  >> Shift Right/Down  and ^ power


PlayBASIC Code: [Select]
; PROJECT : 16bit Multiply and Divide Functions
; AUTHOR : Kevin Picone - PlayBASIC Tutor
; CREATED : 20/03/2022
; EDITED : 20/03/2022
; ---------------------------------------------------------------------

// Run Code in DEBUG MODE (f7 from IDE)


MaxTests=100

Test_Mults(MaxTests)

Test_Division(MaxTests)

Sync
waitkey



Function Test_Mults(MaxTests)

s$=make$("-",100)
#print s$
#print "Multiply Test"+make$("-",60)
#print s$
a=100
for lp= 1 to MaxTests
a=rnd($ffff)
b=rnd($ffff)

result1=Mult(a,b)
result2=a*b

Status=Result1<>Result2
Failed+=Status
#print "Mult:("+str$(a)+","+str$(b)+")="+str$(Result1)+" A*B="+str$(result2)+" Status="+str$(Status)
next
#print ""
#print "Result="+Str$(Failed)+" Fails"
#print s$
#print ""
#print ""

Endfunction




Function Test_Division(MaxTests)

s$=make$("-",100)
#print s$
#print "Division Test"+make$("-",60)
#print s$
a=100
for lp= 1 to MaxTests
a=rnd($ffff)
b=rnd($ffff)
swapiflower a,b

result1,Remainder1 =Div(a,b)
result2 =a/b
Remainder2 = a-(B*result2)
Status=Result1<>Result2
Failed+=Status
#print "Div:("+str$(a)+","+str$(b)+")="+str$(Result1)+" Remainder:"+str$(Remainder1)+" A/B="+str$(result2)+" Remainder:"+str$(Remainder2)+" Status="+str$(Status)
next
#print ""
#print "Result="+Str$(Failed)+" Fails"
#print s$
#print ""
#print ""

Endfunction



/* -------------------------------------------------------------
------------>> Unsigned Integer 16bit Multiply <<------------
-------------------------------------------------------------

This function performs a fixed time unsigned 16bit multiply
between a pair of 16 bit integers.

------------------------------------------------------------- */



Function Mult(A,B)

if B
For lp =0 to 15
if B and 1
Result += A << lp
endif
B=B >> 1
next
endif

EndFunction Result


/* -------------------------------------------------------------
------------>> Unsigned Integer 16bit Divider <<------------
-------------------------------------------------------------

This function performs a fixed time unsigned 16bit division
with remainder between a pair of 16bit integers

------------------------------------------------------------- */



Function Div(A,B)
remainder=A
if B
For lp =15 to 0 step -1
if (B<<lp)<=A
A-=(B<<lp)
Result+=2 ^ lp
endif
next
remainder=A
endif

EndFunction Result,Remainder




kevin

Learn To Code: 16bit Addition Operations  (unsigned integer operations)


PlayBASIC Code: [Select]
      //  Run Code in DEBUG MODE (f7 from IDE)
MaxTests=100

Test_Addition(MaxTests)

Sync
waitkey




/* -------------------------------------------------------------
------------>> Unsigned Integer 16bit Addition <<------------
-------------------------------------------------------------

This function performs an unsigned 16bit ADDITION
between a pair of 16 bit integers.

It steps through each bit pair adds them and carries
the overflow forward to the next operation

------------------------------------------------------------- */



Function Add(A,B)

for lp =0 to 15

// Get the bits we want from the input
TempA = (A>>lp) and 1
TempB = (B>>lp) and 1

// Tally these bits with carry
Bit = TempA + TempB+Carry

// Mask bit 0 in the output
Result|= (Bit & 1) << lp

// the carry is bit 1
Carry = (Bit >>1)
next

EndFunction Result







Function Test_Addition(MaxTests)

s$=make$("-",100)
#print s$
#print "Addition Test"+make$("-",60)
#print s$
a=100
for lp= 1 to MaxTests
a=rnd($ffff)
b=rnd($ffff)

result1=Add(a,b)
result2=(a+b) and $ffff

Status=Result1<>Result2
Failed+=Status
#print "Add:("+str$(a)+","+str$(b)+")="+str$(Result1)+" A+B="+str$(result2)+" Status="+str$(Status)
next
#print ""
#print "Result="+Str$(Failed)+" Fails"
#print s$
#print ""
#print ""

Endfunction








  This function is a PlayBASIC implementation of bitwise addition, which shows how to add two numbers using bitwise operations instead of traditional arithmetic. The process mimics how a computer adds numbers at the binary level using XOR and AND operations with a carry mechanism.

Here's a step-by-step breakdown of how the function works:

### Step-by-Step Explanation:

1. **Initialize Variables:**
   - `A`: The first number to be added.
   - `B`: The second number to be added.
   - `Result`: Initially set to the value of `A` (the first number).
   - `Carry`: Initially set to the value of `B` (the second number).
   
   These variables represent the two numbers being added (`A` and `B`), the result of the addition (`Result`), and the carry value from bitwise operations (`Carry`).



2. **Loop Until No More Carry:**


   - The `While Carry` loop continues as long as there is a carry value to add. The loop's purpose is to handle the propagation of carries that result from binary addition.

3. **XOR Operation:**

   - The line `A = Result xor Carry` performs a bitwise XOR between `Result` and `Carry`.

     - XOR adds the two bits together without considering the carry, where:

       - 0 xor 0 = 0
       - 1 xor 0 = 1
       - 0 xor 1 = 1
       - 1 xor 1 = 0 (no carry)
     - This step gives the sum of the bits without any carry.


4. **AND Operation and Left Shift:**

   - The line `Carry = (Result & Carry) << 1` performs two operations:

     1. `Result & Carry` performs a bitwise AND operation, which identifies where both bits are 1 (i.e., where a carry would occur).

     2. The result of the AND is then left-shifted (`<< 1`), meaning that the carry is moved one bit to the left to be added in the next higher bit position.

   - This operation ensures that the carry is propagated to the next bit in the next loop iteration.


5. **Update Result:**

   - The line `Result = A` assigns the current value of `A` (which now holds



PlayBASIC Code: [Select]
      //  Run Code in DEBUG MODE (f7 from IDE)
MaxTests=100

Test_Addition(MaxTests)

Sync
waitkey




/* -------------------------------------------------------------
------------>> Unsigned Integer 16bit Addition <<------------
-------------------------------------------------------------

This function performs an unsigned 16bit ADDITION
between a pair of 16 bit integers.

This version of the addition function avoids using addition
in favour of using AND and shifting to cache the Carry bits
which are exclusive or'ed between passes. We keep processing
until there's no carry left. When its expty, we have a result

------------------------------------------------------------- */



Function Add(A,B)

Result = A
Carry = B

While Carry
// Compute Carry AND, and shift overflow up
A = Result xor Carry
Carry = (Result & Carry) <<1
Result = A
EndWhile

result=A
EndFunction Result







Function Test_Addition(MaxTests)

s$=make$("-",100)
#print s$
#print "Addition Test"+make$("-",60)
#print s$
a=100
for lp= 1 to MaxTests
a=rnd($ffff)
b=rnd($ffff)

result1=Add(a,b) and $ffff
result2=(a+b) and $ffff

Status=Result1<>Result2
Failed+=Status
#print "Add:("+str$(a)+","+str$(b)+")="+str$(Result1)+" A+B="+str$(result2)+" Status="+str$(Status)
next
#print ""
#print "Result="+Str$(Failed)+" Fails"
#print s$
#print ""
#print ""

Endfunction