http://www.perlmonks.org?node_id=11688


in reply to RE: There can be only one!
in thread There can be only one!

They are bit-shift operators: They return the value of the left argument shifted left or right by the number of bits in the right argument.
2<<4 #returns 32; 32>>3 #returns 4;


vroom | Tim Vroom | vroom@cs.hope.edu

Replies are listed 'Best First'.
RE: RE: RE: There can be only one!
by Anonymous Monk on May 16, 2000 at 04:35 UTC
    could you explain this a little more in depth? i don't quite get what you mean by shifting the bytes
      Numbers represented in binary format ie: 000000101 #5 in decimal 1*2^2+1*2^0 #this shifted 1 place to the left is equivalent to #mutliplying by two 000001010 #this is equal to 10 or 1*2^3+1*2^1
      It shifts bits, not bytes. << moves the bits left and >> moves the bits right.
      2<<4 #2 is 00000010 in binary, the <<4 moves the bits left 4,00100000 + which is 32 32>>3 #32 is 00100000 in binary, the >>3 moves the bits right 3, 00000 +100 which is 4
      I hope this clears things up.
      >> and << shift bits, not bytes. Numbers are internally represented in digital computers using two-state bits. The bitshift operators shift these bits.

      For example 7 is 111 in binary. Shifting it one place left results in 1110 (digits coming from off the edge are zero), or 14. 111b shifted right once is 11 (a bit is shifted off), or 3. 7*2=14, and 7/2 is 3.5, the .5 is lost because of the one being shifted off.

      Hope this helps.

      okay, 1 = 00000001 2 = 00000010 3 = 00000011 4 = 00000100 and so on...... if you did 2<<3, you would shift 2 3 bits...... so you would have 10000, or 16.
      A byte is 8 bits. A bit is a 1 or a 0 that spells out the binary number corresponding to what you're trying to express. 2 in binary is 00000010. Shifting that to the left by 3 is this: 2 << 3 which yeilds 00010000 which is 8. shifting bits is like taking an eight bit number and rotating it in a certain direction.
      2 << 4 is 2 "left shifted" by four digits. In binary, 10.00000 is moved four digits to the left to result in 100000.0, or 32. Every time you move the decimal point in binary, you multiply or divide by two. So it's like moving the decimal point to multiply or divide by ten in decimal.
      this require some knowlege of binary numbers
      00000010 = 2
      if you shift that to the left four times
      00100000 =32