# Python Program to Find the Total Number of Bits Needed to be Flipped

Given two numbers the task is to print the number of bits to be flipped to convert the given number to the other number.

Examples:

Example1:

Input:

given the first number = 12
given the second number = 9

Output:

Binary representations of the given two numbers are :
12 = 1100
9 = 1001
The total number of bits to be flipped = 2

Example2:

Input:

given the first number = 59
given the second number = 3

Output:

Binary representations of the given two numbers are :
59 = 111011
3 = 11
The total number of bits to be flipped = 3

## Program to Find the Total Number of Bits Needed to be Flipped

Below is the full approach to print the number of bits to be flipped to convert the given number to the other number.

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

### 1)Algorithm

This problem can be rephrased as determining the number of dissimilar corresponding bits in firstnumber and secondnumber. And the bitwise XOR operator () can assist us in determining this. The XOR of two bits returns 1 only if the bits are not the same. As a result, our solution consists of two easy steps:

1. Determine the firstnumber and secondnumber’s bitwise XOR.
2. Determine the number of set bits in the result.

1. Calculate XOR of firstnumber and secondnumber.
firstnumber _xor_secondnumber = firstnumber ^ secondnumber
2. Count the set bits in the above
calculated XOR result.
countSetBits ( firstnumber _xor_secondnumber)

### 2)Using XOR Operator and Count Variable(Static Input)

Approach:

• Give the two numbers as static input and store them in two separate variables.
• Pass the given two numbers as arguments to the countFlipbits function which calculates the total number of bits to be flipped to convert one number into another.
• Calculate the xor value of the two numbers and store it in a result say resultnum.
• Use the while loop to calculate the total number of set bits in the resultnum.
• Return the number of set bits which is the result (number of bits to be flipped).
• Print the result.
• The Exit of the Program.

Below is the implementation:

# function which accepts the two numbers as arguments
# and return the number of bits to be flipped

def countFlipbits(firstnumber, secondnumber):
# Calculate the xor value of the two numbers and store it in a result say resultnum.
resultnum = firstnumber ^ secondnumber
# Use the while loop to calculate the total number of set bits in the resultnum.
setbitCounter = 0
while resultnum:
resultnum = resultnum & (resultnum - 1)
setbitCounter = setbitCounter + 1
# Return the number of set bits which is the result (number of bits to be flipped).
return setbitCounter

# Driver Code
# Give the two numbers as static input and store them in two separate variables.
firstnumber = 12
secondnumber = 9
# Pass the given two numbers as arguments to the ( function which calculates the total number
# of bits to be flipped to convert one number into another.
print('Binary representations of the given two numbers are :')
print(firstnumber, '=', bin(firstnumber)[2:])
print(secondnumber, '=', bin(secondnumber)[2:])
print('The total number of bits to be flipped =',
countFlipbits(firstnumber, secondnumber))


Output:

Binary representations of the given two numbers are :
12 = 1100
9 = 1001
The total number of bits to be flipped = 2

### 3)Using XOR Operator and Count Variable(User Input separated by spaces)

Approach:

• Give the two numbers as user input using int, map(), and split() functions.
• Store them in two separate variables.
• Pass the given two numbers as arguments to the countFlipbits function which calculates the total number of bits to be flipped to convert one number into another.
• Calculate the xor value of the two numbers and store it in a result say resultnum.
• Use the while loop to calculate the total number of set bits in the resultnum.
• Return the number of set bits which is the result (number of bits to be flipped).
• Print the result.
• The Exit of the Program.

Below is the implementation:

# function which accepts the two numbers as arguments
# and return the number of bits to be flipped

def countFlipbits(firstnumber, secondnumber):
# Calculate the xor value of the two numbers and store it in a result say resultnum.
resultnum = firstnumber ^ secondnumber
# Use the while loop to calculate the total number of set bits in the resultnum.
setbitCounter = 0
while resultnum:
resultnum = resultnum & (resultnum - 1)
setbitCounter = setbitCounter + 1
# Return the number of set bits which is the result (number of bits to be flipped).
return setbitCounter

# Driver Code
# Give the two numbers as user input using int, map(), and split() functions.
# Store them in two separate variables.
firstnumber, secondnumber = map(int, input(
'Enter some random two numbers separated by spaces = ').split())
# Pass the given two numbers as arguments to the ( function which calculates the total number
# of bits to be flipped to convert one number into another.
print('Binary representations of the given two numbers are :')
print(firstnumber, '=', bin(firstnumber)[2:])
print(secondnumber, '=', bin(secondnumber)[2:])
print('The total number of bits to be flipped =',
countFlipbits(firstnumber, secondnumber))


Output:

Enter some random two numbers separated by spaces = 54 96
Binary representations of the given two numbers are :
54 = 110110
96 = 1100000
The total number of bits to be flipped = 4

### 4)Using XOR Operator and Built-in Count function(Static Input)

Approach:

• Give the two numbers as static input and store them in two separate variables.
• Pass the given two numbers as arguments to the countFlipbits function which calculates the total number of bits to be flipped to convert one number into another.
• Calculate the xor value of the two numbers and store it in a result say resultnum.
• Convert this resultnum to binary and calculate the total number of sets bits in it using the count() function.
• The Exit of the Program.

Below is the implementation:

# function which accepts the two numbers as arguments
# and return the number of bits to be flipped

def countFlipbits(firstnumber, secondnumber):
# Calculate the xor value of the two numbers and store it in a result say resultnum.
resultnum = firstnumber ^ secondnumber
# Convert this resultnum to binary and calculate the total
# number of sets bits in it using the count() function.
binNumber = bin(resultnum)
setbitCounter = binNumber.count('1')
return setbitCounter

# Driver Code

# Driver Code
# Give the two numbers as static input and store them in two separate variables.
firstnumber = 18
secondnumber = 68
# Pass the given two numbers as arguments to the ( function which calculates the total number
# of bits to be flipped to convert one number into another.
print('Binary representations of the given two numbers are :')
print(firstnumber, '=', bin(firstnumber)[2:])
print(secondnumber, '=', bin(secondnumber)[2:])
print('The total number of bits to be flipped =',
countFlipbits(firstnumber, secondnumber))


Output:

Binary representations of the given two numbers are :
18 = 10010
68 = 1000100
The total number of bits to be flipped = 4

### 5)Using XOR Operator and Built-in Count function(User Input Separated by Newline)

Approach:

• Give the first number as user input using int(input()) and store it in a variable.
• Give the second number as user input using int(input()) and store it in another variable.
• Pass the given two numbers as arguments to the countFlipbits function which calculates the total number of bits to be flipped to convert one number into another.
• Calculate the xor value of the two numbers and store it in a result say resultnum.
• Convert this resultnum to binary and calculate the total number of sets bits in it using the count() function.
• The Exit of the Program.

Below is the implementation:

# function which accepts the two numbers as arguments
# and return the number of bits to be flipped

def countFlipbits(firstnumber, secondnumber):
# Calculate the xor value of the two numbers and store it in a result say resultnum.
resultnum = firstnumber ^ secondnumber
# Convert this resultnum to binary and calculate the total
# number of sets bits in it using the count() function.
binNumber = bin(resultnum)
setbitCounter = binNumber.count('1')
return setbitCounter

# Driver Code

# Driver Code
# Give the first number as user input using int(input()) and store it in a variable.
firstnumber = int(input('Enter first random number = '))
# Give the second number as user input using int(input()) and store it in another variable.
secondnumber = int(input('Enter second random number = '))
# Pass the given two numbers as arguments to the ( function which calculates the total number
# of bits to be flipped to convert one number into another.
print('Binary representations of the given two numbers are :')
print(firstnumber, '=', bin(firstnumber)[2:])
print(secondnumber, '=', bin(secondnumber)[2:])
print('The total number of bits to be flipped =',
countFlipbits(firstnumber, secondnumber))


Output:

Enter first random number = 59
Enter second random number = 3
Binary representations of the given two numbers are :
59 = 111011
3 = 11
The total number of bits to be flipped = 3

Related Programs: