in reply to Understanding Shift Operators

then aren't the only valid arguments for the left side 0 and 1?
No.
Does it convert the decimal number 2 to binary and then shift each bit?
Yes, that is one way to look at it.

Replies are listed 'Best First'.
Re^2: Understanding Shift Operators
by 7stud (Deacon) on Apr 06, 2010 at 21:03 UTC
    What other way is there to look at it?

      You could view every register and memory address as already containing a collection of bits.

      Binary and decimal are representations, a way of displaying something. The computer does not need to represent the bits in a human readable way (e.g. binary) to operate on them. No conversion occurs, because the binary representation of a scalar describes what's already in the scalar.

      What other way is there to look at it?
      Another way to look at the left-shift operation is a mulitiply-by-power-of-2 operation. $num << $power is the same as $num * 2**$power:
      use strict; use warnings; foo(1, 2); foo(1, 3); foo(5, 2); foo(7, 3); sub foo { my ($n, $p) = @_; print "$n << $p = ", $n << $p , "\n"; print "$n * 2**$p = ", $n * 2**$p, "\n\n"; } __END__ 1 << 2 = 4 1 * 2**2 = 4 1 << 3 = 8 1 * 2**3 = 8 5 << 2 = 20 5 * 2**2 = 20 7 << 3 = 56 7 * 2**3 = 56

        Toolic -

        That's cool - thanks.

      You know, the people who give negative reputation to people who ask questions should be banned permanently from this site. Here is a news flash: this is a discussion forum. If that offends you, then go elsewhere.

      By the way, I welcome all negative reputation. In fact, if I get to negative 100,000 by next week, I will be very happy. If you think giving me negative reputation is going to dissuade me from asking questions, then you are an imbecile.

        Disclaimer: I didn't vote on your post, nor would I downvote it if I had votes left today (nor upvote it, as it was really pretty banal).

        Here is a news flash: votes are at the voter's discretion. There's already a system in place to discourage downvoting. Throwing a tantrum isn't it. If that offends you, perhaps you should go elsewhere. Or, enjoy your negative reputation--which doesn't prevent posting, but it does suggest that the community isn't especially interested in your input into its norms, now, doesn't it?