[Gray Code], Gray Code

Source: Internet
Author: User

[Gray Code], Gray Code

/* Gray Code Description: Gray Code is a set of series. Each number is expressed by binary. Assume that n-bit elements are used to indicate that each number is better. Only one element value is different between any two numbers, for example, the Gray Code of the following three bits: 000 001 011 010 110 111 101 100 is defined by the user. The order of the Gray Code is not unique. For example, the preceding sequence is written in reverse order, it is also a set of GrayCode: 100 101 111 110 010 011 001 000 Gray Code, which was proposed by Frank Gray of Bell Labs in the 1940 s and used in the use of PCM (Pusle CodeModulation) method to avoid errors when sending signals, and obtained a US patent in March 17, 1953. Solution: because Gray Code only changes one element between two adjacent numbers, we can observe the position where Gray Code changes from 1 to 0 or from 0 to 1, assume that the Gray Code of 4 bits is as follows: 0000 0001 0011 0010 0110 0111 0101 1000 1100 1101 1111 1110 1010 1011 1001 1000 when observing the change of the odd number of items, we found that no matter how many Gray codes it is, it will always change only the rightmost bits. If it is 1, it will be changed to 0. If it is 0, it will be changed to 1. When we observe the change of the even number, we find that the changed bitwise element is the left-side element of the first 1 calculated from the right side. The above two change rules are fixed, regardless of the number of BITs; so as long as the location of the bits is an odd or even number, you can decide which bits to change, for the convenience of programming, array index 0 is regarded as the rightmost value, and when printing the result, the reverse printing starts from the large index number. Taking the Gray Code of two bits as a plane coordinate, a quadrilateral can be formed. You can find that starting from any vertex, a circle is taken around the perimeter of the Quadrilateral, the vertex coordinates are a set of Gray codes, so you can get four sets of graycodes. Similarly, if we look at the Gray Code of the three-digit element as a plane coordinate, we can form a cube. If you can start from any vertex, we will walk all the sides long, if the vertex is not repeated, the combination of the vertex coordinate sequence is a set of Gray Codes. */# Include <stdio. h> # include <stdlib. h> # define MAXBIT 20 # define TRUE 1 # define CHANGE_BIT (x) x = (x) = '0 '? '1': '0') # define NEXT (x) x = (1-(x) int main (void) {char digit [MAXBIT]; int I, bits, odd; printf ("input number of bits:"); scanf ("% d", & bits); for (I = 0; I <bits; I ++) {digit [I] = '0'; printf ("0");} printf ("\ n"); odd = TRUE; while (1) {if (odd) {CHANGE_BIT (digit [0]);} else {for (I = 0; I <bits & digit [I] = '0'; I ++ ); if (I = bits-1) {break;} CHANGE_BIT (digit [I + 1]);} for (I = bits-1; I> = 0; I --) {printf ("% c", digit [I]);} printf ("\ n"); NEXT (odd);} return 0 ;}

 

Related Article

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.