Shift Operators - I don't get it? What's the application?!?

Hello folks,

Sorry for dump question, but I really don’t get it!

I was looking at one of the Ruby Q. solutions (Countdown) in Ruby
Quiz book, it uses << operator, which I don’t understand!

I checked WIkipedia and Ruby Doc, I know what it does, but can’t
understand how it is useful and what’s its application?

BTW, I saw a pseudo code in Wikipedia but can’t translate it to ruby:

c := 0
while b e$B!be(B 0
if (b and 1) e$B!be(B 0
c := c + a
shift a left by one
shift b right by one
return c

May you please give me a clue about it?

Thanks in advance,

  • Dunnil

Human D. wrote:

I was looking at one of the Ruby Q. solutions (Countdown) in Ruby
Quiz book, it uses << operator, which I don’t understand!

If you’re looking at the same code as I am, the << isn’t used as the
shift
operator. It’s used to add items to an array. << only acts as shift
operator
on numbers.

On 12 May 2007, at 11:37, Sebastian H. wrote:

Human D. wrote:

I was looking at one of the Ruby Q. solutions (Countdown) in Ruby
Quiz book, it uses << operator, which I don’t understand!

If you’re looking at the same code as I am, the << isn’t used as
the shift
operator. It’s used to add items to an array. << only acts as shift
operator
on numbers.

The << operator has many different uses:

  • In an Array it is used to append (push) an object into a given array
  • In a Fixnum and a Bignum object (i.e integers) it shifts the number
    bits to the left
  • In an IO object it will write the object (as a string) to the IO
    object.
  • In a String it will append the object (to_s) to the String (if the
    object is a Fixnum between 0 and 255 it will convert it to the
    character representation before appending it)

I hope this helps…

Cheers,

Enrique Comba R.

It’s not what you think. Not always.
It’s not a bit shift operator all the time.
<<
is a bit different in different contexts in Ruby.
You can look it up with the tool ri
ri ‘<<’
That will give you a list of different classes that use <<
In the Array class for example, << pushes an object on the right side
onto the end of the array on the left side.
an_array << object_to_push_onto_end

In the class BigNum and the class FixNum, << is a left shift bit
shift operator.
Bit shifting isn’t often done in Ruby, but certainly possible. It has
various uses and is often used for speed tricks in C, because bit
shifting is some times faster than some math operations. It
litterally takes a binary number and moves all the 1’s and zeros to
the left. (to the right with >>)

With class IO (and its subclasses, such as File)
<< writes the object on the right to the IO object on the left. It
also converts the object on the right to a string first.

Class String uses << to append the object on the right to the string
object on the left. Conversion to string first will happen.

This may actually be a weakness of Ruby, maybe not. (could be a
contentious issue) but much like in natural languages, context makes
it pretty clear that something different is happening.

If you really want a tutorial on how bit-shifting works, there should
be a few good ones out there on the web, or maybe somebody else here
will do it. I always hated bit-shifting and bit-filters. (Dan
Gookin’s, C All-in-one Desk Reference for Dummies has a good bitwise
operator chapter.)

Hi –

On Sat, 12 May 2007, Human D. wrote:

Thanks for the info, I knew << method is implemented by different
classes for different tasks. I really need info on bitwise operator.

If you look at Ruby Q. book (Page 240), you see:

@num_sources = sources.size
@num_hashes = 1 << @num_sources

I think << here is doing bit manipulation, which I do not get!

It’s got to do with binary arithmetic. For example, let’s say you do:

12 << 2

In binary notation, 12 is 1100. If you shift that to the left by 2
(i.e., << 2), you get 110000, which is 48:

12 << 2
=> 48

David

Thanks for the info, I knew << method is implemented by different
classes for different tasks. I really need info on bitwise operator.

If you look at Ruby Q. book (Page 240), you see:

@num_sources = sources.size
@num_hashes = 1 << @num_sources

I think << here is doing bit manipulation, which I do not get!

Thanks guys,

Human D. wrote:

@num_sources = sources.size
@num_hashes = 1 << @num_sources

I think << here is doing bit manipulation

It is.

which I do not get!

1<<x is the same as 2**x. If you think about it for a bit or try to
solve 1<<x
with pencil and paper, it’ll become apparent why.

On 5/12/07, Sebastian H. [email protected] wrote:

1<<x is the same as 2**x. If you think about it for a bit or try to solve 1<<x
with pencil and paper, it’ll become apparent why.

Or more generally

if x is a Fixnum or Bignum then

x << i

is the same as
x * 2**i


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On 5/12/07, John J. [email protected] wrote:

It’s not what you think. Not always.

In the class BigNum and the class FixNum, << is a left shift bit
shift operator.

With class IO (and its subclasses, such as File)
<< writes the object on the right to the IO object on the left. It
also converts the object on the right to a string first.

Class String uses << to append the object on the right to the string
object on the left. Conversion to string first will happen.

and Date#<<(n) produces a date n months earlier than the receiver.

This may actually be a weakness of Ruby, maybe not. (could be a
contentious issue) but much like in natural languages, context makes
it pretty clear that something different is happening.

I think that this analogy with natural languages goes hand in hand
with what makes Ruby seem natural to some of us, which actually makes
it a strength.

It’s also a nice counter-example for those who try to ‘tighten-up’
duck-typing by using respond_to?, it’s not just the name but semantics
that matter. Again to me this is a strength, others will no doubt see
it differently. It’s really no different than the cases which
strongly typed languages fail to catch such as sqrt(-1) in a language
which doesn’t support complex numbers.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/