如何使用 Ruby 对整数值进行二进制左移?
我正在尝试进行左移二进制操作,但我得到一个奇怪的字符而不是移动..
我认为它应该像这样执行:(java)
b = (b >> 2); //0011 1111
b = (b << 2); //1111 1100
我在红宝石中这样做:
currentRed = ChunkyPNG::Color.r(image[x,y])
currentGreen = ChunkyPNG::Color.g(image[x,y])
currentBlue = ChunkyPNG::Color.b(image[x,y])
binRed = currentRed.to_s.unpack("b*")[0]
binGreen = currentGreen.to_s.unpack("b*")[0]
binBlue = currentBlue.to_s.unpack("b*")[0]
puts "original"
puts "r #{binRed}"
puts "g #{binGreen}"
puts "b #{binBlue}"
puts "------"
binRed = binRed << 2
binGreen = binGreen << 2
binBlue = binBlue << 2
puts "new"
puts "r #{binRed}"
puts "g #{binGreen}"
puts "b #{binBlue}"
并得到它:
先感谢您..