Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Understanding Shift Operators

by spickles (Scribe)
on Apr 06, 2010 at 20:44 UTC ( [id://833118]=perlquestion: print w/replies, xml ) Need Help??

spickles has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to understand someone else's code, and I was very curious as to what the binary shift operator does. So I looked it up on perldoc.perl.org and found this definition:

Binary "<<" returns the value of its left argument shifted left by the number of bits specified by the right argument. Arguments should be integers. (See also "Integer Arithmetic".)

Seems rather straight forward, so I created the following code to test:

#!c:/perl/bin/perl use strict; use warnings; print 1 << 1; print "\n"; print 1 << 2; print "\n"; print 1 << 3; print "\n"; print 1 << 4; print "\n"; print 1 << 5; print "\n"; print 1 << 6; print "\n"; print 1 << 7; print "\n"; print 1 << 8; print "\n"; print 1 << 9; print "\n"; print 1 << 10; print "\n"; print 2 << 1; print "\n"; print 2 << 2; print "\n"; print 2 << 3;

Everything made sense to me until I started using the last few beginning with print 2 << 1. I was surprised that it compiled without error and produced what I am assuming to be valid output. If the shift operator is used with binary and it shifts the argument on the left by the number of bits specified by the argument on the right, then aren't the only valid arguments for the left side 0 and 1? How does it produce output with numbers 2 and up? Does it convert the decimal number 2 to binary and then shift each bit?

Replies are listed 'Best First'.
Re: Understanding Shift Operators
by FunkyMonk (Chancellor) on Apr 06, 2010 at 20:52 UTC
    Does it convert the decimal number 2 to binary and then shift each bit?
    Yes. Consider:
    my $num = 22 # 0b00010110 print $num << 2; # 0b01011000

    $num << 2 has two zeros added to the right, and two removed from the left. The binary pattern has been shifted two bits to the left.

    Update

    You may find it helpful to play with the following

    perl -wE 'printf "%#010b", 33 # 0b00100001'

    (The documentation for printf's formatting is described in sprintf)

Re: Understanding Shift Operators
by roboticus (Chancellor) on Apr 06, 2010 at 20:51 UTC

    spickles:

    It's taking the integer on the left and shifting the bit pattern. Thus, 5<<1 == 10.

Re: Understanding Shift Operators
by toolic (Bishop) on Apr 06, 2010 at 20:53 UTC
    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.
      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

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://833118]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-24 22:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found