## 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.

## A3:

Minimized expression from the above k map,
$A_3 = B_3$

## A2:

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:

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:

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$

## Hexadecimal to Decimal Conversion Code C

Sample Input:

F7B

Sample Output:

3963

CODE:

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

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

gets(bin);
int len = strlen(bin);
x = len-1;

for(int k=0; k<len; k++){
if(bin[k]-'0'>=0 && bin[k]-'0'<=9){
sum += (bin[k]-'0') * pow(16, x);
--x;
}else if(bin[k]>='A' && bin[k]<='F'){
sum += (bin[k]-55) * pow(16, x);
--x;
}else{
printf("Invalid input");
return 0;
}
}

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



## Octal to Decimal Conversion Code C

Sample Input:

147

Sample Output:

103

CODE:

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

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

gets(bin);
int len = strlen(bin);
x = len-1;

for(int k=0; k<len; k++){
if(bin[k]-'0'>=0 && bin[k]-'0'<=7){
sum += (bin[k]-'0') * pow(8, x);
--x;
}else
valid = false;
}

if(valid)
printf("%d", sum);

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;
}