12/08/2018, 14:19

Playing With Bits in Ruby

Bitwise operations were quite an important issue at the time of programming in C. Mostly it was a cool stuff where one can play with basics of computer - Binary numbers. But working in high-level languages like ruby, one seldom use bitwise operations in their work. Sometimes, i accidentally ...

bits_viblo.jpg

Bitwise operations were quite an important issue at the time of programming in C. Mostly it was a cool stuff where one can play with basics of computer - Binary numbers. But working in high-level languages like ruby, one seldom use bitwise operations in their work. Sometimes, i accidentally used '&' where i intended to type '&&' in my work. In fact, it is one of the few times when i use bitwise operators now-a-days. So let’s talk about Ruby bitwise operations.

Binary Number

I guess you already have understood that in case of demonstrating bitwise, the first thing one should learn is binary numbers as it makes demonstrating how bitwise operation works much easier. To convert an integer to a string of 0's and 1's, we can simply use Fixnum#to_s passing 2 as the argument.

10.to_s(2)
=> "1010"

Bitwise operations

Bitwise operators are more or less almost same in every programming language. Here are the bitwise operators in ruby -

  1. AND(&) operator: If the bits at the same position in both integers are 1 the resulting integer will have the corresponding bit set to 1. If not, the bit will be set to 0.
b = 9
=> 9  #binary string"1001"
a = 19
=> 19  #binary string"10011"
(a & b).to_s(2)
=> "1"
  1. OR(|) operator: If the bits at the same position in any one or both of integers are 1 the resulting integer will have the corresponding bit set to 1. If not, the bit will be set to 0.
b = 9
=> 9  #binary string"1001"
a = 19
=> 19 #binary string"10011"
(a | b).to_s(2)
=> "11011"
  1. Exclusive OR(^) operator: If the bits at the same position in any one of integers are 1 the resulting integer will have the corresponding bit set to 1. If not, the bit will be set to 0.
b = 9
=> 9 #binary string"1001"
a = 19
=> 19 #binary string"10011"
(a ^ b).to_s(2)
=> "11010"
  1. NOT(~) operator: This operator inverts the bits of binary representation of an integer. So if there is 0 in any position, not operator converts them to 1 and vice versa.
b = 9
=> 9 #binary string"1001"
(~b).to_s(2)
=> "-1010"

Question may arise that numbers are not flipped as said and how minus sign come. To understand this you have to understand 2's complement in mathematics. You can get information from here.

  1. Shift left(<<) operator: Actually this operator only "shift" or moves all the bits to the left by a specified number and adds 0 in last.
a = 19
=> 19 #binary string"10011"
(a << 1)
=> 38 #binary string "100110"
(a << 2)
=> 76 #binary string "1001100"

Do you find any pattern? ya every left shift actually double the given number. It is actually faster than widely used multiplication symbol(*).

  1. Shift right(<<) operator: Actually this operator only "shift" or moves all the bits to the right by a specified number and adds 0 in front.
a = 28
=> 28 #binary string"11100"
(a >> 1)
=> 14 #binary string "01110"
(a >> 2)
=> 7 #binary string "0111"

Again Do you find any pattern? ya every right shift actually halves the given number. It is actually faster than widely used division symbol(/).

Bit manipulation is an useful technique for performance increase. Technically they are not so useful in Ruby. But learning them and using them is fun and someday may be useful in future.

ha5

0