News:

Function Finder  Find all the functions within source code files

Main Menu

Binary Multipy (Binary opp'ds)

Started by kevin, May 24, 2003, 04:06:21 PM

Previous topic - Next topic

kevin


This PlayBASIC example / theory function shows how integer multiplies actually work.  The multiply is only 16bit ..  So it can mult two 16bit values and get a 32bit result, without overflow.  

 I only posted this there was chat how shifts (those << and >> things) could be used to mult values..


PlayBASIC Code: [Select]
do
A=rnd(64000)
B=rnd(64000)

print A*B
print BinaryMult16(A,B)

inc c
if c >10 then c=0: cls 0
sync
loop


Function BinaryMult16(A,B)
Result=0
Shiftoffset =1

for Bitlp=0 to 15
if (ShiftOffset and B)<>0
result=result+(A << bitlp)
endif
ShiftOffset=ShiftOffset << 1
next bitlp
EndFunction Result






BinaryMoon

What's that written in? DBP?

Are the << and >> the same as shr and shl in Blitz (I have a feeling they are). I have never understodd these things although I use them to get inividual colour values from hex(?) colours. eg



rcol=(pix Shr 16) And $FF
gcol=(pix Shr 8) And $FF
bcol=pix And $FF

Ben aka Mop

BinaryMoon
BinarySun

kevin

#2
Yeah it's Dbpro.. The << and >> are bit shift left and right. Same as shr, shl in BB

Shifting as well as bitwise and/or/xor are binary (base 2) level operations.. it's prolly easier to see what they do by looking at them like this



` Shift Left..
v=rnd(65535)
  print "Orig Value:";bin$(v);"  ";v
   for Bitlp=0 to 15
        result=(V << bitlp)
      print bin$(result);"  ";result
  next bitlp
wait key