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
3
votes
1 answer

How to obtain this result from a 256 bits monochrome picture?

I just found this image in the book I am studying for Computer Vision (Digital Image Processing by Rafael C. Gonzalez and Richard E. Woods): The book does not explain how to reach this result, and I was wondering how I could reach the same results…
nic
  • 105
  • 6
3
votes
2 answers

Please suggest an algorithm to compare Gray code numbers

I have an absolute encoder which is outputting a 10 bit value (0 to 1023) in Gray code. The problem I am trying to solve is how to figure out if the encoder is moving forwards or backwards. I decided that the “best” algorithm is as follows: first…
Mike
  • 371
  • 1
  • 4
  • 18
3
votes
1 answer

To convert a grayscale image to RGB image using PIL

I am trying a code to convert a grayscale image to a RGB image format in python, but, a TypeError is raised every time I try to execute it. My code is as follows: from PIL import Image path = "bw.jpg" img = Image.open(path) rgb =…
Dhvani Shah
  • 351
  • 1
  • 7
  • 17
3
votes
2 answers

Efficient way to iterate over Gray code change positions

There a number of ways iterating over n-bit Gray codes. Some are more efficient than others. However, I don't actually need the Gray codes and would like instead to iterate over the bit index that is changed in a Gray code list, not the actual…
Simd
  • 19,447
  • 42
  • 136
  • 271
3
votes
1 answer

PHP Gray Code - XOR error

I wrote this function in php to do the Gray Code of a number: function c_gray($num){ $bin=decbin($num); //binary of the number $xor=array(); $xor[]=reset(str_split($bin)); //Get the first bit of binary and put it as the first element of…
adrian b.
  • 33
  • 2
2
votes
1 answer

Gray code pattern in tournament chart?

In a tournament chart from the bottom to the top where there is a winner I've been told that it is somehow connected with the gray-code. I know that the grey code is an alternative code, it's recursive and is useful to find the best solution in…
Micromega
  • 12,486
  • 7
  • 35
  • 72
2
votes
1 answer

k-combination of n items with "Gray code"-like property

I already know how to generate k combinations of n values as described here https://stackoverflow.com/a/28698654/15853075. The problem is that I need one additional property: I want each pair of successive combinations to differ only at one position…
Curious
  • 507
  • 3
  • 16
2
votes
1 answer

Cartesian product in Gray code order with itertools?

Is there something like Python's itertools.product() that provides the iteration through the Cartesian product of a set of sets in Gray code order? For example, supposing that such a hypothetical generator existed, and it was called…
2
votes
4 answers

Is there an accepted way to get a Gray Code counter in Chisel?

I'm looking to write counters in Chisel3 that will be used to address subunits. If the counter matches some register in a subunit then the subunit fires, otherwise it doesn't. I would much rather have the addresses cycle in Gray code than in…
2
votes
3 answers

Gray code to binary conversion

Given a gray code for a number, find the binary code for the number. Gray code is a binary numeral system where two successive values differ in only one bit. For Example two bit gray code is: 0 - 00 1 - 01 2 - 11 3 - 10 Binary is: 0 - 00 1 - 01 2 -…
sunmoon
  • 1,448
  • 1
  • 15
  • 27
2
votes
2 answers

Gray Code Generation - n-bit Gray Codes

The question is asked by hackerrank and i got the solution, but there is a problem in the solution at last test cases. The question is below. 1 <= $n <= 65 Following is 1-bit sequence (n = 1) 0 1 Output 1 Following is 2-bit sequence (n = 2) 00…
Jees K Denny
  • 531
  • 5
  • 27
2
votes
0 answers

Solve Gray code for the intput range 62 < $input <= 65

The following code show the gray code for upto last $input function getGrayCode($n) { return $n ^ ($n >> 1); }; $input = 62; $max = (1 << $input); for( $i= $max- $input ; $i<$max; $i++) { printf("%s\n", decbin(getGrayCode($i))); } The…
2
votes
2 answers

An algorithm using bit flips to iterate over all numbers with k bits

I am looking for an efficient way to iterate over all n bit non-negative integers which have at most k bits set by flipping one bit at a time. What is the minimum number of bit flips I need to do to iterate over all n bit non-negative integers…
Simd
  • 19,447
  • 42
  • 136
  • 271
2
votes
1 answer

Gray code algorithm (32 bit or less)

I have recently come across the Gray code, and I have been quite stuck trying to wrap my head around the efficient algorithm used to convert Gray code back to binary (32 bits or less). num = num ^ (num >> 16); num = num ^ (num >> 8); num = num ^…
EidolonMK
  • 313
  • 2
  • 9
2
votes
3 answers

input 2 integers and get binary, brgc, and hamming distance

I've got everything except hamming distance. I keep getting the error "int() can't convert non-string with explicit base" here is my code: def int2bin(n): if n: bits = [] while n: …
Joe
  • 21
  • 1
  • 4