Human Dunnil
5/12/2007 11:21:00 AM
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.)
>
>