Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Bitwise right shift operator (>>)
2.1.
Syntax
2.2.
Parameters
2.3.
Return value
2.4.
Example
2.5.
Program
2.5.1.
Output
3.
Bitwise left shift operator (<<)
3.1.
Syntax
3.2.
Parameters
3.3.
Return value
3.4.
Example
3.5.
Program
3.5.1.
Output
4.
Frequently Asked Questions
4.1.
What does Ruby's |= stand for?
4.2.
In Ruby, what is a lambda?
4.3.
In Ruby, what is self?
4.4.
In Ruby, what is yield?
5.
Conclusion
Last Updated: Mar 27, 2024

Shift and Append: << and >> operators in Ruby

Author SHIVANGI MALL
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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 serversDevOps, 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
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Bitwise left shift operator (<<)

The bitwise left shift operator in Ruby shifts each bit of an integer by n position to the left.

As a general convention, << in Ruby means "append" which implies it appends its argument to its receiver before returning the receiver. So, for Array, it appends the argument to the array, and for string, it concatenates strings.

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 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 7 << 2 is 28.

# For integers it means bitwise left shift: 

5 << 1 # gives 10

17 << 3 # gives 136

 

#For arrays and strings, it means append: 

"hello, " << "world" # gives "hello, 

world" [1, 2, 3] << 4 # gives [1, 2, 3, 4]

 

Let’s make this operation in Ruby

Program

# shift some bits of numbers
puts 5 << 1
puts 7 << 2
puts 8 << 2
puts 5 << 3

Output

10
28
32
40

 

Read about Bitwise Operators in C here.

Frequently Asked Questions

What does Ruby's |= stand for?

It's short for or-equals-to. It first checks whether the value on the left is defined, after which it uses it. If it isn't, use the right-hand value.

In Ruby, what is a lambda?

A lambda is a Ruby object that is comparable to a proc. A lambda, unlike a proc, requires a fixed number of arguments to be supplied to it, and instead of returning immediately, it returns s to its calling procedure.

In Ruby, what is self?

self is a unique variable that refers to the object that "owns" the code that is now running.

In Ruby, what is yield?

Yield is a Ruby keyword that allows a developer to give an argument to a block. The amount of arguments that can be passed to the block is unlimited.

Conclusion

This article extensively discussed Shift and Append i.e., << and >> operators in Ruby.

We hope this article helps you to learn something new. And if you're interested in learning more, see our posts on 8 reasons why Ruby should be your first language!RubyIntroduction to Ruby on RailsRuby on rails for your next web development project!Ruby on Rails.

Visit our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Thank you for reading this post :-)

Feel free to upvote and share this article if it has been useful for you.

 

Previous article
Arithmetic Operations in Ruby
Next article
Bitwise NOT(~), AND(&), OR(|) and XOR(^) Operations in Ruby
Live masterclass