Skip to content

Instantly share code, notes, and snippets.

/ruby.rb Secret

Created January 15, 2016 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0f14f4c273b66ae2edaf to your computer and use it in GitHub Desktop.
Save anonymous/0f14f4c273b66ae2edaf to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby -w
def twos_compliment(integer)
if integer > 0
return "0" + integer.to_s(2)
else
integer = ~integer + 1
return integer.to_s(2)
end
end
arr = [1, -2, 3, -4, -3, 2]
arr.each { | item |
printf("%s %s %s \n", item.to_s(10), item.to_s(2), twos_compliment(item))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment