|
|
Subscribe / Log in / New account

The Grumpy Editor's Python 3 experience

The Grumpy Editor's Python 3 experience

Posted Aug 2, 2018 13:27 UTC (Thu) by virtex (subscriber, #3019)
In reply to: The Grumpy Editor's Python 3 experience by cortana
Parent article: The Grumpy Editor's Python 3 experience

When setting bits you usually don't want to just add the numbers together like you're doing because if the bit is already set the answer won't be what you want:

>>> oct(0o400 + 0o200)
'0o600'  (Looks good)
>>> oct(0o600 + 0o200)
'0o1000'  (Likely not what you wanted)
It's better to use the bitwise OR operation which will set the bit if it's unset, or leave it alone if it's already set:
>>> oct(0o400 | 0o200)
'0o600'  (Looks good)
>>> oct(0o600 | 0o200)
'0o600'  (The bit is already set, so nothing changes)


to post comments

The Grumpy Editor's Python 3 experience

Posted Aug 6, 2018 12:59 UTC (Mon) by cortana (subscriber, #24596) [Link]

You're quite right, whoops! :)


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds