Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DWord operations in Ruby
- puts [255,1,255,2].map{|val| val.to_s(2).rjust(8, '0')}.join(' ')
- OR => 1 | 2 = 3
- AND => 1 & 2 = 0
- XOR => 1 ^ 3 = 2
- class Byte
- attr_accessor :value
- def initialize(integer)
- @value = integer
- end
- def to_s
- value.to_s(2).rjust(8,"0")
- end
- end
- class DWord
- attr_accessor :bytes
- def initialize(*byte_list)
- @bytes = []
- byte_list.each do |b|
- @bytes << Byte.new(b)
- end
- end
- def to_s
- @bytes.map(&:to_s).join(' ')
- end
- end
- dword = DWord.new(255,1,255,2)
- puts dword
- # 11111111 00000001 11111111 00000010
- dword.bytes.each do |b|
- puts "#{b.value} = #{b}"
- end
- # 255 = 11111111
- # 1 = 00000001
- # 255 = 11111111
- # 2 = 00000010
Add Comment
Please, Sign In to add comment