You are given two numbers A and B. Write a program to count the number of bits to be flipped to change the number A to the number B. Flipping a bit of a number means changing a bit from 1 to 0 or vice versa.Input FormatThe first line of input contains T - the number of test cases. Each of the next T lines contains 2 integers A and B, separated by space.Output FormatFor each test case, print the number of bit flips required to convert A to B, separated by a new line.Constraints1 <= T <= 1000000 <= N <= 109ExampleInput420 1016 81 153549 24Output4236
Question
You are given two numbers A and B. Write a program to count the number of bits to be flipped to change the number A to the number B. Flipping a bit of a number means changing a bit from 1 to 0 or vice versa.Input FormatThe first line of input contains T - the number of test cases. Each of the next T lines contains 2 integers A and B, separated by space.Output FormatFor each test case, print the number of bit flips required to convert A to B, separated by a new line.Constraints1 <= T <= 1000000 <= N <= 109ExampleInput420 1016 81 153549 24Output4236
Solution
Here is a Python solution for the problem:
def countBitsFlip(a,b):
# XOR of two number will have set bits only at those places where a and b have different bits.
# To count the number of set bits in a number, we can use Brian Kernighan’s Algorithm.
n = a^b
count = 0
while n:
n = n & (n-1)
count += 1
return count
T = int(input())
for _ in range(T):
a, b = map(int, input().split())
print(countBitsFlip(a, b))
This program works by first taking the XOR of the two numbers, which will have set bits only at those places where a and b have different bits. Then it uses Brian
Similar Questions
Max Score: 50You are given two numbers A and B. Write a program to count the number of bits to be flipped to change the number A to the number B. Flipping a bit of a number means changing a bit from 1 to 0 or vice versa.Input FormatThe first line of input contains T - the number of test cases. Each of the next T lines contains 2 integers A and B, separated by space.Output FormatFor each test case, print the number of bit flips required to convert A to B, separated by a new line.Constraints1 <= T <= 1000000 <= N <= 109ExampleInput420 1016 81 153549 24Output4236
Given a number, swap the adjacent bits in the binary representation of the number, and print the new number formed after swapping.Input FormatThe first line of input contains T - the number of test cases. Each of the next T lines contains a number N.Output FormatFor each test case, print the new integer formed after swapping adjacent bits, separated by a new line.Constraints1 <= T <= 1000000 <= N <= 109ExampleInput410743100Output51123152
Given an Integer N, write a program to reverse it.InputThe first line contains an integer T, total number of testcases. Then follow T lines, each line contains an integer N.OutputFor each test case, display the reverse of the given number N, in a new line.Constraints1 ≤ T ≤ 10001 ≤ N ≤ 1000000Sample 1:InputOutput41234531203212323005432130213321232
Reverse the bits of an 32 bit unsigned integer A.Problem Constraints0 <= A <= 232Input FormatFirst and only argument of input contains an integer A.Output FormatReturn a single unsigned integer denoting the decimal value of reversed bits.Example InputInput 1: 0Input 2: 3Example OutputOutput 1: 0Output 2: 3221225472Example ExplanationExplanation 1: 00000000000000000000000000000000=> 00000000000000000000000000000000Explanation 2: 00000000000000000000000000000011 => 11000000000000000000000000000000
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in reverse binary. For an integer x, the algorithm is:As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is:6the output is:0116 in binary is 110; the algorithm outputs the bits in reverse.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.