[Gambas-user] binary numeral system

Ron_1st ronstk at ...239...
Wed Apr 15 02:53:59 CEST 2009


On Wednesday 15 April 2009, Jeff Gray wrote:
> 
> That's pretty cool.
> 
> My only comment would be that it took me a few goes at stepping through the 
> code before I worked out how the blazes it worked, so perhaps less readable 
> than the exponent version.  
> 
> But hey - I'm no rocket scientist :-)

I'm just a ordinary Dutch cheese eating man on wooden shoes :)

>  
> > From: ronstk at ...239...
> > 
> > 
> > 
> > function bin2dec(sBin) as long
> > dim r as long
> > r=0
> > for i= 1 to len(sBin)
> > r = r*2 + Val(Mid(sBin, i, 1)) 
> > next
> > return r
> > end

You did forget the remainder.

> for 10011 as sBin the sequence is as follows:
> r*2=0  +1 = 1
> r*2=2  +0 = 2
> r*2=4  +0 = 4
> r*2=8  +1 = 9
> r*2=18 +1 =19

That was showing the way it works.
  r is your value to return
  +0/+1 are your bits in the string
  and last is the value computed each for/next loop.



Best regards,

Ron_1st





More information about the User mailing list