如何找到,如果两个数字都在灰色code顺序连续编号 [英] How to find if two numbers are consecutive numbers in gray code sequence

查看:191
本文介绍了如何找到,如果两个数字都在灰色code顺序连续编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拿出一个解决方案,这两个数字的问题,发现如果他们是灰色code顺序即连续编号,如果是灰色code邻居假设灰色code序列没有被提及。

I am trying to come up with a solution to the problem that given two numbers, find if they are the consecutive numbers in the gray code sequence i.e., if they are gray code neighbors assuming that the gray code sequence is not mentioned.

我搜索的各种论坛,但不能得到正确的答案。这将是巨大的,如果你能提供一个解决方案。

I searched on various forums but couldn't get the right answer. It would be great if you can provide a solution for this.

我试图问题 - 转换两个整数二进制并在这两个数字分别添加数字,并找到在两个数的数字的总和之间的差。如果差异之一,那么他们是灰色code邻居。

My attempt to the problem - Convert two integers to binary and add the digits in both the numbers separately and find the difference between the sum of the digits in two numbers. If the difference is one then they are gray code neighbors.

但我觉得所有的情况下,这不会工作。任何帮助是非常AP preciated。非常感谢事先!

But I feel this wont work for all cases. Any help is highly appreciated. Thanks a lot in advance!!!

推荐答案

我不得不解决在接受采访时这个问题为好。该条件之一为两个值是一个灰色code序列是,它们的值仅1比特不同。下面是对这个问题的解决方案:

I've had to solve this question in an interview as well. One of the conditions for the two values to be a gray code sequence is that their values only differ by 1 bit. Here is a solution to this problem:

def isGrayCode(num1, num2):
    differences = 0
    while (num1 > 0 or num2 > 0):
        if ((num1 & 1) != (num2 & 1)):
            differences++
        num1 >>= 1
        num2 >>= 1
    return differences == 1

这篇关于如何找到,如果两个数字都在灰色code顺序连续编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆