2

What are >> and << for?

I read this in code:

https://github.com/mlaiosa/emlx2maildir/blob/master/emlx2maildir.py

FL_READ = (1<<0)
FL_DELETED = (1<<1)
FL_ANSWERED = (1<<2)
FL_ENCRYPTED = (1<<3)
FL_FLAGGED = (1<<4)
FL_RECENT = (1<<5)
FL_DRAFT = (1<<6)
FL_INITIAL = (1<<7)
FL_FORWARDED = (1<<8)
FL_REDIRECTED = (1<<9)
FL_SIGNED = (1<<23)
FL_IS_JUNK = (1<<24)
FL_IS_NOT_JUNK = (1<<25)
FL_JUNK_LEVEL_RECORDED = (1<<29)
FL_HIGHLIGHT_IN_TOC = (1<<30)

I cannot find the documentation of it yet.

2
  • Same as in any other language, really. Commented Nov 24, 2011 at 7:07
  • Sorry i am not used to bit shifting. Not in high level languages. Commented Nov 24, 2011 at 7:26

3 Answers 3

4

It's bitshift operator. If you have a 1(0b1), and shift it left 4 bits(1 << 4), what you get is 0b10000, which means 16.

And here's the documentation: http://docs.python.org/reference/expressions.html#shifting-operations

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot , is that still useful in high level langauge as python? What are their PRACTICAL uses?
I'm not sure about it's performance advantages, but I know one practical use of them: setting flags(like in the code you posted). For example, you can use 16bit integer to hold 16 flags with this: first, you assign a flag 1, then you assign second flag 1 << 1, third flag 1 << 2 etc. And when checking this flags, and when you checking for a flag you use flag & flag_name, if it yields 1, you have the flag, 0, you don't.
Thanks quite confusing but i will look more into it.
V3ss0n, I think this could be a good start en.wikipedia.org/wiki/Bitwise_operation
Sometimes it's relevant when you have to deal with data that was originally prepared for use in a lower-level language, that encodes values that way.
|
2

The operators are defined in section 5.7, "Shifting Operations", of the Python Language Reference:

These operators accept plain or long integers as arguments. The arguments are converted to a common type. They shift the first argument to the left or right by the number of bits given by the second argument.

A right shift by n bits is defined as division by pow(2, n). A left shift by n bits is defined as multiplication with pow(2, n). Negative shift counts raise a ValueError exception.

Comments

1

In most languages, including Python, those are shift-operators. They work on the bits of a byte.

For example, 8 is 0b00001000. 8 >> 1 means shift the bits of it 1 digit to the right, adding a zero at the left (0b00000100 or 4). 8 >> 2 means shift it to the right twice. (0b00000010 or 2). The << is a left shift, which works in the opposite way. 8 << 1 would come out to 0b00010000 or 16. 8 << 2 would come out to 0b00100000 or 32.

See the python documentation for more information.
Python 2.x: http://docs.python.org/reference/expressions.html#shifting-operations
Python 3.x: http://docs.python.org/py3k/reference/expressions.html#shifting-operations

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.