Author Topic: [SOLVED] Pointers: Losing My Heart Again  (Read 1778 times)

0 Members and 1 Guest are viewing this topic.

Mike Lobanovsky

  • Guest
[SOLVED] Pointers: Losing My Heart Again
« 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:

Code: [Select]
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?!  :-\
« Last Edit: October 14, 2014, 09:14:16 PM by Mike Lobanovsky »

Charles Pegge

  • Guest
Re: Pointers: Losing My Heart Again
« Reply #1 on: October 14, 2014, 07:54:18 PM »
Mike,

Once a variable is declared, its pointish nature becomes implicit

Code: [Select]
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
« Last Edit: October 14, 2014, 08:08:13 PM by Charles Pegge »

Mike Lobanovsky

  • Guest
[SOLVED] Re: Pointers: Losing My Heart Again
« Reply #2 on: October 14, 2014, 09:13:30 PM »
Thanks Charles,

You've brought me back to life. :)

I'm leaving this
Quote
s="1001"
print val "0b" & s
'9
out till the time when you take over and rationalize the C-ish original for 64 bits.