Gray Code Generation

Source: Internet
Author: User

I recently started to learn about taocp 4th, Volume 1, and book 2nd. I first introduced the gray binary code and found it very interesting. I wrote a small program to generate the Gray code by referring to the algorithm in the book. Before starting, let's take a look at what is a gray binary code. The gray binary code is a simple and regular arrangement of all 2 ^ N-bit binary strings, that is, only one binary bit is changed at a time. For example, for n = 4, the gray binary code is: 0000,000 1, 0011,001 0, 0110,011 1, 0101,010 0, 1100,110 1, 1111,111 0, 1010,101 1, 1001,100 0, the gray binary code is very useful when converting analog and digital information, but I am not sure how to use it. Let's just look at how to generate the corresponding gray binary code for any given n. The book provides a simple rule for generating gray binary codes. If gamma n represents the gray binary code of N-bit binary strings, We Can recursively define the following two rules, 1 gamma Nr here ε represents an empty string, 0 gamma n represents the sequence obtained by adding 0 to the front of each string in the Gamma n sequence, Gamma Nr represents the Inverse Order of the Gamma n sequence, 1 gamma Nr indicates the sequence obtained by adding 1 to the front of each string in the Gamma Nr sequence. Because the last string of Gamma N is equal to the first string of the Gamma NR, it is obvious that if the Gamma n satisfies the nature of the gray code, the gamma n + 1 is also satisfied. Since only one binary bit is changed at a time, if we start to calculate the Gray code from all 0 strings, we will certainly get the numbers 1, 3, 5, 7 ,...... Each string has an even number of 1 bits, while the numbers are 2, 4, 6, 8 ,...... Each string has an odd number of 1 characters. After some inferences, the book provides an algorithm for generating the gray binary code: starting from a full 0 string, the first string is a full 0 string; if the current string has an even number of digits as 1, the next string is the result of reversing the delimiter of the current string. If the odd digit of the current string is 1, the first delimiter of the current string is found, the next string is obtained by reversing the first bit of this bit. The previous steps are cyclically performed until the n-bit binary string overflows. The corresponding program code is as follows:
# Include <iostream> # include <cstdlib> using namespace STD; void gray (int n) {unsigned long G = 0; unsigned long max = (unsigned long) (1l) <n; do {cout <G <"; G ^ = 1; cout <G <"; G ^ = (G ^ (G-1 )) + 1 ;}while (G <max) ;}int main (INT argc, char * argv []) {If (argc! = 2) {cerr <"Usage:" <argv [0] <"<n>/N"; return 1 ;} int n = atoi (argv [1]); If (n <1 | n> 30) {cerr <n <"is too big or too small/N "; return 2;} gray (n); Return 0 ;}
Because 32-bit machines are used, I limit n to less than 30. The main () function does not need to be said. The main algorithm is the gray () function. Variable G is the gray code output each time, and starts from 0. Variable Max is used to detect whether g overflows. Two Gray Codes are generated each time, and the first one has an even number of digits 1, the second is odd digit 1. The string from the string of an even digit 1 to the string of an odd digit 1 is very simple, G ^ = 1; that is, the forward digit is reversed. It is a bit skillful to generate a string with an even number of 1 from a string of odd digit 1. G ^ = (G ^ (G-1) + 1; let me explain. First, assume that the first percentile in G is the second percentile, that is, the second percentile in G is 1, and all bits lower than the second percentile are all 0; then the G-1 can change the I-th bit of G to 0, and all the bits lower than the I-th are changed from 0 to 1; then g compared with the G-1, all bits above I (excluding I bits) are all the same, all bits below I bits (including I bits) are all different, so G ^ (G-1) the result is as follows: all bits above I (excluding I bits) are 0, and all bits below I bits (including I bits) are 1; the addition of 1 gets all strings whose bits I + 1 are 1 and whose bits are 0. Let G be different from this string or, that is, the I + 1 position of G is reversed. Is it a bit complicated? Let's use an example to illustrate how it will be easy to understand. Suppose the current G is 1110, then the G-1 is 1101, the two are different or get 0011, add 1 to get 0100, this number is different from G or eventually get 1010. It is precisely the inverse of the first digit in G. We enter N = 4 for this program and run the following results: 0 1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 because it is not a binary representation, it looks messy. If you convert them to binary, you can see that the output result is correct, which is consistent with the gray binary code example given above.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.