简体   繁体   English

将二进制数的字符数组转换为C ++中的格雷码计数器

[英]Convert a character array of binary numbers into a counter of Gray Code in C++

is there any way to convert a char array of binary number into Gray Code. 有什么办法可以将二进制数的char数组转换为Gray Code。 For example, I have the following code: 例如,我有以下代码:

int j;
char binaryNum[10], *pointer;
/* From Hex convert to decimal */
j = strtol( str, &pointer, 16);
/* From Decimal convert to Binary */
itoa(j, binaryNum, 2);
cout<<"Binary form of Y = "<<binaryNum<<"\n";

What i want is to convert this binaryNum into Gray Code, ie change it one bit at a time. 我想要的是将此binaryNum转换为格雷码,即一次将其更改一位。 Can somebody please help me with the code ? 有人可以帮我提供代码吗? forexample, i have a char binaryNum[10] == 101101 and i want to convert it to gray code, ie change only one bit at a time, like: 101100 101110 101111 something like thiss.. 例如,我有一个char binaryNum [10] == 101101,我想将其转换为格雷码,即一次只更改一位,例如:101100 101110 101111。

它可以很简单:

x_gray = x ^ (x >> 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM