w3resource

Ruby Bitwise Operators

Bitwise Operators

In Ruby, Bitwise operators allow to operate on the bitwise representation of their arguments.

What is a bit?

A bit (Binary digIT) is the basic unit of information stored in the computing system that exists in two possible states, represented as ON or OFF. In a computer system, the ON state considered as 1 and OFF state considered as 0. These states can be compared with two states of a flip-flop, two states of an electric switch etc. These two values 0 and 1 are called Binary digit and these digits are in a specific number system, that is BINARY number system which constructs upon the base of 2.

In decimal number system, a number construct upon the base of 10. Let us see how a decimal number can be constructed -

  231=(2 x 102)+(3 x 101)+(1 x 100)
      =200+30+1
      =231

The binary number system also follows the same concept. The only difference is the base is 2 instead of 10. Let us see how a binary number can be converted into a decimal number -

1011010=(1 x 26)+(0 x 25)+(1 x 24)+(1 x 23)+(0 x 22)+(1 x 21)+(0 x 20)
          =(1 x 64) +(0 x 32)+(1 x 16)+(1 x 8)+(0 x 4)+(1 x 2)+(0 x 1)
          =64+0+16+8+0+2+0
          =90

So, (1011010)2= (90)10  Byte

A byte is made up of a sequence of bits. A byte consists of eight bits. The maximum value of a byte is 255, so the place value of each bit is set.

Tabular representation of a byte

1 Byte ( 8 bits )
  8th 7th 6th 5th 4th 3rd 2nd 1st
Place value 128 64 32 16 8 4 2 1

Here is a Tabular representation of a byte shows how the maximum value of a byte is 255

1 Byte ( 8 bits )
Place Value 128 64 32 16 8 4 2 1    
  1 1 1 1 1 1 1 1    
  27 26 25 24 23 22 21 20    
  128 64 32 16 8 4 2 1 = 255

A decimal number 93 can be represent in binary form like bellow -

1 Byte ( 8 bits )
Place Value 128 64 32 16 8 4 2 1    
  0 1 0 1 1 1 0 1    
  27 26 25 24 23 22 21 20    
  0 64 0 16 8 4 0 1 = 93

Following Bitwise operators supported by Ruby language

Operator Name Example Result
& And x &y Bits that are set in both x and y are set.
| Or x | y Bits that are set in either x or y are set.
^ Xor x ^ y Bits that are set in x or y but not both are set.
~ Not ~x Bits that are set in x are not set, and vice versa.
<< Shift left x <<y Shift the bits of x, y steps to the left.#
>> Shift right x >>y Shift the bits of x, y steps to the right.*

# Each step means 'multiply by two'
* Each step means 'divide by two'

Example: Ruby bitwise operator

puts ("bitwise operators in Ruby")
a = 58      #  00111010
b = 34      #  00100010
puts ("Binary AND Operator")
puts (a&b)
puts ("Binary OR Operator")
puts (a|b)
puts ("Binary OR Operator")
puts (a^b)
puts ("Binary Ones Complement Operator")
puts (~a)
puts ("Binary Left Shift Operator")
puts (a<<4)
puts ("Binary Right Shift Operator")
puts (a>>4)

Output:

bitwise operators in Ruby
Binary AND Operator
34
Binary OR Operator
58
Binary OR Operator
24
Binary Ones Complement Operator
-59
Binary Left Shift Operator
928
Binary Right Shift Operator
3

Previous: Ruby Parallel Assignment
Next: Ruby Logical Operators



Follow us on Facebook and Twitter for latest update.