Skip to main content

Binary and Gray Code Interchange

V can convert ny Binary Number in2 its Gray Code @ nytime.V dn't need 2 start 4m 0. Da algo is:

    Binary to Gray Code
  • Start 4m MSB (Left side of the Number)
  • g(MSB)=b(MSB)
  • g(ith bit)=b(ith bit) XOR b(i+1 th bit)
As an Example:
Binary= 1 0 0 0 1 1 0
        |\|\|\|\|\|\|
Gray  = 1 1 0 0 1 0 1
    Gray Code to Binary
  • Start from MSB
  • b(MSB)=g(MSB)
  • b(ith bit)=g(ith bit) if the number of 1's preceding g(ith bit) is Even
    else b(ith bit)=NOT of g(ith bit)
As an Example:
Gray1100101
Binary 1 0 0 0 1 1 0
Comments MSB Prec 1 is odd Prec 1 is EvenPrec 1 is EvenPrec 1 is EvenPrec 1 is oddPrec 1 is Odd

Here is a Conversion Program::
// Header Files Inclusion 
#include<stdio.h>
#include<conio.h>
#include<string.h>

// User defined Functions Prototype List
void printcenter(char *s,int c);       // Function to print text in Center of Screen
void printtabbed(char *s,int t,int c); // Function to print text with preceding space from the left
void bintogray(char *t, char *f);      // Function to Convert Binary Number into Gray Code
void graytobin(char *t, char *f);      // Function to Convert Graycoded Number into Binary
void getinput(char *t);                // Function to take input in formatted way

// Main Function definition
void main()
{
 char f[65], t[65];
 char ch;
 int i;
textmode(C80);
textbackground(3);
_setcursortype(_NOCURSOR);
 do{
 clrscr();
 textbackground(RED);
 printcenter("\nBINARY and GRAY Code Interchanger", BROWN);
 textbackground(3);
 printcenter("\n-=Copy Righted by Arindam Roy=-",DARKGRAY);
 printcenter("\nThis can process 64 bit numbers", YELLOW);
 printtabbed("\n1. Binary -> Gray", 3, LIGHTBLUE);
 printtabbed("\n2. Gray -> Binary", 3, LIGHTBLUE);
 printtabbed("\n3. Exit", 3, LIGHTBLUE);
 printcenter("\n<Enter Your Choice>", LIGHTGREEN+BLINK);
 do
 ch=getch();
 while(ch<'1'||ch>'3');
 if(ch=='1') printcenter("\nBinary -> Gray", LIGHTMAGENTA);
 if(ch=='2') printcenter("\nGray -> Binary", LIGHTMAGENTA);
 if(ch=='3')
  break;
 do{
 printcenter("\n<Enter The Number>", LIGHTGREEN+BLINK);
 printcenter("\n", BLACK);
 getinput(t);
 i=strlen(t)-1;
 for(; i>=0;i--)
  if(t[i]!='0' && t[i]!='1')
   {
    i=66;
    break;
   }
 if(i==66)
 printcenter("\n:::Invalid Entry. Reenter the Number:::", RED);
 }while(i==66);

 if(ch=='1')
  bintogray(t,f);
 if(ch=='2')
  graytobin(t,f);
 printcenter("\n The Output \n", MAGENTA+BLINK);
 printcenter(f, BLACK);
 printcenter("\n\n Do you want to Process another Number? (y/n)", RED);
 do ch=getch(); while(toupper(ch)!='Y' && toupper(ch)!='N');
}while (toupper(ch)=='Y');

_setcursortype(_NORMALCURSOR);
textcolor(7);
textbackground(0);
clrscr();
}


// User defined Funcions' Definitions 

void printcenter(char *s, int c)
{
 int l;
 l=strlen(s);
 l=(80-l)/2;
 gotoxy(l,wherey());
 textcolor(c);
 cprintf("%s",s);
 textcolor(BLACK);
}


void printtabbed(char *s, int t, int c)
{
 gotoxy(8*t, wherey());
 textcolor(c);
 cprintf("%s",s);
 textcolor(BLACK);
}

void bintogray(char *t, char *f)
{
 int i,l;
 l=strlen(t);
 f[0]=t[0];
 for(i=1;i<l;i++)
 {
  if(t[i]==t[i-1])
   f[i]='0';
  else
   f[i]='1';
 }
 f[i]='\0';
}

void graytobin(char *t, char *f)
{
 int one=0,i;
 f[0]=t[0];
 if(f[0]=='1') one=1;
 for(i=1; t[i]; i++)
  {
   if(!(one%2)) f[i]=t[i];
   else f[i]=('1'-t[i])+48;
   if(t[i]=='1') one++;
  }
 f[i]='\0';
}

void getinput(char *t)
{
 int i=0;
 do{
 t[i]=getch();
 if(t[i]=='\r'&& i) break;
 t[i+1]='\0';
 printcenter(t,BLACK);
 i++;
 }while(i<65);
 t[i]='\0';
}


Comments

Popular posts from this blog

2's Complement of 2Byte Number

This program will work out the 2's Complement of a 2 Byte Number. As Microprocessor can perform 8 bits at a time in Accumulator, so we follow the foloowing Algo Fetch the 2 Byte Number in a Register Pair Bring Least Significant Byte of the Number from last Register into A (Accumulator) Make Complement of A and add 1 Store back the Updated LSByte into its source Register Bring MSByte of the Number from another Register into A Make Complement of A and add 1 if carry was genetrated at the time of LSByte opeartion Store back the Updated MSByte into its source Register Store the Result to a Memory location Suppose, Argument Number is stored in Location 1000H and 1001H Memory Location and store the value in 2000H and 2001H Memory Locations. Memory Location Hex Code Label Mnemonics Comment 3050 3051 3052 2A 00 10 START: LHLD 1000H ;Load the HL Register Pair with the content of Memory Location 1000H and 1001H 3053 7D MOV A, L ;Move the Content of L to A ( Accumulator ) 3054 2F

Flip-Flop Conversion

Flip-Flop is a memory element. You can say a basic element. It is a circuit that can hold a binary state (0 or 1 as low or high volts) until an input to change that state. We have many flip-flops as SR Flip-Flop (Set Reset) D Flip-Flop (Data) JK Flip-Flop T Flip-Flop (Toggle) Those Flip-Flops are available in market in IC Package Form. Now You have a circuit that uses 1 JK and 1 D Flip-Flop and 3 NOT Gates. So you have to buy three ICs- one for JK, one for D and other for NOT Gate. Ultimately you will see that a flip flop in IC of JK, a flip-flop in D IC and 3 NOT gates are not used. Now, if you convert a JK flip flop into D flip flop then you didn’t have to pay for the D flip flop IC. So, your circuit will be made by 2 ICs in place of 3 and in cheap cost. This article is for that reason. How the Flip-Flops are converted from one to other. Procedure: 1. Write Qt and Qt+1 Table with 4 combination of 0 and 1. 2. Write columns for inputs of destination Flop-Fl op. 3. Write the ce

How to set Set-Top Box to Resume automatically to last Channel after switch on?

How to set customised automatic Resume Channel to last viewed in Set-Top Box?  Introduction In the era of Digitally fast growing life WiFi enabled smart devices and also voice controlled smart devices are trying to be companion of every human being. But, all human do not change their habit at the same time. It may be cause of mental attachment or financial limits or unwillingness or though as unnecessary. What ever the reason may be, there exists the old technology, like IR(Infrared), RF(Radio Frequency), etc. So, this article for them who are using "Un-smart" TV like me in this time of Smart World. I was searching for a solution to set my Set-top Box of Videocon DTH (controlled by a RF Remote) to resume the last viewed channel when I power on my TV anytime. Suppose, I was watching NAT-Geo and switch off my (Un-smart) TV, even switch off the plug power at board. Now, after some time / on the next day, when I switch on my TV it will directly tune to the last viewed channel, i.