0

is there any way to convert a char array of binary number into 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, i.e change it one bit at a time. 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, i.e change only one bit at a time, like: 101100 101110 101111 something like thiss..

4
  • 2
    Is this homework? What all have you tried so far ? Commented Feb 6, 2011 at 18:35
  • no, its basically a long code. this is just a snippet of what. What i am not understanding is that, how to change only one bit at a time and generate series, like its done in gray's code? i can convert it to array of intergers but what next? Commented Feb 6, 2011 at 18:47
  • See Oli's answer below - it's very easy to convert an integer to its corresponding Gray Code representation Commented Feb 6, 2011 at 18:49
  • i want to do something like, for example i have a char binaryNum[10] == 101101 and i want to convert it to gray code, i.e change only one bit at a time, like: 101100 101110 101111 something like thiss.. Commented Feb 6, 2011 at 19:16

1 Answer 1

4

It can be as simple as:

x_gray = x ^ (x >> 1);
Sign up to request clarification or add additional context in comments.

5 Comments

@bijlikamasla: Sounds like you need to go look up bitwise operators in C. That's not exponentiation.
i want to do something like, for example i have a char binaryNum[10] == 101101 and i want to convert it to gray code, i.e change only one bit at a time, like: 101100 101110 101111 something like thiss.
@bijilk: That doesn't make any sense. Are you just asking for a gray-code counter?
yeah, sort of, but for char array.. not integer array.
@bijilk: Then please update your question to make it clear exactly what you're trying to do. If you want to make a counter, then say so!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.