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

Day of Week according to Date

We have 7 days in a week. And the first Day of the Date System is Sunday . And it should be. So, 01.01.0001, i.e., First Date of Christ was Sunday. We just number the days as follows. 0 = Sunday 1 = Monday 2 = Tuesday 3 = Wednessday 4 = Thursday 5 = Friday 6 = Saturday So, we can handle every day by the reminder of 7. Cause, u just see, after every 7 days the same day is repeated. Then, lets a rough Calculation. 01/01/0001 => Sunday = 0 Now, we just try to find out the day on 1st January on next year, i.e., 01/01/0002 0001 has 365 days. So, after 365 days which day will come? To find out the answer of this question we have to perform following calculation. 7 ) 365 ( 52 35 ----- 15 14 ---- 1 The reminder is 1, i.e.,Monday. But, using this method, it is very critical to find out the day of 1st January of 1988. So, we have an another idea. Find out how many Leap years are present before current year. Suppose it is L . So, L = y/4 If Current year is...

Class Point:: It holds co-ordinates of a point in format (x,y)

A class Named "Point" which stores the Co-ordinates of a point in (x,y) Format. This Class Contains:: O Constructor with default arguments O Copy Constructor O Function to display which Co-ordinate a Point belongs to O Function to get value of x of a point O Function to get value of y of a point O Function to get the Radious Vector (r) of a point O Function to get the Theta of a point O Overloaded Operator - to find out distance between two points O Overloaded Operator >> to take input O Overloaded operator O Destructor #include<iostream.h> #include<math.h> class point{ //Data Members int x,y; //They hold the value of X and Y co-ordinate of a Point. public: //Member Functions point(int t1=0, int t2=0) //Constructor with default arguments. { x=t1; y=t2; } point(point &t) //Copy Constructor { x=t.x; y=t.y; } int coordinate(void); //It returns the point's Co-ordinate, e.g., 1, 2, 3, or 4th. int getx(...