Showing posts with label number system. Show all posts
Showing posts with label number system. Show all posts

Saturday, 20 July 2013



Gray code

Gray Code Wheel

“The reflected binary code is called Gray code after the name of Frank Gray.”
·         4 bit code.
·         Contains 0-15 decimal numbers.
Decimal
Binary
Gray code
0
0000
0000
1
0001
0001
2
0010
0011
3
0011
0010
4
0100
0110
5
0101
0111
6
0110
0101
7
0111
0100
8
1000
1100
9
1001
1101
10
1010
1111
11
1011
1110
12
1100
1010
13
1101
1011
14
1110
1001
15
1111
1000

   Binary to Gray code conversion

To convert a binary number to Gray code, apply following steps;

Step 1:
 left most bit of gray code and its corresponding binary code is same.
Step2:
going from left to right, add each adjacent pair of binary digit to get the next gray code digit, regardless carries.
e.g convert binary number (0100)2 to gray code.
Step1:
             0    1    0    0       (binary code of 4)
             ↓
             0
Step2:
            0 +  1    0     0
            ↓      ↓
            0      1
Step3:
             0      1   +   0     0
             ↓       ↓       ↓
             0      1        1
Step4:
             0       1       0  +   0
             ↓       ↓        ↓        ↓
             0       1        1       0     (Gray code)

 Gray code to binary conversion

It has following steps.
Step1:
 write down left most bit of gray code as it is same in binary code.
Step2:
 going from left to write, add last single bit of binary into next bit of gray code then write down the answer and continue till last bit.
e.g convert gray code (0101) into binary code.
Step1:
             0     1     0    1
             ↓
             0
Step2:
             0     1      0      1
             ↓ +  ↓
             0           1

Steo3:
                   0       1          0       1
                   ↓        ↓    +    ↓
                   0        1          1
Step4:
                  0       1       0       1
                  ↓       ↓        ↓   +  ↓

                  0        1       1       0      

BCD (Binary Coded Decimal)

·         It represents 0 to 9 digits.
·         4-bits binary = 1-bit BCD.
·         Invalid BCD numbers are 1010, 1011, 1100, 1101, 1110 and 1111.

   Decimal Digit BCD

   0          1         2          3          4          5          6          7          8          9         (decimal number)
0000   0001   0010   0011   0100    0101   0110   0111   1000   1001      (BCD code)

  Convert BCD code into Decimal number

(10000110)2
Step 1: break the given number in 4, 4-bit.
1000   0110
Step 2: convert these 4-bit into their respective decimal numbers.
1000   0110
  8           6
Given BCD is (86)10 in Decimal number.


  Convert Decimal  number into BCD code

(453)10
Step 1: convert each decimal digit into 4-bit of binary number.
   4           5          3
0100    0101    0011
Step 2: join these binary digits.
(010001010011)2

 BCD Addition

   23      →   00100011        
+15      →    00010101
   38      →   00111000

  BCD Subtraction

86-41=?
41 → 01000001 → 2’s compliment → 10111111 → -41
  86 →   10000110
-41 →   10111111
 451 01000101
Here  left most 1 is discarded.

Note:


If answer of addition or subtraction of BCD numbers is invalid, then add +6 or -6 respectively to their answers to get valid answer.



Gray code


 Here I'm telling you a direct method to write Gray code.
Gray code is the reflection of decimal number. And reflection is always in reversed of original.
Gray code writing depends on 2ᴺ.

  For N=1 

Here we discus 0 and 1 number only.
Write 0 and 1 in vertical line in decimal.
Decimal             Gray code
   0              →              0
   1         →              1

It shows that gray code of 0 is 0 and gray code of 1 is 1.



  For N=2

Here we discus 0,1,2,3 numbers only. Follow these steps.

a.       Write 0,1,2 and 3 in vertical line in decimal.
Decimal
0
1
2
3

b.      For every power of N remember its previous power to write the exact gray code.
c.       Copy gray code of power N=1 in vertical line in front of step a.
Decimal            Gray code
0           →                0
1           →                1
2
3

d.      Copy the reverse of step a in step c in front of 2 and 3 decimal number.
Decimal            Gray code
0          →             0
1          →             1
2          →             1
3          →             0

e.      Now write 0 with original gray code of N=1 and write 1 with reversed gray code of N=1 on left side.

Decimal            Gray code
0         →                 0  0
1         →                 0  1
2         →                 1  1
3         →                 1  0

This is the decimal to gray code conversion.

Note:
For N=3,4,5,6,.......... so on
The same steps will be followed as mentioned above.
For N=2 the remembering power is N=1.
For N=3 the remembering power is N=2.
For N=4 the remembering power is N=3.
For N=5 the remembering power is N=4.
And so on...........