Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Take any 2 input numbers and converts into binary format and find the number of digits flipped in between both numbers.

+1 vote
asked Feb 24, 2020 by Vamsi Gudivada (130 points)
Consider 2 numbers 7 and 10
Binary digits of 7 is 00000111
Binary digits of 10 is 00001010
Output is 3.from the above we can see that 3 digits are flipped.

3 Answers

0 votes
answered Feb 25, 2020 by anonymous
x=int(input())
y=int(input())
a=[]
b=[]
a1=[]
b1=[]
k=0
while x>=1:
    m=x%2
    x=x//2
    a.append(m)

while y>=1:
    n=y%2
    y=y//2
    b.append(n)

if(len(a)<len(b)):
    while(len(a)!=len(b)):
        a.append(k)

if(len(b)<len(a)):
    while(len(b)!=len(a)):
        b.append(k)

a1=a[::-1]
b1=b[::-1]

for i in a1:
    print(i,end="")
print('\n')

for j in b1:
    print(j,end="")
print('\n')
    
count=0
for i in range(0,len(a1)):
    if(a1[i]!=b1[i]):
        count+=1
print("flips=",count)
0 votes
answered Feb 25, 2020 by galingrudov (140 points)

#include <iostream>

unsigned int countSetBits(unsigned int n)

{

unsigned int count = 0;

while (n) {

count += n & 1;

n >>= 1;

}

return count;

}

int main()

{

int a, b;

std::cin >> a >>b;

int c = a^b;

std::cout << countSetBits(c);

return 0;

}

0 votes
answered Feb 25, 2020 by SURYA N (180 points)
n,m=map(int,input().split())
b1=bin(n)
b2=bin(m)
c=0
b1=b1[2:]
b2=b2[2:]
n1=len(b1);n2=len(b2)

b1=str(0)*(8-n1)+b1
b2=str(0)*(8-n2)+b2

for i in range(len(b1)):
    if b1[i]!=b2[i]:
        c+=1;

print(c)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...