Questions tagged [gray-code]

Anything related to Gray code, also known as reflected binary code, i.e. a n-bit binary code where any one of the 2^n binary code words differs from the next in just one bit position.

Anything related to Gray code, also known as reflected binary code, i.e. a n-bit binary code where any one of the 2n binary code words differs from the next in just one bit position.

Gray codes were invented in 1947 by Frank Gray and are used to solve a variety of problems (e.g. Tower of Hanoi) and to ease error correction in some communication systems.

See Wikipedia page on Gray code.

90 questions
22
votes
17 answers

Code Golf: Gray Code

The Challenge The shortest program by character count that outputs the n-bit Gray Code. n will be an arbitrary number smaller than 1000100000 (due to user suggestions) that is taken from standard input. The gray code will be printed in standard…
Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95
17
votes
3 answers

Gray codes addition

Is there any known way to compute the addition (and maybe the subtraction) of two Gray codes without having to convert the two Gray codes to regular binary, perform a binary addition then convert the result back to a Gray code? I managed to write…
Morwenn
  • 21,684
  • 12
  • 93
  • 152
15
votes
4 answers

Gray code increment function

Without using any external counters or other state, I'm looking for an efficient function which takes an n-bit value (32 bits or thereabouts) and returns the subsequent value in a Gray code. That is: int fn(int x) { int y = gray_to_binary(x); …
sh1
  • 4,324
  • 17
  • 30
12
votes
8 answers

How do I find next bit to change in a Gray code in constant time?

I have a small 8-bit processor which has a N-to-M decoder on some output lines - eg, for the 5 to 32 bit case, I write 00101 and bit 5 changes state. The only interface to the output is change-state, there is no read-back. The device counts…
frLich
  • 163
  • 1
  • 5
9
votes
4 answers

the nth gray code

the formula for calculating nth gray code is : (n-1) XOR (floor((n-1)/2)) (Source: wikipedia) I encoded it as: int gray(int n) { n--; return n ^ (n >> 1); } Can someone explain how the above formula works, or possibly its deriviation?
sud03r
  • 19,109
  • 16
  • 77
  • 96
9
votes
6 answers

Generating gray codes.

I tried generating gray codes in Python. This code works correctly. The issue is that I am initialising the base case (n=1,[0,1]) in the main function and passing it to gray_code function to compute the rest. I want to generate all the gray codes…
sagar_jeevan
  • 761
  • 5
  • 16
  • 34
8
votes
2 answers

Generating long-run Gray codes

For a communication system, I need a special kind of gray codes. The requirements are: Two successive values differ in only one bit, like all gray codes. Two transitions on the same bit should be at least distant of some arbitrary number of values.…
pserra
  • 206
  • 1
  • 11
7
votes
1 answer

Structured Light - How to do when the projector's resolution is lower than patterns?

I try to build a structured light environment to do 3D scanning. As far as I know, if I choose to use gray code to reconstruct a 3D model, I have to implement specific patterns that were encode in power 2(2^x, x = 0 ~ 10). That is said, the…
APU
  • 239
  • 2
  • 17
7
votes
5 answers

Gray code in .NET

Is there a built in Gray code datatype anywhere in the .NET framework? Or conversion utility between Gray and binary? I could do it myself, but if the wheel has already been invented...
tbischel
  • 6,337
  • 11
  • 51
  • 73
6
votes
2 answers

Does Gray code exist for other bases than two?

Just a matter of curiosity, is the Gray code defined for bases other than base two? I tried to count in base 3, writing consecutive values paying attention to change only one trit at a time. I've been able to enumerate all the values up to 26…
Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
6
votes
9 answers

How to find if two numbers are consecutive numbers in gray code sequence

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…
user3923643
  • 113
  • 1
  • 2
  • 8
5
votes
1 answer

Why is gray code called reflected code?

I understand that each gray code differs from its preceding code by one bit, but i don't exactly understand why its called reflected. I came across this website https://www.pc-control.co.uk/gray_code.htm, where it says " The gray code is sometimes…
lordvcs
  • 2,466
  • 3
  • 28
  • 37
5
votes
2 answers

Algorithm for generating "anti-Gray" on-demand combinations of k elements from n

I'm trying to implement an algorithm to get all combinations of k elements out of a set of n elements where the difference between two consecutive combinations are maximized (so kind of reverse Gray codes). In other words, the combinations should be…
JHH
  • 8,567
  • 8
  • 47
  • 91
4
votes
1 answer

What are the benefits of Gray code in evolutionary computation?

Books and tutorials on genetic algorithms explain that encoding an integer in a binary genome using Gray code is often better than using standard base 2. The reason given is that a change of +1 or -1 in the encoded integer, requires only one bit…
4
votes
2 answers

how to convert a negative binary number to its gray code

I have negative binary number which has a sign bit and want to write a program to get its gray code. However, I can only find the solution for positive number. So here I am asking this question. Thanks.
1
2 3 4 5 6