[Tutor] << operator ?

Kalle Svensson kalle@gnupung.net
Wed, 10 Apr 2002 23:06:46 +0200


[Prahlad Vaidyanathan]
> What does this "<<" operator do ? IIRC, it is a bit-wise shift operator,
> but I don't know what that means either :-(

Consider a number, say 4711.  It is represented in the computer as the
binary number 00000000 00000000 00010010 01100111.

When shifted one step to the left (<< 1), this becomes
00000000 00000000 00100100 11001110, or 9422.  This is, as you can
see, twice the original number.

4711 << 8 is 00000000 00010010 01100111 00000000, 1206016, 256 * 4711.

The >> operator works in the same way, but a different direction:
4711 >> 8 is 00000000 00000000 00000000 00010010, 18, 4711 / 256.

Any clearer?

Med utmärkt högaktning,
  Kalle
-- 
Kalle Svensson (kalle@gnupung.net) - Laziness, impatience, hubris: Pick two!
English: http://www.gnupung.net/  Svenska: http://www.lysator.liu.se/~kalle/
Stuff: ["http://www.%s.org/" % x for x in "gnu debian python emacs".split()]