I notice that I can do things like 2 << 5
to get 64 and 1000 >> 2
to get 250.
Also I can use >>
in print
:
print >>obj, "Hello world"
What is happening here?
I notice that I can do things like 2 << 5
to get 64 and 1000 >> 2
to get 250.
Also I can use >>
in print
:
print >>obj, "Hello world"
What is happening here?
The other case involving
print >>obj, "Hello World"
is the "print chevron" syntax for theprint
statement in Python 2 (removed in Python 3, replaced by thefile
argument of theprint()
function). Instead of writing to standard output, the output is passed to theobj.write()
method. A typical example would be file objects having awrite()
method. See the answer to a more recent question: Double greater-than sign in Python.Actual binary value of 12 is "00 1100" when we execute the above statement Left shift ( 2 places shifted left) returns the value 48 its binary value is "11 0000".
The binary value of 48 is "11 0000", after executing above statement Right shift ( 2 places shifted right) returns the value 12 its binary value is "00 1100".
These are the shift operators
I think it is important question and it is not answered yet (the OP seems to already know about shift operators). Let me try to answer, the >> operator in your example is used for two different purposes. In c++ terms this operator is overloaded. In the first example it is used as bitwise operator (left shift), while in the second scenario it is merely used as output redirection. i.e.
example
These are bitwise shift operators.
Quoting from the docs:
Returns
x
with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplyingx
by2**y
.Returns
x
with the bits shifted to the right by y places. This is the same as dividingx
by2**y
.