Yagiz Yagiz - 7 months ago 66
Ruby Question

-2 base conversion algorithm suggestion

I am trying to convert an integer to base -2, but in Ruby

doesn't get accepted. Do you have any other solutions to convert a number to -2 base?


Adapted from Mark Dickinson's comment:

def negabinary(n)
  return negabinary(-(n>>1)) + (n & 1).to_s if n != 0
  return ''