如何使用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}"
,並得到它:
預先感謝您..
你的問題是什麼? – sawa
如何將rgb值轉換爲整數二進制形式?我嘗試將rgb值轉換爲二進制形式,如下所示:binRed = currentRed.to_s(2) \t binGreen = currentGreen.to_s(2) \t binBlue = currentBlue.to_s(2)但我得到了與在printscreen中相同的結果上面..因爲它們仍然是字符串.. – Alexandre