Different ways to Invert the Binary bits in Python
Last Updated :
21 Jun, 2025
We know how binary value for numbers look like. For example, the binary value for 10 (Number Ten) is 1010 (binary value).
Sometimes it is required to inverse the bits i.e., 0's to 1's ( zeros to ones) and 1's to 0's (ones to zeros). Here are there few ways by which we can inverse the bits in Python.
Using Loops
You can iterate through each bit of the binary string and manually flip 0 to 1 and 1 to 0.
Python
bit_s = '1010'
inverse_s = ''
for i in bit_s:
if i == '0':
inverse_s += '1'
else:
inverse_s += '0'
print("Inversed string is ", inverse_s)
OutputInversed string is 0101
Using Dictionary
Dictionaries provide fast access to key-value pairs (O(1) time complexity). You can use a dictionary to map '0' to '1' and '1' to '0'.
Python
b_dict = {'0': '1', '1': '0'}
bit_s = '1010'
inverse_s = ''
for i in bit_s:
inverse_s += b_dict[i]
print("Inversed string is", inverse_s)
OutputInversed string is 0101
Explanation: dictionary b_dict maps 0 to 1 and 1 to 0.
Using List comprehension
List comprehension allows you to achieve the same result in a more compact and Pythonic way. The ternary operator can be used to conditionally replace 0 with 1 and 1 with 0.
Python
bit_s = '1010'
inverse_s = ''.join(['1' if i == '0' else '0' for i in bit_s])
print("Inversed string is", inverse_s)
OutputInversed string is 0101
Explanation:
- List comprehension is used to iterate over the binary string and apply the ternary operator for flipping each bit.
- ''.join() method is used to convert the list of flipped bits back into a string.
Using the replace() Method
You can use the string replace() method to replace 1 with 2, then 0 with 1, and finally, 2 with 0. This method can be a bit tricky but still effective.
Python
bit_s = '1010'
# Replace "1" with "2"
inv_s = bit_s.replace('1', '2')
# Replace "0" with "1"
inv_s = inv_s.replace('0', '1')
# Replace "2" with "0"
inv_s = inv_s.replace('2', '0')
print("Inversed string is", inv_s)
OutputInversed string is 0101
Explanation: This method leverages replace() to modify the bits step by step. It is a bit less efficient but still works.
Using bit-wise XOR operator
XOR operator is a powerful tool for flipping bits. XOR returns 1 if one of the bits is 1 and the other is 0. This is particularly useful for inverting bits.
Python
bit_s = '1010'
# Convert binary string into an integer
temp = int(bit_s, 2)
# XOR with 2^(n+1) - 1 to invert bits
inv_s = temp ^ (2 ** (len(bit_s)) - 1)
# Convert the result back to binary and strip the '0b' prefix
res = bin(inv_s)[2:]
print("Inversed string is", res)
OutputInversed string is 101
Similar Reads
How to invert the elements of a boolean array in Python? Given a boolean array the task here is to invert its elements. A boolean array is an array which contains only boolean values like True or False, 1 or 0. Input : A=[true , true , false] Output: A= [false , false , true] Input: A=[0,1,0,1] Output: A=[1,0,1,0] Method 1: You can use simple if else me
2 min read
Python Bin | Count total bits in a number Given a positive number n, count total bit in it. Examples: Input : 13 Output : 4 Binary representation of 13 is 1101 Input : 183 Output : 8 Input : 4096 Output : 13 We have existing solution for this problem please refer Count total bits in a number link. Approach#1: We can solve this problem quick
3 min read
How to Convert Bytes to Int in Python? Converting bytes to integers in Python involves interpreting a sequence of byte data as a numerical value. For example, if you have the byte sequence b'\x00\x01', it can be converted to the integer 1.Using int.from_bytes()int.from_bytes() method is used to convert a byte object into an integer. It a
3 min read
How to Convert Int to Bytes in Python? The task of converting an integer to bytes in Python involves representing a numerical value in its binary form for storage, transmission, or processing. For example, the integer 5 can be converted into bytes, resulting in a binary representation like b'\x00\x05' or b'\x05', depending on the chosen
2 min read
Convert binary to string using Python We are given a binary string and need to convert it into a readable text string. The goal is to interpret the binary data, where each group of 8 bits represents a character and decode it into its corresponding text. For example, the binary string '01100111011001010110010101101011' converts to 'geek'
3 min read