Now here I have provided the code for binary to gray code. Like for example if you give the input the output of given will be displayed while running the program. Using C++ Programing Language
Definition for N-Bit Gray Code Sequences:
1) The reflected binary code or Gray code is an ordering of the binary numeral system such that two successive values differ in only one bit (binary digit).
2) This cyclic variable code means every transition from one value to the next value involves only one-bit change.
Algorithm to generate N Bit Gray Code list:
1) Start.
2) Now Let '(n-1)' be the length of the gray code lists that has m elements, [(m=2^(n-1))].
3) In this step let us create a list called 'LIST A' by adding the prefix element '0' to all the 'm' elements in the 'LIST A'.
4) In this step let us create a list called 'LIST B' by adding the prefix element '1' to all the 'm' elements in the 'LIST B'.
5) Now the newest 'LIST C' for n-bit gray code is = 'LIST A + reverse(LIST C)'.
6) Now the newest 'LIST C'size is [2m (2^n)].
7) Stop.
NOTE:
For all the n bit gray code lists, we start with the list of length 1 and the elements are only '0' and '1'.
Example for the algorithm:
For n=1
List: [ 0, 1 ]
For n=2
Add prefix '0' to create list A
List A= [ 00, 01 ]
Add prefix '1' to create list B
List B= [ 10, 11 ]
New list C = list A + reverse(list B)
=[ 00, 01, 11, 10 ] //for n=2
For n=3
Add prefix '0' to create list D
List D= 000, 001, 011, 010
Add prefix '1' to create list E
List E= 100, 101, 111, 110
New list F=list D + reverse(list E)
=[000, 001, 011, 010, 110, 111, 101, 100 ]//for n=3
For n=4
Add prefix '0' to create list G
List D= 0000, 0001, 0011, 0010 ,0110 ,0111 , 0101, 0100
Add prefix '1' to create list H
List E= 1100, 1101, 1111, 1110, 1010,,1011, 1001, 1000
New list I=list G + reverse(list H)
=[0000, 0001, 0011, 0010, 0110, 0111, 0101,0100,1100,1101,1111,1110,1010,1011,1001,1000 ]
Submitted by Srivenurajulu G (Srivenurajulu)
Download packets of source code on Coders Packet
Comments