Introduction
Ruby is an open-source, object-oriented scripting language. Yukihiro Matsumoto created Ruby in the mid-1990s. Ruby is an excellent language for making desktop programs, static webpages, data processing services, and even automation solutions. Web servers, DevOps, and web scraping and crawling are all examples of where it's employed. When you bring in the Rails application framework's features, you can accomplish even more, especially with database-driven web apps.
This article will provide you with sufficient knowledge of Shift and Append, i.e., << and >> operators in Ruby, to enable you to go to higher levels of expertise. Before reading this article, you should have a basic understanding of computer programming terms and ruby. If this isn't the case, come back here after reading our Ruby article.
Bitwise right shift operator (>>)
The operator >>, often known as the Bitwise right shift operator, is used to shift the bits of a number by n places to the right.
Syntax
number >> shifts
Parameters
number: The number whose bits we wish to change is this one.
Shifts: number of positions to shift the bits of a number to the right.
Return value
An integer is returned as a result. It's the same as a number that's been shifted right by shifts.
Example
So the result of the operation 40 >> 2 is 10.
Let’s make this operation in Ruby
Program
# shift some bits of numbers
puts 300 >> 1
puts 60 >> 2
puts 12 >> 2
puts 5 >> 3
puts 4 >> 2
Output
150
15
3
0
1