Oxygen Basic
Programming => Problems & Solutions => Topic started by: Mike Lobanovsky on October 14, 2014, 06:10:53 PM
-
Charles,
I know you can offer solutions that would be a lot better and more elegant and faster and BASIC-ish but I still need to know why this elementary and seemingly obvious strategy fails:
function binary_decode(char* s) as quad
quad x = 0
byte* p = @s
while (*p != 0) // O2 logic isn't short-circuited!
if (*p == 0x31 || *p == 0x30) then // '1' '0'
x *= 2 // ugly but no shifts available for O2 quads!
x += *p - 0x30 // '0'
p++
else
exit while
end if
wend
return x
end function
print binary_decode("1")
What am I doing wrong again?! :-\
-
Mike,
Once a variable is declared, its pointish nature becomes implicit
function binary_decode(char* s) as quad
quad x = 0
byte p at @s
'
while (p != 0) // O2 logic isn't short-circuited!
if (p == 0x31 || p == 0x30) then // '1' '0'
x *= 2 // ugly but no shifts available for O2 quads!
x += p - 0x30 // '0'
@p++ 'increment address
else
exit while
end if
wend
return x
end function
print binary_decode("1")
PS: binary 0b, and Octal 0o conversions are available
s="1001"
print val "0b" & s
'9
-
Thanks Charles,
You've brought me back to life. :)
I'm leaving this
s="1001"
print val "0b" & s
'9
out till the time when you take over and rationalize the C-ish original for 64 bits.