A bytes sequence is an immutable sequence of integers (like a tuple of numbers). Unfortunately, bitwise operations are not defined on them—regardless of how much sense it would make to have them on a sequence of bytes.

So you will have to go the manual route and run the operation on the bytes individually. As you only have a single byte each, it’s really simple to do so though. For the same reason you also don’t need to care about endianness, as that’s only applicable when talking about multiple bytes.

So, you could do it like this:

>>> a, b = b'\x12', b'\x34'
>>> bytes([a[0] & b[0]])
b'\x10'
Answer from poke on Stack Overflow
🌐
Mimo
mimo.org › glossary › python › bytes
Python Bytes: Syntax, Usage, and Examples
This process is essential when sending data over a network or storing data in binary files. The opposite of this operation—bytes to string—requires decoding. If you skip specifying the encoding, Python 3 uses UTF-8 as the default encoding, which works for most unicode characters.
🌐
Real Python
realpython.com › python-bytes
Bytes Objects: Handling Binary Data in Python – Real Python
March 5, 2025 - Other than that, Python bytes and str share over eighty percent of methods in their public interfaces. After all, operations like finding and replacing substrings or splitting and joining strings can be just as useful for byte sequences:
🌐
Programiz
programiz.com › python-programming › methods › built-in › bytes
Python bytes()
Online Python Online JavaScript ... Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... The bytes() method returns an immutable bytes object initialized with the given size and data....
🌐
Python
docs.python.org › 3 › builtins › functions.html
Built-in Functions — Python 3.14.7 documentation
If the argument is a bytes or bytearray object of length 1, return its single byte value. For example, ord(b'a') returns the integer 97. ... Return base to the power exp; if mod is present, return base to the power exp, modulo mod (computed ...
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › methods › built-in › bytes › python-bytes
Python bytes() function | Why is Python bytes() function used? |
July 30, 2021 - Simply supply the string as the source argument and the encoding as the encoding argument to convert a string to bytes. The error argument is optional and can be specified as per the programmer’s need. ... # Python program to illustrate bytes() with string source txt = 'Hello World' arr = ...
🌐
Narkive
python-ideas.python.narkive.com › tQwCFH6r › bitwise-operations-on-bytes
bitwise operations on bytes
GLORPED = 0x10 newchar = flags[1] | GLORPED flags = flags[0] + newchar + flags[2:] GLORPED = b"\x00\x10\x00\x00" flags |= GLORPED You can already do bitwise operations on bytes, and retrieving the relevant byte and using single-byte operations on it is simple enough. As I see it, the major underlying problem here is that byte-arrays are immutable, since what you really want is to be able to change a single byte of the array in-place. In general, Python's byte arrays and strings are very ill suited for dealing with data which isn't byte-based, and definitely horrible at building or modifying data streams.
Find elsewhere
🌐
Fredscave
fredscave.com › 44-micropython-data-types-bytes.html
MicroPython - bytes Data Type
For example b1[:3] will return the first three elements of the bytes object b1. If two numbers are supplied with the slicing operator then all elements starting from the first index up to but not including the second index will be returned.
🌐
PythonForBeginners.com
pythonforbeginners.com › home › bytes in python
Bytes in Python - PythonForBeginners.com
July 5, 2021 - In this article, we have seen what bytes objects are and how we can create bytes objects from iterables and strings using bytes() method.We can also write the programs used in this article with exception handling using python try except to make the programs more robust and handle errors in a systematic way.
🌐
Splunktool
splunktool.com › bytes-operations-in-python
HeadlineLogic News Portal
September 21, 2022 - Search on topics your interested. Read great personalised news.
🌐
Google Groups
groups.google.com › g › python-ideas › c › d1hkiD__fl0
[Python-ideas] bitwise operations on bytes
You can already do bitwise operations on bytes, and retrieving the relevant byte and using single-byte operations on it is simple enough. As I see it, the major underlying problem here is that byte-arrays are immutable, since what you really want is to be able to change a single byte of the array in-place. In general, Python's byte arrays and strings are very ill suited for dealing with data which isn't byte-based, and definitely horrible at building or modifying data streams.
🌐
Github
he-arc.github.io › livre-python › binary › index.html
bytes / bytearray / memoryview, struct — Documentation Bibliothèques Python 1.0.0
En plus des opérations, ci-dessous voir les opérations de bytes ci-dessus. """Module d'exemple de cast entre bytes et bytearray.""" # Cast bytes à bytearray. mutable_bytes = bytearray(b'\x00\x0F') # Cast bytearray à bytes. immutable_bytes = bytes(mutable_bytes) Une memoryview est un objet permettant d’utiliser des buffers afin de pouvoir les manipuler comme tout autre objet Python...
🌐
PyPI
pypi.org › project › bitarray
bitarray · PyPI
3 weeks ago - Bitwise operations: ~, &, |, ^, <<, >> (as well as their in-place versions &=, |=, ^=, <<=, >>=). Free-threading support (for GIL-disabled CPython 3.14 and later) Fast methods for encoding and decoding variable-length prefix codes.
🌐
Python Examples
pythonexamples.org › python-bytes
Python bytes
Python bytes object is immutable, so inplace update operations or modifications on the original bytes object cannot be done.
🌐
Sjoerd Langkemper
sjoerdlangkemper.nl › 2019 › 04 › 24 › bits-bytes-in-python-2-3
Working with bits and bytes in Python 2 and 3
April 24, 2019 - After we modify the number we want to put it back in our message. We have to convert it to bytes again. In Python 2 we used chr for this, but this won’t work in Python 3: it will convert the number to a string instead of a byte.
🌐
BeginnersBook
beginnersbook.com › 2019 › 05 › python-bytes
Python bytes() Function (With Examples)
July 6, 2021 - If the source is an integer, an empty bytearray object of the specified size is created and if the source is a String, make sure that you specify the encoding of the source.
🌐
Google Books
books.google.ru › books
T-Bytes Agile & AI Operations - IT-Shades - Google Книги
This document brings together a set of latest data points and publicly available information relevant for Agile & AI Operations Industry. We are very excited to share this content and believe that readers will benefit from this periodic publication immensely.
🌐
Dot Net Perls
dotnetperls.com › bytes-python
Python - bytes, bytearray Examples - Dot Net Perls
A bytearray supports many of the same operations as a list. We can append values. We can delete a value or a range of values with del.