0

Had a question come up recently which was: write the algorithm to convert a decimal number to an n-bit gray code.

So for example: Using 1-bit (simplest):

0 -> 0
1 -> 1

Using 2-bit

0 -> 00
1 -> 01
2 -> 11
3 -> 10

Using 3-bit

0 -> 000
1 -> 001
2 -> 011
3 -> 010
4 -> 110
5 -> 111
6 -> 101
7 -> 100
Nabeel Saad
  • 152
  • 2
  • 12
  • possible duplicate of [Fill matrix with binary numbers, regular and gray coded](http://stackoverflow.com/questions/17652524/fill-matrix-with-binary-numbers-regular-and-gray-coded) – mbomb007 Mar 03 '15 at 21:21

2 Answers2

1

I was working in a different mathematical field. Unintentionally, I discovered two ways to convert numbers to Gray code. Example. From right to left: I make the divisions of 173 (8 digits in binary system) with the numbers 2,4,8,16,32, ..., 256. I round each quotient to the nearest integer. I am writing this integer down from the corresponding fraction. If this integer is even, then I write below this digit 0, otherwise I write the digit 1. These digits form the Gray Code of 173.

Faster method. I can convert all numbers that have equal lengths of digits in the binary system to Gray codes. I do this without turning any number into binary. Here I find it difficult to present this method because it contains graphs, but you can find this here:

http://viXra.org/abs/2004.0456?ref=11278286

0

Wrote the following and figured I'd share it as I don't see many Java implementations showing up on here:

static String getGreyCode(int myNum, int numOfBits) {
    if (numOfBits == 1) {
        return String.valueOf(myNum);
    }

    if (myNum >= Math.pow(2, (numOfBits - 1))) {
        return "1" + getGreyCode((int)(Math.pow(2, (numOfBits))) - myNum - 1, numOfBits - 1);
    } else {
        return "0" + getGreyCode(myNum, numOfBits - 1);
    }
}

static String getGreyCode(int myNum) {

    //Use the minimal bits required to show this number
    int numOfBits = (int)(Math.log(myNum) / Math.log(2)) + 1;
    return getGreyCode(myNum, numOfBits);
}

And to test this, you can call it in either of the following ways:

System.out.println("Grey code for " + 7 + " at n-bit: " + getGreyCode(7));
System.out.println("Grey code for " + 7 + " at 5-bit: " + getGreyCode(7, 5));

Or loop through all the possible combinations of grey codes up to the ith-bit:

for (int i = 1; i <= 4; i++) {
        for (int j = 0; j < Math.pow(2, i); j++)
            System.out.println("Grey code for " + j + " at " + i + "-bit: " + getGreyCode(j, i));

Hope that proves helpful to folks!

Nabeel Saad
  • 152
  • 2
  • 12
  • 3
    Why not use shift left instead of `Math.pow(2,...)`? – RealSkeptic Mar 03 '15 at 20:42
  • This is probably the worst piece of code I have ever seen on SO... `new Double(Math.pow(2, (numOfBits))).intValue()` should be `(int) Math.pow(2, numBits)`, `<= Math.pow(2, i) - 1` is the same as `< Math.pow(2, i)` and should be stored in a variable, and you don't need recursion to implement this method. – Clashsoft Mar 03 '15 at 20:48
  • 3
    It's literally as easy as `x ^ (x >> 1)`. What is all this stuff? – harold Mar 03 '15 at 20:50
  • @harold it was a requirement to implement the algorithm rather then use the out of the box operators – Nabeel Saad Mar 03 '15 at 21:00
  • @Clashsoft there's no need to be so negative, if you feel the code can be improved, be my guest to modify it so that it'll be helpful for someone else. Recursion was requested in the solution and this code worst, sure it could be optimised. – Nabeel Saad Mar 03 '15 at 21:03
  • @harold also, if I replace x with 7 as in my example, then your equation comes out at 4, have I got it wrong? – Nabeel Saad Mar 03 '15 at 21:12
  • 1
    4 is the right answer, the same answer you give. Of course you give it as a binary string, but it's the same value. – harold Mar 03 '15 at 21:14