fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. /* The class name doesn't have to be Main, as long as the class is not public. */
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. new Main().test2();
  9. }
  10.  
  11. void test2() {
  12.  
  13. final int grayCodeLength = 4;
  14.  
  15. // generate matrix
  16. final int grayCodeCount = 1 << grayCodeLength; // = 2 ^ grayCodeLength
  17. int grayCodeMatrix[][] = new int[grayCodeCount][grayCodeLength];
  18. for (int i = 0; i < grayCodeCount; i++) {
  19. int grayCode = (i >> 1) ^ i;
  20. for (int j =0; j <grayCodeLength; j++) {
  21. // extract bit
  22. final int grayCodeBitMask = 1 << j;
  23. grayCodeMatrix[i][j] = (grayCode & grayCodeBitMask) >> j;
  24. }
  25. }
  26.  
  27. // view result
  28. for (int y = 0; y < grayCodeMatrix.length; y++) {
  29. for (int x = 0; x < grayCodeMatrix[0].length; x++) {
  30. System.out.print(grayCodeMatrix[y][x]);
  31. }
  32. System.out.print("\n");
  33. }
  34.  
  35.  
  36.  
  37. }
  38. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
0000
1000
1100
0100
0110
1110
1010
0010
0011
1011
1111
0111
0101
1101
1001
0001