Guest User

Untitled

a guest
Aug 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. DWord operations in Ruby
  2. puts [255,1,255,2].map{|val| val.to_s(2).rjust(8, '0')}.join(' ')
  3.  
  4. OR => 1 | 2 = 3
  5. AND => 1 & 2 = 0
  6. XOR => 1 ^ 3 = 2
  7.  
  8. class Byte
  9. attr_accessor :value
  10.  
  11. def initialize(integer)
  12. @value = integer
  13. end
  14.  
  15. def to_s
  16. value.to_s(2).rjust(8,"0")
  17. end
  18. end
  19.  
  20. class DWord
  21. attr_accessor :bytes
  22.  
  23. def initialize(*byte_list)
  24. @bytes = []
  25. byte_list.each do |b|
  26. @bytes << Byte.new(b)
  27. end
  28. end
  29.  
  30. def to_s
  31. @bytes.map(&:to_s).join(' ')
  32. end
  33. end
  34.  
  35. dword = DWord.new(255,1,255,2)
  36.  
  37. puts dword
  38.  
  39. # 11111111 00000001 11111111 00000010
  40.  
  41. dword.bytes.each do |b|
  42. puts "#{b.value} = #{b}"
  43. end
  44.  
  45. # 255 = 11111111
  46. # 1 = 00000001
  47. # 255 = 11111111
  48. # 2 = 00000010
Add Comment
Please, Sign In to add comment