this post was submitted on 09 Mar 2025
18 points (87.5% liked)
Python
6788 readers
41 users here now
Welcome to the Python community on the programming.dev Lemmy instance!
📅 Events
Past
November 2023
- PyCon Ireland 2023, 11-12th
- PyData Tel Aviv 2023 14th
October 2023
- PyConES Canarias 2023, 6-8th
- DjangoCon US 2023, 16-20th (!django 💬)
July 2023
- PyDelhi Meetup, 2nd
- PyCon Israel, 4-5th
- DFW Pythoneers, 6th
- Django Girls Abraka, 6-7th
- SciPy 2023 10-16th, Austin
- IndyPy, 11th
- Leipzig Python User Group, 11th
- Austin Python, 12th
- EuroPython 2023, 17-23rd
- Austin Python: Evening of Coding, 18th
- PyHEP.dev 2023 - "Python in HEP" Developer's Workshop, 25th
August 2023
- PyLadies Dublin, 15th
- EuroSciPy 2023, 14-18th
September 2023
- PyData Amsterdam, 14-16th
- PyCon UK, 22nd - 25th
🐍 Python project:
- Python
- Documentation
- News & Blog
- Python Planet blog aggregator
💓 Python Community:
- #python IRC for general questions
- #python-dev IRC for CPython developers
- PySlackers Slack channel
- Python Discord server
- Python Weekly newsletters
- Mailing lists
- Forum
✨ Python Ecosystem:
🌌 Fediverse
Communities
- #python on Mastodon
- c/django on programming.dev
- c/pythorhead on lemmy.dbzer0.com
Projects
- Pythörhead: a Python library for interacting with Lemmy
- Plemmy: a Python package for accessing the Lemmy API
- pylemmy pylemmy enables simple access to Lemmy's API with Python
- mastodon.py, a Python wrapper for the Mastodon API
Feeds
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Thanks. I think I understand why I wouldn't want to use it in this case. But what is an example of where I can use it? This makes me think I should avoid using bitwise operators with integers and keep it to strings only, but I know that's not true from what I've learned.
I use that in match case operations, but usually when is just two possibilities, try something like this and see if works
match coin:
case 5 | 10 | 20:
...
Edit: just tested and it's works.
When you're working with the binary representation of numbers.
In your code you had three numbers 25, 10 and 5. If we write those number in binary we get:
(The 0b at the start is just a way of saying "this is binary")
When you do a bitwise-or, it's a bit like adding up but you don't bother with carrying anything. So let's do 25 | 10, starting at the right-hand end going bit by bit (bitwise):
So the result is 0b00011011 which is 27.
So now you're asking "when would I ever need to do such a thing?" and the flippant answer is "you'll know when you need it".
You're looking for more though, I know. Basically computers often put multiple bits of data into bitstreams (long sequences of bits). Think networking and file storage. Constructing these bitstreams is done with bitwise operators like |, &, ^, << and >>. Together they form a different type of maths to what you're used to.
These operators work in a very similar way to how +, -, * and / work. They take two numbers and return a third. If we rewrite your code using operators you're more familiar with...
...you can see it's obviously wrong because you're doing one comparison with the result of the operation (addition or bitwise-or), not three comparisons.
Thank you for the thorough explanation. It makes sense to me why I had the error that I did. I'll keep this in mind next time when I consider using a bitwise operator.
Aside from operations on bitfields, a bitwise operator can be useful in several "non bits" cases. For instance:
value & 1
evaluates to 1 ifvalue
is odd (and will evaluate to True in anif
statement)value >> 1
dividesvalue
by 2 (integer division)But usually bitwise operators are for when you want to manipulate bits in values. For instance:
value | 5
returnsvalue
with bits 1 and 3 set to Truevalue & 0xffff
returns the 16 least-significant bits invalue
(usually you do this to make sure it will fit in 2 bytes in memory for example)value & (0xffff ^ 5)
returns the lower 16 bits ofvalue
with bits 1 and 3 set to FalseEtc.
Thank you for the reply. It seems bitwise operators are somewhat of an advanced concept that I may revisit down the road.
python in general tends toward readability over performance optimisation… you’re right they’re an advanced concept, and i’d say if you ever use bitwise operators in python they should be wrapped in some descriptive and very minimal function: treat it like a black box, because the next person that comes along likely won’t understand what’s happening without a pretty good description
a bit field is just not a descriptive data structure, so manipulate it directly as little as possible
i’d also say that most peoples use of bitwise operators is when unpacking external data formats (network traffic, operating system primitives, files formats, etc) and they’re usually wrapped in data structures that make those things more pythonic
unless you know you need bitwise operators, you probably don’t need bitwise operators
honestly yes you're probably not going to use them a lot, if at all, especially in python
You might use them with sets:
They're quite simple. Just convert the values to binary and apply the applicable truth tables. Just remember operator precedence when you use them, and in doubt, don't trust your luck and apply parentheses generously 🙂
And write generous unit tests so the next person doesn't accidentally mess it up.