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}"
そしてそれを得る:
前もって感謝します..