Advertisement
Guest User

2sc

a guest
Jan 15th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.43 KB | None | 0 0
  1. #!/usr/bin/ruby -w                                                                
  2.  
  3. def twos_compliment(integer)
  4.   if integer > 0
  5.     return "0" + integer.to_s(2)
  6.   else
  7.     integer = ~integer + 1                                                        
  8.     return integer.to_s(2)
  9.   end
  10. end
  11.  
  12. arr = [1, -2, 3, -4, -3, 2]
  13. arr.each { | item |
  14.   printf("%s %s %s \n", item.to_s(10), item.to_s(2), twos_compliment(item))        
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement