You can convert between a string representation of the binary using bin() and int()

>>> bin(88)
'0b1011000'
>>> int('0b1011000', 2)
88
>>> 

>>> a=int('01100000', 2)
>>> b=int('00100110', 2)
>>> bin(a & b)
'0b100000'
>>> bin(a | b)
'0b1100110'
>>> bin(a ^ b)
'0b1000110'
Answer from John La Rooy on Stack Overflow
🌐
AskPython
askpython.com › python › examples › binary-numbers-operations
Binary Numbers and Their Operations in Python - Complete Guide - AskPython
June 30, 2023 - The result has 1s in the positions where the bits differ between the two numbers. In a simpler language, if the corresponding bits are same (11 or 00) the result is 0 otherwise the result is 1. If you have interest in problem solving some observations on XOR operation can be really helpful. Let’s go through the example to understand the python implementation: # Bitwise XOR num1 = 0b110 num2 = 0b111 result = num1 ^ num2 print(bin(result)) The code will perform a bitwise XOR operation on binary numbers 0b110 and 0b111.
Discussions

Is there a way to see what the "real" binary number is when using bitwise operators like ~
There's many ways to store a number. What you are calling the "real binary" is two's compliment . However one requirement of an operation like that is that the integer has a fixed number of bits. Python integers are unlimited, so they cannot use two's complement internally. So python uses it's own system, which is why you see it as a negative. Edit: if you want to see the "real" binary of any number, here's some code that will do it for you. As I mentioned before, it requires that you define the size of the output result_size = 2 # in bytes result = " ".join(f"{x:0>8b}" for x in data.to_bytes(result_size, signed=True)) More on reddit.com
🌐 r/learnpython
7
6
April 11, 2023
Why use binary in python?
Yes, bitwise logic often is used when individual bits represent something. Bit masks are an easy way to describe let's say a state of series of on/off switches. >>> gate1 = 0b0001 >>> gate2 = 0b0010 >>> gate3 = 0b0100 >>> gate4 = 0b1000 >>> gate1 | gate3 5 #0b0101 >>> gate2 | gate3 | gate4 14 #0b1110 instead of pushing a shitload of bools into a function you can pass a mask. More on reddit.com
🌐 r/learnpython
12
9
June 17, 2015
How to generate a binary file with 32bit numbers in Python.
The issue there is that you're trying to cram an entire integer into one byte, because bytearray takes each element of your list and tries to convert it to a byte. There are several ways you could proceed. In no particular order: use struct.pack to convert integers to bytes (can do all at once) use number.to_bytes for each number in your list skip generating random numbers, and use secrets.rand_bits or similar to generate the bytes directly use an array type of integers (eg, numpy.array, array.array), and use those objects built in conversion to/from bytes More on reddit.com
🌐 r/learnpython
6
2
May 7, 2022
Why numbers in binary representation has the prefix "0b" or "0B"?

B for binary

It's been that way for almost every programming language since at least C.

More on reddit.com
🌐 r/Python
3
0
May 17, 2020
🌐
Boot.dev
boot.dev › lessons › 185942ab-a62c-4e12-be0c-5b345e2cc928
Learn Python for Beginners: Binary Numbers | Boot.dev
Binary numbers are just "base 2" numbers. They work the same way as "normal" base 10 numbers, but with two symbols instead of ten.
🌐
Exercism
exercism.org › tracks › python › concepts › binary-octal-hexadecimal
Binary, Octal, and Hexadecimal in Python on Exercism
So if we want to represent the number 6 in binary, it would be 110. ... In Python, we can represent binary literals using the 0b prefix.
🌐
Boot.dev
boot.dev › lessons › 5a228543-eef4-492c-8b8c-df87c76f028b
Binary Numbers - Learn to Code in Python
Ready to finally become a backend developer? Don't make it a grind. The smartest way to learn to code is to ensure you're never bored.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › binary number in python
Binary number in Python | How Binary number works in Python?
April 3, 2023 - In Python, we can basically utilize the bin() function to change over from a decimal value to its corresponding value. Also, comparatively, the int() function is used to convert binary numbers into decimal numbers.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Pythonspot
pythonspot.com › binary-numbers-and-logical-operators
Python Binary Number — Tutorial with Examples | Pythonspot
January 1, 2026 - The number '00110010' would be (0 x 2^0) + (1 x 2^1) + (0 x 2^2) + (0 x 2^3) + (1 x 2^4) + (1 x 2^5) + (0 x 2^6) + (0 x 2^7) = 32+16+2 = 50. Try the sequence '00101010' yourself to see if you understand and verify with a Python program. Binary Left Shift and Binary Right Shift Multiplication by a factor two and division by a factor of two is very easy in binary.
Find elsewhere
🌐
Educative
educative.io › answers › how-to-add-binary-numbers-in-python
How to add binary numbers in Python
Binary numbers are essential to ... management, and logical operations. Python’s bin() method converts an integer to its binary representation as a string with a 0b prefix....
🌐
IncludeHelp
includehelp.com › python › binary-numbers-representation.aspx
Python | Binary numbers representation (assign, conversion, bitwise operations)
June 26, 2023 - a = 0b111101 b = 0b000010 # print value in binary print "values in binary..." print "a: ",bin (a) print "b: ",bin (b) # bitwise OR and AND operations print "(a|b) : ", bin (a|b) print "(a&b) : ", bin (a&b) # print values in decimal print "values in decimal..." print "a: ",a print "b: ",b # bitwise OR and AND operations print "(a|b) : ", int (bin (a|b),2) print "(a&b) : ", int (bin (a&b),2)
🌐
LinkedIn
linkedin.com › pulse › understanding-binary-numbers-using-python-elizabeth-parnell-wp8ae
Understanding Binary Numbers using Python
April 29, 2026 - We do this because if we were to represent the binary number as an integer, Python would interpret it as a decimal number. We can also use the 0b prefix to represent binary numbers in Python.
🌐
Medium
medium.com › @gitauharrison › binary-representation-of-integers-in-python-bfd2f4d3c6e5
Binary Representation of Integers in Python | by Gitau Harrison | Medium
September 14, 2022 - With the addition of the negative sign at the beginning of the binary number, we know that the integer is negative. ... This function takes a value plus the format spec, which in this case is “b”. $ python3>>> my_negative_number = -1234 >>> my_positive_pos_number = 1234>>> format(my_negative_number, 'b') '-10011010010'>>> format(my_positive_pos_number, 'b') '10011010010'
🌐
Delft Stack
delftstack.com › home › howto › python › binary numbers in python
Binary Numbers Representation in Python | Delft Stack
March 11, 2025 - Python offers a straightforward way to sum binary numbers by leveraging its built-in functions. We can convert binary strings into integers, perform the addition, and then convert the result back into a binary format.
🌐
W3Schools
w3schools.com › python › ref_func_bin.asp
Python bin() Function
Remove List Duplicates Reverse ... Python Study Plan Python Interview Q&A Python Training ... The bin() function returns the binary version of a specified integer....
🌐
Reddit
reddit.com › r/learnpython › is there a way to see what the "real" binary number is when using bitwise operators like ~
r/learnpython on Reddit: Is there a way to see what the "real" binary number is when using bitwise operators like ~
April 11, 2023 -

So to explain what I mean, in Python3 negative binary numbers are represented as something like -0b110 (decimal -6). So -0b110 is what Python shows me if I do: print(bin(~5))

But if I'm understanding things correctly, the "real" binary number of a ~ operation is not this, it's the inversion of the original binary number. So instead of getting -0b110 (decimal -6) from a binary of 0101 (decimal 5), I would like to see the inverted binary, which I believe is 1010.

The reason for this is that, as I try to get gud with these operators, it appears to me that Python is sometimes using the "real" binary number to perform operations and not the ones printed out by bin(). Now maybe I'm just confused, I find this pretty difficult but if I could print the "real" binary value it would make it easy for me to clarify things by observing what a script is doing in a more direct way.

I guess I could just write something that inverts the provided original binary, but I'm wondering if there's a direct way to have it print the output besides writing a function.

🌐
Real Python
realpython.com › videos › binary-numbers
Binary Numbers (Video) – Real Python
01:26 The patterns can mean other things as well. Pi gets approximated by a sixteen-digit floating-point number, which is represented by the following binary code. 01:36 This code is actually comprised of three numbers: a sign indicator and an exponent value and a fractional decimal value.
Published: December 7, 2021
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-covert-decimal-to-binary-number
Convert Decimal to Binary Number - GeeksforGeeks
The remainder is stored to form the binary digits. This is a straightforward and efficient way to manually convert decimal to binary, leveraging integer division. ... n = 7 # decimal number res = '' # binary result while n > 0: res = str(n % 2) + res n //= 2 print(res)
Published: July 11, 2025
🌐
k0nze
k0nze.dev › posts › python-binary-numbers
Python Course #4: Introduction to Binary Numbers (Converting Decimal to Binary) | k0nze
January 30, 2024 - The x enclosed in braces { } means that the value of x should be printed as part of the string between quotes. The b after the colon tells Python to print x as a binary number. But how do you convert a decimal number using only pen and paper? Let’s use example 23 again.
🌐
Medium
rjallain.medium.com › understanding-binary-and-hexadecimal-numbers-by-writing-python-code-f7c4012fafe7
Understanding Binary and Hexadecimal Numbers by Writing Python Code | by Rhett Allain | Medium
July 8, 2023 - We use 0–9 plus A, B, C, D, E, F (since we ran out of decimal numbers). I’m going to show how to convert from binary to decimal (and back) and then the same for hexadecimal. Although you can use built in functions in python, we are going ...
🌐
Michaelcho
michaelcho.me › article › bit-manipulation-part-1-understanding-binary-numbers
Bit Manipulation Part 1 - Understanding binary numbers in Python
November 13, 2018 - The bits at the far right represent ... the simple way to get this number is using the built in bin() function (note - this returns a binary string, not the binary bits), and using int() to convert back from binary to a base 10 integer....
🌐
NetworkLessons
networklessons.com › home › python › python bitwise operators
Python Bitwise Operators
We use the AND operator with decimal numbers 8 and 9 and store the result in variable “and_result”. We format variable “and_result” into binary and store the result in variable “binary_and_result”.
Published: April 3, 2020