Stonemonkey wrote:@Albert

for divide by 2 you shift right by 1, bit 0 of the high byte gets shifted into bit 7 of the low byte.

Albert, you are getting ahead of yourself. Check that it works first.

Code: Select all

`function halfer( byref src as string, byref carry as ubyte = 0 ) as string`

dim as string answer = string(len(src),0)

for i as integer = 0 to len( src ) - 1

answer[i] = (src[i] shr 1) + (carry and 1) shl 7

carry = src[i] and 1

next

return answer

end function

function doubler( byref src as string, byref carry as ubyte = 0 ) as string

dim as string answer = string(len(src),0)

for i as integer = len( src ) - 1 to 0 step -1

answer[i] = (src[i] shl 1) + (carry and 1)

carry = src[i] shr 7

next

return answer

end function

function isZero( byref src as const string ) as boolean

for i as integer = 0 to len( src ) - 1

if( src[i] ) then

return false

end if

next

return true

end function

function strToBin( byref src as const string ) as string

dim as string result = ""

for i as integer = 0 to len( src ) - 1

result &= bin( src[i], 8 )

next

return result

end function

dim bits as string = chr( 3, 255, 7, 99 )

dim point_5 as string = ""

print "step bits 'point_5' "

print "---- -------------------------------- . --------------------------------"

dim count as integer = 0

'' halfing

do

print using "####"; count;

print " " & strToBin(bits) & " . " & point_5

'' quit if zero

if( isZero( bits ) ) then

exit do

end if

'' right most bit becomes the remainder

if( bits[len(bits)-1] and 1 ) then

point_5 = "1" + point_5

else

point_5 = "0" + point_5

end if

'' divide by 2

bits = halfer( bits )

count += 1

loop

print "---- -------------------------------- . --------------------------------"

'' doubling

do

print using "####"; count;

print " " & strToBin(bits) & " . " & point_5

count -= 1

'' quit if no more point_5

if( len(point_5) = 0 ) then

exit do

end if

'' divide by 2

if( left(point_5, 1) = "1" ) then

bits = doubler( bits, 1 )

else

bits = doubler( bits, 0 )

end if

'' remove a bit from point_5

point_5 = mid( point_5, 2 )

loop

sleep

There's lots of people here still willing to help. Maybe they will help explain. Sometimes hard to know what it is you are doing. Mixing strings of numbers and bits and characters. It's kind of all the same thing.