[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Human Dunnil

5/12/2007 9:22:00 AM

Hello folks,

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

I was looking at one of the Ruby Quiz 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 ? 0
if (b and 1) ? 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

8 Answers

Sebastian Hungerecker

5/12/2007 9:37:00 AM

0

Human Dunnil wrote:
> I was looking at one of the Ruby Quiz 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.


--
Ist so, weil ist so
Bleibt so, weil war so

Enrique Comba Riepenhausen

5/12/2007 9:45:00 AM

0


On 12 May 2007, at 11:37, Sebastian Hungerecker wrote:

> Human Dunnil wrote:
>> I was looking at one of the Ruby Quiz 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 Riepenhausen

John Joyce

5/12/2007 9:55:00 AM

0

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.)

Human Dunnil

5/12/2007 11:21:00 AM

0

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 Quiz 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,

On 5/12/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
> 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.)
>
>

dblack

5/12/2007 11:32:00 AM

0

Sebastian Hungerecker

5/12/2007 11:36:00 AM

0

Human Dunnil 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.


--
NP: Kataklysm - Blood On The Swans
Ist so, weil ist so
Bleibt so, weil war so

Rick DeNatale

5/12/2007 2:57:00 PM

0

On 5/12/07, Sebastian Hungerecker <sepp2k@googlemail.com> wrote:
> Human Dunnil 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.

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.denh...

Rick DeNatale

5/12/2007 3:07:00 PM

0

On 5/12/07, John Joyce <dangerwillrobinsondanger@gmail.com> 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.denh...