Digital Logic: Binary to Gray Code Converter

Before starting with this read Digital Logic: Designing Decimal to 4 bit Gray Code Converter if you are unfamiliar.


Binary, Decimal and Gray Code Table:

Here I am representing gray code bits with A, Decimal values with D and Binary bits with B.

Binary Decimal Gray Code
B3 B2 B1 B0 D A3 A2 A1 A0
0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 1
0 0 1 0 2 0 0 1 1
0 0 1 1 3 0 0 1 0
0 1 0 0 4 0 1 1 0
0 1 0 1 5 0 1 1 1
0 1 1 0  6 0 1 0 1
0 1 1 1 7 0 1 0 0
1 0 0 0 8 1 1 0 0
1 0 0 1 9 1 1 0 1
1 0 1 0 10 1 1 1 1
1 0 1 1 11 1 1 1 0
1 1 0 0 12 1 0 1 0
1 1 0 1 13 1 0 1 1
1 1 1 0 14 1 0 0 1
1 1 1 1 15 1 0 0 0
Binary to Gray Code Converter:

This same technique can be applied to make gray to binary converter. There will be 4 input bits, which represent binary and 4 output bits which represent equivalent gray code.

Since we are creating binary to gray code converter so, we need to find expressions for each gray code output in terms of input binary bits.

So, there will be four output bits A_3, A_2, A_1, A_0 . For these output bits the input will be different combinations of B_3, B_2, B_1, B_0 based on minimized expression.


Karnaugh Maps:

A3:

4 bit binary to gray code A3 expression
4 bit binary to gray code A3 expression

Minimized expression from the above k map,
A_3 = B_3


A2:

4 bit binary to gray code A2 k map
4 bit binary to gray code A2 k map

Minimized expression from the above k map,
A_2 = B_3 \overline{B_2} + B_2 \overline{B_3}

But, the expression of XOR Gate is (Let, A and B be the inputs),
A.\overline{B} + B.\overline{A} = A \oplus B
[Note: This is not related to this problem but, only showing how XOR can be made from And, Or, Not gates]

Similarly,it can be shown that,
B_3 \overline{B_2} + B_2 \overline{B_3} = B_2 \oplus B_3

If XOR gate is not available then the former expression can be used with and, or and not gates to design the gray code converter.


A1:

4 bit binary to gray code A1 k map
4 bit binary to gray code A1 k map

Minimized expression from the above k map,
A_1 = B_1 \overline{B_2} + B_2 \overline{B_1}

Similarly,it can be shown that,
B_1 \overline{B_2} + B_2 \overline{B_1} = B_1 \oplus B_2


A0:

4 bit binary to gray code A0 k map
4 bit binary to gray code A0 k map

Minimized expression from the above k map,
A_0 = B_0 \overline{B_1} + B_1 \overline{B_0}

Similarly,it can be shown that,
B_0 \overline{B_1} + B_1 \overline{B_0} = B_0 \oplus B_1


Circuit Diagram using XOR gates:

Download the logisim circ file from github.

4 bit binary to Gray Code Converter using XOR gates
4 bit binary to Gray Code Converter using XOR gates

Circuit Diagram without using XOR gates:

Download the logisim circ file from github.

4 bit binary to Gray Code Converter without using XOR gates
4 bit binary to Gray Code Converter without using XOR gates

Converting Case using Binary and Hexadecimal values

We can use the same logic for octal numbers. Also using the binary logic below we can convert lower case letters to upper case letters. For reference, Chapter 1 of the book Digital logic ( Author M. Morris Mano ) may be helpful.


Capital to Small Case Using Binary Values:

/**
 * @author  Quickgrid ( Asif Ahmed )
 * @link    https://quickgrid.wordpress.com
 * Problem: Usage of Binary Values
 */

#include<cstdio>

int main () {
    /*
     * We represent binary ( Base 2, 0 and 1 ) with prefix, 0b
     */
    register unsigned i = 0b0;

    /*
     * ASCII code for capital A
     * Explanation:
     *  1*2^6 + 0*2^5 + 0*2^4 + 0*2^3 + 0*2^2 + 0*2^1 + 1*2^0
     * =   64 +     0 +     0 +     0 +     0 +     0 +     1
     * =   65
     */
    int A = 0b1000001;

    /*
     * ASCII code for line feed
     */
    int n = 0b0001010;

    /*
     * The trick here is that difference between An ASCII Code for a capital
     * and small letter is always 32. So if we can toggle that bit representing
     * 32 then we can convert capital to small letter and vice versa.
     */
    while (i < 0b0011010){
        /*
         * Since Capital letter differ in the bit representing 32 bit
         * We can just bitwise OR it with any capital letter and get
         * its small letter representation.
         * We know from truth table of OR that
         * 0 OR 1 = 1, meaning our bit representing 32 is active
         */
        int a = 0b0100000 | A;

        /*
         * Here A starts from 'A' and goes to 'Z'
         * Here n represents new line
         */
        printf("Capital: %c%c", A++, n);
        printf("Small: %c%c", a, n);

        ++i;
    }

    return 0;
}

 

Small and Capital Case Using Hexadecimal Values:

/**
 * @author  Quickgrid ( Asif Ahmed )
 * @link    https://quickgrid.wordpress.com
 * Problem: Usage of Hexadecimal Values
 */

#include<cstdio>

int main () {
    /*
     * We represent hexadecimal ( Base 16, from 0 to F total of 16 ) with prefix, 0x
     */
    register unsigned i = 0x0;

    /*
     * ASCII code for capital A
     * Explanation:
     *  6*16^1 + 1*16^0
     * =    96 +      1
     * =    97
     */
    int A = 0x61;

    /*
     * ASCII code for line feed
     */
    int n = 0xA;
    int small_a = 0x41;

    while (i < 0x1A){
        int a = small_a++;

        /*
         * Here A starts from 'A' and goes to 'Z'
         * Here n represents new line
         */
        printf("Small: %c%c", A++, n);
        printf("capital: %c%c", a, n);

        ++i;
    }

    return 0;
}

Binary to Decimal Converter Code C

Sample Input:

11111

Sample Output:

31

CODE:

#include<stdio.h>
#include<string.h>
#include<math.h>

int main(){
   int sum,x;
   sum = x = 0;
   char bin[100]={'\0'};

   printf("Enter binary digit:\n");
   gets(bin);
   int len = strlen(bin);

   for(int k=len-1; k>=0; k--){
           if(bin[k]-'0'==1 || bin[k]-'0'==0){
           sum += (bin[k]-'0') * pow(2, x);
           ++x;
       }else{
           printf("\nError\n");
           return 0;
       }
   }

   printf("Decimal: %d", sum);
   return 0;
}