Write an algorithm to find out if the binary
representation of two given positive integers
can be placed successively in a gray code
sequence.
Input
The first line of the input consists of the first
positive integer.
The second line consists of the second positive
}
integer.
Output
Print 1 if the binary representation of given
Answers
Answered by
0
Answer:
Cohen chota chota chota dos tunic
Answered by
0
n algorithm to find out if the binary representation of two given positive integers
def gTob(num):
mask = num >> 1
while mask!=0:
num = num^mask
mask >>= 1
return num;
def areGrayNeighbors(a,b):
return abs(gTob(a) - gTob(b)) == 1
- It's a simple solution, but it works. Subtract the two after converting each grey code to its corresponding Binary form. The two grey codes are nearby if your response is the binary value of +1 or -1.
- This may sound like overkill, but it might be useful if you're in an interview and are unsure about the proper approach. The single bit difference filter can be used to further optimise, saving time by preventing the conversion and subtraction of values that we are certain are not adjacent.
- Two numbers differ by one binary digit if they are in the grey code sequence. i.e., a power of 2 is produced when the two numbers are ORed exclusively.
#SPJ2
Similar questions
Science,
2 months ago
English,
2 months ago
English,
2 months ago
Social Sciences,
6 months ago
Business Studies,
6 months ago
Chemistry,
11 months ago
Physics,
11 months ago