Gray code

Gray code is a binary numeral system where two successive values differ in only one bit.

For example, the sequence of Gray codes for 3-bit numbers is: 000, 001, 011, 010, 110, 111, 101, 100, so G(4)=6G(4)=6.

This code was invented by Frank Gray in 1953.

1. Finding Gray code

Let’s look at the bits of number n and the bits of number G(n). Notice that i-th bit of G(n) equals 1 only when i-th bit of n equals 1 and i+1-th bit equals 0 or the other way around (i-th bit equals 0 and i+1-th bit equals 1). Thus, G(n)=n(n>>1):

1
2
3
int g (int n) {
    return n ^ (n >> 1);
}

2. Finding inverse Gray code

Given Gray code g, restore the original number n.

We will move from the most significant bits to the least significant ones (the least significant bit has index 1 and the most significant bit has index k). The relation between the bits ni of number n and the bits gi of number g:

nk=gk,nk1=gk1nk=gkgk1,nk2=gk2nk1=gkgk1gk2,nk3=gk3nk2=gkgk1gk2gk3,

The easiest way to write it in code is:

1
2
3
4
5
6
int rev_g (int g) {
  int n = 0;
  for (; g; g >>= 1)
    n ^= g;
  return n;
}

3. Practical applications

Gray codes have some useful applications, sometimes quite unexpected:

  • Gray code of n bits forms a Hamiltonian cycle on a hypercube, where each bit corresponds to one dimension.
  • Gray codes are used to minimize the errors in digital-to-analog signals conversion (for example, in sensors).
  • Gray code can be used to solve the Towers of Hanoi problem. Let n denote number of disks. Start with Gray code of length n which consists of all zeroes (G(0)) and move between consecutive Gray codes (from G(i) to G(i+1)). Let i-th bit of current Gray code represent n-th disk (the least significant bit corresponds to the smallest disk and the most significant bit to the biggest disk). Since exactly one bit changes on each step, we can treat changing i-th bit as moving i-th disk. Notice that there is exactly one move option for each disk (except the smallest one) on each step (except start and finish positions). There are always two move options for the smallest disk but there is a strategy which will always lead to answer: if n is odd then sequence of the smallest disk moves looks like ftrftr where f is the initial rod, t is the terminal rod and r is the remaining rod), and if n is even: frtfrt.
  • Gray codes are also used in genetic algorithms theory.

4. Practice Problems

20 Trackbacks / Pingbacks

  1. meritroyalbet
  2. canadian pharmacy viagra brand
  3. meritroyalbet
  4. canadian pharmacies that ship to us
  5. eurocasino
  6. buy viagra online usa
  7. https://avuiom.sellfy.store/
  8. canadian pharmacycanadian pharmacy
  9. kwersd.mystrikingly.com
  10. https://gewsd.estranky.sk/clanky/drugstore-online.html
  11. kqwsh.wordpress.com20220516what-everybody-else-does-when-it-comes-to-online-pharmacies
  12. site592154748.fo.team
  13. safe canadian online pharmacies
  14. http://aonubs.website2.me/
  15. https://dkyubn.bizwebs.com/
  16. https://asebg.bigcartel.com/canadian-pharmacy
  17. medicine-online.estranky.skclankyunderstand-covid-19-and-know-the-tricks-to-avoid-it-from-spreading-----medical-services.html
  18. https://kertvbs.webgarden.com/
  19. https://disvaiza.mystrikingly.com/
  20. https://swenqw.company.site/

Comments are closed.