Your Ad Here

Wednesday, January 23, 2008

RSA Algorithm

/* C Program For The Implementation Of RSA*/

#include<>
#include<>

void main()
{
long int p,q,n,fn,e,s=0,d=1,C[100],M;
char m[100];
unsigned long int b=0,c=0;
int i,j,t;
clrscr();
printf("Enter the value of p and q: ");
scanf("%ld%ld",&p,&q);
n=p*q;
fn=(p-1)*(q-1);
printf("\nEnter the Encryption key e: ");
scanf("%ld",&e);
do
{ s=(d*e)%fn;
d++;
}while(s!=1);
d=d-1;
printf("\n\tPublic key (%ld,%ld) ",e,n);
printf("\n\tPrivate key (%ld,%ld) ",d,n);
printf("\n\nEnter message :\n");
scanf("%s",m);
for(j=0;j< strlen(m);j++)
{
t=(int)m[j];
c=1;
for(i=0;i< e;i++)
c=c*t%n;
c=c%n;
printf("%d ",c);
}
printf("\n\nEnter cipher text :\n");
for(i=0;i< strlen(m);i++)
scanf("%ld",&C[i]);
printf("\n\n\tPlaintext :");

for(j=0;j< strlen(m);j++)
{
M=1;
for(i=0;i< d;i++)
M=M*C[j]%n;
M=M%n;
printf("%c",M);
}
getch();
}
/********************* OUTPUT ***********************
Enter the value of p and q: 7 17

Enter the Encryption key e: 5

Public key (5,119)
Private key (77,119)

Enter message :
Lionel_Noronha
111 56 76 94 33 75 23 57 76 88 76 94 83 20

Enter cipher text :
111 56 76 94 33 75 23 57 76 88 76 94 83 20


Plaintext :Lionel_Noronha
****************************************************/

Run Length Encoding

/* C Program For The Implementation Of RUN LENGTH ENCODING */

#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
int i,j,cnt,l,count[50]={0};
char str[50];
clrscr();
printf("Enter the string: ");
scanf("%s",str);
printf("\n\tOriginal String is: %s",str);
printf("\n\n\tEncoded String is: ");
l = strlen(str);
for(i=0;i< l;i*=1)
{
j = 0;
count[i] = 1;
do
{
j++;
if(str[i+j] == str[i])
count[i]++;
}while(str[i+j]==str[i]);
if(count[i]==1)
printf("%c",str[i++]);
else
{
printf("$%d%c",count[i],str[i]);
i += count[i];
}
}
getch();
}
/************************* OUTPUT ************************
Enter the string: sleepzzzzzzzzzzzzzzzzzzz

Original String is: sleepzzzzzzzzzzzzzzzzzzz

Encoded String is: sl$2ep$19z */

RSA Algorithm

/* C program for the Implementation Of RSA Algorithm */

#include< stdio.h>
#include< conio.h>

int phi,M,n,e,d,C,FLAG;

int check()
{
int i;
for(i=3;e%i==0 && phi%i==0;i+2)
{
FLAG = 1;
return;
}
FLAG = 0;
}

void encrypt()
{
int i;
C = 1;
for(i=0;i< e;i++)
C=C*M%n;
C = C%n;
printf("\n\tEncrypted keyword : %d",C);
}

void decrypt()
{
int i;
M = 1;
for(i=0;i< d;i++)
M=M*C%n;
M = M%n;
printf("\n\tDecrypted keyword : %d",M);
}

void main()
{
int p,q,s;
clrscr();
printf("Enter Two Relatively Prime Numbers\t: ");
scanf("%d%d",&p,&q);
n = p*q;
phi=(p-1)*(q-1);
printf("\n\tF(n)\t= %d",phi);
do
{
printf("\n\nEnter e\t: ");
scanf("%d",&e);
check();
}while(FLAG==1);
d = 1;
do
{
s = (d*e)%phi;
d++;
}while(s!=1);
d = d-1;
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
scanf("%d",&M);
encrypt();
printf("\n\nEnter the Cipher text\t: ");
scanf("%d",&C);
decrypt();
getch();
}

/*************** OUTPUT *****************

Enter Two Relatively Prime Numbers : 7 17

F(n) = 96

Enter e : 5

Public Key : {5,119}
Private Key : {77,119}

Enter The Plain Text : 19

Encrypted keyword : 66

Enter the Cipher text : 66

Decrypted keyword : 19 */

Shanno - Fano Code

/*
The Shannon-Fano Algorithm

This is a basic information theoretic algorithm. A simple example will be used to illustrate the algorithm:


Symbol A B C D E
----------------------------------
Count 15 7 6 6 5

Encoding for the Shannon-Fano Algorithm:


A top-down approach
1. Sort symbols according to their frequencies/probabilities, e.g., ABCDE.

2. Recursively divide into two parts, each with approx. same number of counts.




Symbol Count log(1/p) Code Subtotal (# of bits)
------ ----- -------- --------- --------------------
A 15 1.38 00 30
B 7 2.48 01 14
C 6 2.70 10 12
D 6 2.70 110 18
E 5 2.96 111 15
TOTAL (# of bits): 89

*/

#include< stdio.h>
#include< conio.h>
#include< string.h>
struct node
{
char sym[10];
float pro;
int arr[20];
int top;
}s[20];

typedef struct node node;

void prints(int l,int h,node s[])
{
int i;
for(i=l;i< =h;i++)
{
printf("\n%s\t%f",s[i].sym,s[i].pro);
}
}

void shannon(int l,int h,node s[])
{
float pack1=0,pack2=0,diff1=0,diff2=0;
int i,d,k,j;
if((l+1)==h || l==h || l>h)
{
if(l==h || l>h)
return;
s[h].arr[++(s[h].top)]=0;
s[l].arr[++(s[l].top)]=1;
return;
}
else
{
for(i=l;i< =h-1;i++)
pack1=pack1+s[i].pro;
pack2=pack2+s[h].pro;
diff1=pack1-pack2;
if(diff1< 0)
diff1=diff1*-1;
j=2;
while(j!=h-l+1)
{
k=h-j;
pack1=pack2=0;
for(i=l;i< =k;i++)
pack1=pack1+s[i].pro;
for(i=h;i>k;i--)
pack2=pack2+s[i].pro;
diff2=pack1-pack2;
if(diff2< 0)
diff2=diff2*-1;
if(diff2>=diff1)
break;
diff1=diff2;
j++;
}
k++;
for(i=l;i< =k;i++)
s[i].arr[++(s[i].top)]=1;
for(i=k+1;i< =h;i++)
s[i].arr[++(s[i].top)]=0;
shannon(l,k,s);
shannon(k+1,h,s);
}
}

void main()
{
int n,i,j;
float x,total=0;
char ch[10];
node temp;
clrscr();
printf("Enter How Many Symbols Do You Want To Enter\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("Enter symbol %d ---> ",i+1);
scanf("%s",ch);
strcpy(s[i].sym,ch);
}
for(i=0;i< n;i++)
{
printf("\n\tEnter probability for %s ---> ",s[i].sym);
scanf("%f",&x);
s[i].pro=x;
total=total+s[i].pro;
if(total>1)
{
printf("\t\tThis probability is not possible.Enter new probability");
total=total-s[i].pro;
i--;
}
}
s[i].pro=1-total;
for(j=1;j< =n-1;j++)
{
for(i=0;i< n-1;i++)
{
if((s[i].pro)>(s[i+1].pro))
{
temp.pro=s[i].pro;
strcpy(temp.sym,s[i].sym);
s[i].pro=s[i+1].pro;
strcpy(s[i].sym,s[i+1].sym);
s[i+1].pro=temp.pro;
strcpy(s[i+1].sym,temp.sym);
}
}
}
for(i=0;i< n;i++)
s[i].top=-1;

shannon(0,n-1,s);
printf("---------------------------------------------------------------");
printf("\n\n\n\tSymbol\tProbability\tCode");
for(i=n-1;i>=0;i--)
{
printf("\n\t%s\t%f\t",s[i].sym,s[i].pro);
for(j=0;j< =s[i].top;j++)
printf("%d",s[i].arr[j]);
}
printf("\n---------------------------------------------------------------");
getch();
}

/********************* OUTPUT **************************

Enter How Many Symbols Do You Want To Enter : 6
Enter symbol 1 ---> a
Enter symbol 2 ---> b
Enter symbol 3 ---> c
Enter symbol 4 ---> d
Enter symbol 5 ---> e
Enter symbol 6 ---> f

Enter probability for a ---> 0.3

Enter probability for b ---> 0.25

Enter probability for c ---> 0.20

Enter probability for d ---> 0.12

Enter probability for e ---> 0.08

Enter probability for f ---> 0.05


---------------------------------------------------------------
Symbol Probability Code
a 0.300000 00
b 0.250000 01
c 0.200000 10
d 0.120000 110
e 0.080000 1110
f 0.050000 1111
---------------------------------------------------------------
*/

Convolutional Codes

/* C Porgram To Find convolutional Codes or Code Vectors */

#include< stdio.h>
#include< conio.h>

void main()
{
int i,j,k,n,gb,mb,g[10][10],x[10][10],c[10],m[10],t[10];
clrscr();
for(i=0;i< 10;i++)
{
for(j=0;j< 10;j++)
g[i][j]=0;x[i][j]=0;c[i]=0;m[i]=0;t[i]=0;
}
printf("\t\t\t¦ Convolutional Code ¦");
printf("\n\nEnter The No. Of Generator Sequences\t: ");
scanf("%d",&n);
printf("Enter The No. Of Bits In Each Generator Sequence\t: ");
scanf("%d",&gb);
for(i=0;i< n;i++)
{
printf("\n\nEnter The Values Of G%d\t:\n",i);
for(j=0;j< gb;j++)
scanf("%dt",&g[i][j]);
}
printf("\nEnter The No. Of Message Bits\t: ");
scanf("%d",&mb);
printf("Enter The Message Bits\t:\n");
for(i=0;i< mb;i++)
scanf("%d",&c[i]);
for(i=0;i< mb;i++)
{
for(j=0;j< gb;j++)
{
t[j]=m[j];
if(j==0)
m[j]=c[i];
else
m[j]=t[j-1];
}
for(k=0;k< gb;k++)
{
for(j=0;j< gb;j++)
{
if(g[k][j]==1)
x[k][i]=x[k][i]^m[j];
}
}
}
printf("\n\n¦ The Code Vectors Are: ¦\n");
for(i=0;i< mb;i++)
{
printf("¦ \n¦ ");
for(j=0;j< gb;j++)
printf("%d\t",x[j][i]);
}
getch();
}

/*************** OUTPUT ******************
¦ Convolutional Code ¦

Enter The No. Of Generator Sequences : 2
Enter The No. Of Bits In Each Generator Sequence : 3


Enter The Values Of G0 :
1
1
1


Enter The Values Of G1 :
1
0
1


Enter The No. Of Message Bits : 8
Enter The Message Bits :
1
1
0
1
1
0
1
1


¦ The Code Vectors Are: ¦
¦
¦ 1 1 0 ¦
¦ 0 1 0 ¦
¦ 0 1 0 ¦
¦ 0 0 0 ¦
¦ 0 1 0 ¦
¦ 0 1 0 ¦
¦ 0 0 0 ¦
¦ 0 1 0

*/

Cyclic Code

/* C Program For Implementation Of Cyclic Code */

#include< stdio.h>
#include< conio.h>

void main()
{
int n,k,g[10],C[10],M[10],I,T[10];
int i,j,r;
clrscr();
printf("\t\t\t³ CYCLIC ENCODER ³\n");

printf("\n\nEnter The Value Of n and k\t:\n");
scanf("%d%d",&n,&k);
printf("\nEnter The Generator Polynomial Of Dergee %d",(n-k));
printf(" of the form 1+x+xý+x³+...\n");

for(i=0;i< n-k;i++)
scanf("%d",&g[i]);
printf("\nEnter Msg\n");
for(i=0;i< k;i++)
scanf("%d",&M[i]);
printf("\n\n");

for(i=0;i< n-k;i++)
{
T[i]=0;
}
for(i=0;i< k;i++)
{
I = (M[i] + T[n-k-1])%2;
C[0] = I;

for(j=1;j< n-k;j++)
{
if(g[j]==1)
C[j]=(I + T[j-1])%2;
else
C[j]=T[j-1];

}
for(r=0;r< n-k;r++)
{
T[r]=C[r];
}
}
printf("-----------------------------------------------------");
printf("\n³\tGenerated Cyclic code is\t: ");
for(i=0;i< k;i++)
printf("%d",M[i]);
printf(" ");
for(i=0;i< n-k;i++)
printf("%d",C[i]);
printf("\t\n-------------------------------------------------------");
getch();
}
/********************* OUTPUT *************************
³ CYCLIC ENCODER ³

Enter The Value Of n and k :
7
4

Enter The Generator Polynomial Of Dergee 3 of the form 1+x+xý+x³+...
1
1
0

Enter Msg
1
0
1
0


-------------------------------------------------------
Generated Cyclic code is : 1010 110
-------------------------------------------------------

*/

Entropy Of Given Message

/* C Program to find Entropy Of Given Message */

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

void main()
{
int c,i;
float p,sum=0,it;
clrscr();
printf("**** INFORMATION CONTENT OF FIVE MESSEGES *****\n\n");
printf("Enter No Of Messages : ");
scanf("%d",&c);
printf("\n");
for(i=1;i< =c;i++)
{
printf("\tEnter The %d Probability\t: ",i);
scanf("%f",&p);
it = p* (log(1/p)) / log(2);
sum=sum+it;
}
printf("\nEntropy Of The Given Message : %f",sum);
getch();
}
/******************** OUTPUT *************************

**** INFORMATION CONTENT OF FIVE MESSEGES *****

Enter No Of Messages : 5

Enter The 1 Probability : 0.1
Enter The 2 Probability : 0.2
Enter The 3 Probability : 0.3
Enter The 4 Probability : 0.36
Enter The 5 Probability : 0.04

Entropy Of The Given Message : 2.034038 */

Tuesday, January 22, 2008

Linear Block Code

/* C Program For Implementation Of Linear Block Code.
The User Should Enter the following details :
For A Generator Matrix
1. The No Of Message Bits(n)
2. The No Of Parity Bits(k)
3. And The Message Bits */

#include< stdio.h>
#include< conio.h>

void main()
{
int i,j,n,k,M[10],C[10],G[10][10],par[10][10];
clrscr();
printf(" For A Generator Matrix Enter :\n");
printf("1. The No Of Message Bits(n)\t: ");
scanf("%d",&n);
printf("2. The No Of Parity Bits(k)\t: ");
scanf("%d",&k);
printf("\nEnter The Elements Of Generator Matrix:\n");
for(i=0;i< k;i++)
for(j=0;j< n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter The Message Bits\n");
for(i=0;i< k;i++)
scanf("%d",&M[i]);
printf("\t\t\tResult\n");
printf("____________________________________________________");
printf("\n\tThe Generator Matrix Is :\n\t");
for(i=0;i< k;i++)
{
for(j=0;j< n;j++)
printf("%d\t",G[i][j]);
printf("\n\t");
}
printf("\n\tThe Parity Matrix Is :\n\t");
for(i=0;i< k;i++)
{
for(j=0;j< n-k;j++)
{
par[i][j]=G[i][j+k];
printf("%d\t",par[i][j]);
}
printf("\n\t");
}
printf("\n\tThe Messege Bits :\n\t");
for(i=0;i< k;i++)
printf("%d ",M[i]);
printf("\n\n\tThe Parity Bits :");
for(i=0;i< n-k;i++)
{
C[i] = 0;
for(j=0;j< k;j++)
C[i]=(C[i] + M[j] * par[j][i])%2;
printf("\n\tC%d = %d",i+1,C[i]);
}
printf("\n\n\tCode Word For Given Messege Bit:\n\t");
for(i=0;i< k;i++)
printf("%d ",M[i]);
for(i=0;i< n-k;i++)
printf("%d ",C[i]);
printf("\n____________________________________________________");
getch();
}
/********************** OUTPUT **************************
For A Generator Matrix Enter :
1. The No Of Message Bits(n) : 6
2. The No Of Parity Bits(k) : 3

Enter The Elements Of Generator Matrix:
1
0
0
1
1
1
0
1
0
1
1
0
0
0
1
1
0
1

Enter The Message Bits
0
0
1
Result
____________________________________________________
The Generator Matrix Is :
1 0 0 1 1 1
0 1 0 1 1 0
0 0 1 1 0 1

The Parity Matrix Is :
1 1 1
1 1 0
1 0 1

The Messege Bits :
0 0 1

The Parity Bits :
C1 = 1
C2 = 0
C3 = 1

Code Word For Given Messege Bit:
0 0 1 1 0 1
____________________________________________________ */

Information Content

/* C Program to find th information Content OF the 5 message entered by the user */

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

void main()
{
int c,i;
float p,sum=0,it;
clrscr();
printf("***** INFORMATION CONTENT OF FIVE MESSEGES *****\n\n");
printf("Enter No Of Messages : ");
scanf("%d",&c);
for(i=1;i< =c;i++)
{
printf("\tEnter The %d Probability\t: ",i);
scanf("%f",&p);
it=(log(1/p)) / log(2);
sum=sum+it;
}
printf("Information Content Of Given Message : %f",sum);
getch();
}
/********************* OUTPUT ***********************

***** INFORMATION CONTENT OF FIVE MESSEGES *****

Enter No Of Messages : 5
Enter The 1 Probability : 0.1
Enter The 2 Probability : 0.2
Enter The 3 Probability : 0.3
Enter The 4 Probability : 0.36
Enter The 5 Probability : 0.04
Information Content Of Given Message : 13.498609 */

Even / Odd Number

/* C Program For Checking whether a number is even or odd */

#include< stdio.h>
#include< conio.h>

void main()
{
int a;
clrscr();
printf("Enter A number : ");
scanf("%d",&a);
if(a%2 != 0)
{
printf("\nThe Number %d Is Odd",a);
}
else if(a%2 == 0)
{
printf("\nThe Number %d Is Even",a);
}
getch();
}
/******************* OUTPUT ********************

Enter A number : 66
The Number 66 Is Even

Enter A number : 37
The Number 37 Is Odd */

Non - Restoring Division

/* C Program For Implementation Of Non - Restoring Division */

#include< stdio.h>
#include< conio.h>

int a[5]={0,0,0,0,0},q[4],b[5],b2c[5];

comp()
{
int i=4;
do
{
b2c[i]=b[i];
i--;
}while(b[i+1]!=1);
while(i>=0)
{
b2c[i]=(b[i]+1)%2;
i--;
}
printf("\n\tB's complement:");
for(i=0;i< 5;i++)
printf("%d",b2c[i]);
printf("\n");
}


nonresdiv()
{
shiftleft();
if(a[0]==0)
a_minus_b();
else
a_plus_b();
q[3]=(a[0]+1)%2;
}

shiftleft()
{
int i;
for(i=0;i< 4;i++)
a[i]=a[i+1];
a[4]=q[0];
for(i=0;i< 3;i++)
q[i]=q[i+1];
}

a_minus_b()
{
int i,carry=0,sum=0;
for(i=4;i>=0;i--)
{
sum=(a[i]+b2c[i]+carry);
a[i]=sum%2;
carry=sum/2;
}
}

a_plus_b()
{
int i,carry=0,sum=0;
for(i=4;i>=0;i--)
{
sum=(a[i]+b[i]+carry);
a[i]=sum%2;
carry=sum/2;
}
}


void main()
{
int i,j,k;
clrscr();
printf("Enter dividend in binary form\t: ");
for(i=0;i< 4;i++)
scanf("%d",&q[i]);
printf("Enter divisor in binary form\t: ");
for(i=0;i< 5;i++)
scanf("%d",&b[i]);
comp();
printf("\n\t[A]\t[M]\n");
for(i=0;i< 4;i++)
{
nonresdiv();
printf("\t");
for(j=0;j< 5;j++)
printf("%d",a[j]);
printf("\t");
for(k=0;k< 4;k++)
printf("%d",q[k]);
printf("\n");
}
if(a[0]==1)
a_plus_b();printf("\t");
for(j=0;j< 5;j++)
printf("%d",a[j]);
printf("\t");
for(k=0;k< 4;k++)
printf("%d",q[k]);
printf("\n");
printf("\n\tThe Quotient Is\t: ");
for(k=0;k< 4;k++)
printf("%d",q[k]);
printf("\n\tThe Remainder Is\t: ");
for(j=0;j< 5;j++)
printf("%d",a[j]);
getch();
}

/********************** OUTPUT ****************
Enter dividend in binary form : 0 1 0 1
Enter divisor in binary form : 0 0 0 1 1

B's complement:11101

[A] [M]
11101 1010
11110 0100
11111 1000
00010 0001
00010 0001

The Quotient Is : 0001
The Remainder Is : 00010

Enter dividend in binary form : 1 1 1 1
Enter divisor in binary form : 0 0 1 1 1

B's complement:11001

[A] [M]
11010 1110
11100 1100
00000 1001
11010 0010
00001 0010

The Quotient Is : 0010
The Remainder Is : 00001


Enter dividend in binary form : 1 1 0 1
Enter divisor in binary form : 0 0 0 1 1

B's complement:11101

[A] [M]
11110 1010
00000 0101
11101 1010
11110 0100
00001 0100

The Quotient Is : 0100
The Remainder Is : 00001
*/

Restoring Division

/* C Program for the implentation of Restoring Division
*/
#include< stdio.h>
#include< conio.h>
#include< math.h>

int getsize(int x)
{
int c;
if(x< =1)
c = 2;
else if(x < 4)
c = 2;
else if(x< 8)
c = 3;
else if(x< 16)
c = 4;
else if(x< 32)
c = 5;
else if(x< 64)
c = 6;
else if(x< 128)
c = 7;
else if(x< 256)
c = 8;
else if(x< 512)
c = 9;
return c;
}

int max(int x,int y)
{
if(x< y)
return(y);
else
return(x);
}

void main()
{
int B,Q,Z,M,c,c1,e,f,g,h,i,j,x,y,ch,in,S,G,P;
int a[24],b[12],b1[12],q[12],carry=0,count=0,option;
long num;
do
{
clrscr();
printf("¦-----------------------------------------------¦\n");
printf("¦\t\tPROGRAM FOR DIVISION\t\t¦\n");
printf("¦-----------------------------------------------¦");
printf("\n\nENTER DIVIDEND\t: ");
scanf("%d",&Q);
y = getsize(Q);
printf("ENTER DIVISOR\t: ");
scanf("%d",&M);
x = getsize(M);
Z = max(x,y);
printf("\n\tTOTAL BITS CONSIDERED FOR RESULT => %d",2*Z+1);
printf("\n\tINITiALLY A IS RESET TO ZERO:");
for(i=0;i< =Z;i++)
printf("%d ",a[i]=0);
for(i=Z;i>=0;i--)
{
b1[i] = b[i] = M%2;
M = M/2;
b1[i] = 1-b1[i];
}
carry = 1;
for(i=Z;i>=0;i--)
{
c1 = b1[i]^carry;
carry = b1[i]&&carry;
b1[i]=c1;
}
for(i=2*Z;i>Z;i--)
{
a[i] = Q%2;
Q = Q/2;
}
printf("\n\n\tDivisor\t\t(M)\t: ");
for(i=0;i< =Z;i++)
printf("%d ",b[i]);
printf("\n\t2'C Divisor\t(M)\t: ");
for(i=0;i< =Z;i++)
printf("%d ",b1[i]);
printf("\n\tDividend\t(Q)\t: ");
for(i=Z+1;i< =2*Z;i++)
printf("%d ",a[i]);
printf("\n\n\tBITS CONSIDERED:[ A ] [ M ]");
printf("\n\t\t\t");
for(i=0;i< =Z;i++)
printf("%d ",a[i]);
printf(" ");
for(i=Z+1;i< =2*Z;i++)
printf("%d ",a[i]);
count = Z;
do{
for(i=0;i< 2*Z;i++)
a[i] = a[i+1];
printf("\n\nLeft Shift\t\t");
for(i=0;i< =Z;i++)
printf("%d ",a[i]);
printf(" ");
for(i=Z+1;i< 2*Z;i++)
printf("%d ",a[i]);
carry=0;
for(i=Z;i>=0;i--)
{
S=a[i]^(b1[i]^carry);
G=a[i]&&b1[i];
P=a[i]^b1[i];
carry=G||(P&&carry);
a[i]=S ;
}
printf("\nA< -A-M \t\t");
for(i=0;i< =Z;i++)
printf("%d ",a[i]);
printf(" ");
for(i=Z+1;i< 2*Z;i++)
printf("%d ",a[i]);
ch=a[0];
printf("\nBIT Q:%d",ch);
switch (ch)
{
case 0: a[2*Z]=1;
printf(" Q0< -1\t\t");
for(i=0;i< =Z;i++)
printf("%d ",a[i]);
printf(" ");
for(i=Z+1;i< =2*Z;i++)
printf("%d ",a[i]);
break;

case 1: a[2*Z]=0;
printf(" Q0< -0\t\t");
for(i=0;i< =Z;i++)
printf("%d ",a[i]);
printf(" ");
for(i=Z+1;i< 2*Z;i++)
printf("%d ",a[i]);
carry=0;
for(i=Z;i>=0;i--)
{
S=a[i]^(b[i]^carry);
G=a[i]&&b[i];
P=a[i]^b[i];
carry=G||(P&&carry);
a[i]=S ;
}
printf("\nA< -A+M");
printf("\t\t\t");
for(i=0;i< =Z;i++)
printf("%d ",a[i]);
printf(" ");
for(i=Z+1;i< =2*Z;i++)
printf("%d ",a[i]);
break;
}
count--;
}while(count!=0);
num=0;
printf("\n\t\t< < QUOTIENT IN BITS>> :");
for(i=Z+1;i< =2*Z;i++)
{
printf("%d ",a[i]);
num=num+pow(2,2*Z-i)*a[i];
}
printf("\n\t\tOUOTIENT IN DECIMAL :%d",num);
num=0;
printf("\n\t\t< < REMAINDER IN BITS>>:");
for(i=0;i< =Z;i++)
{
printf("%d ",a[i]);
num=num+pow(2,Z-i)*a[i];
}
printf("\n\t\tREMAINDER IN DECIMAL :%d",num);
getche();
printf("\n\tDO YOU WANT TO CONTINUE PRESS 0-ESC 1-CONT.:");
scanf("%d",&option);



}while(option!=0);
getch();
}

/***************** OUTPUT ***********************

¦-----------------------------------------------¦
¦ PROGRAM FOR DIVISION ¦
¦-----------------------------------------------¦

ENTER DIVIDEND : 16
ENTER DIVISOR : 3

TOTAL BITS CONSIDERED FOR RESULT => 11
INITiALLY A IS RESET TO ZERO:0 0 0 0 0 0

Divisor (M) : 0 0 0 0 1 1
2'C Divisor (M) : 1 1 1 1 0 1
Dividend (Q) : 1 0 0 0 0

BITS CONSIDERED: [ A ] [ M ]
0 0 0 0 0 0 1 0 0 0 0

Left Shift 0 0 0 0 0 1 0 0 0 0
A< -A-M 1 1 1 1 1 0 0 0 0 0
BIT Q:1 Q0< -0 1 1 1 1 1 0 0 0 0 0
A< -A+M 0 0 0 0 0 1 0 0 0 0 0

Left Shift 0 0 0 0 1 0 0 0 0 0
A< -A-M 1 1 1 1 1 1 0 0 0 0
BIT Q:1 Q0< -0 1 1 1 1 1 1 0 0 0 0
A< -A+M 0 0 0 0 1 0 0 0 0 0 0

Left Shift 0 0 0 1 0 0 0 0 0 0
A< -A-M 0 0 0 0 0 1 0 0 0 0
BIT Q:0 Q0< -1 0 0 0 0 0 1 0 0 0 0 1

Left Shift 0 0 0 0 1 0 0 0 0 1
A< -A-M 1 1 1 1 1 1 0 0 0 1
BIT Q:1 Q0< -0 1 1 1 1 1 1 0 0 0 1
A< -A+M 0 0 0 0 1 0 0 0 0 1 0

Left Shift 0 0 0 1 0 0 0 0 1 0
A< -A-M 0 0 0 0 0 1 0 0 1 0
BIT Q:0 Q0< -1 0 0 0 0 0 1 0 0 1 0 1
< < QUOTIENT IN BITS>> :0 0 1 0 1
OUOTIENT IN DECIMAL :5
< < REMAINDER IN BITS>>:0 0 0 0 0 1
REMAINDER IN DECIMAL :1 0 0 0 0 1
DO YOU WANT TO CONTINUE PRESS 0-ESC 1-CONT.:0


¦-----------------------------------------------¦
¦ PROGRAM FOR DIVISION ¦
¦-----------------------------------------------¦

ENTER DIVIDEND : 25
ENTER DIVISOR : 6

TOTAL BITS CONSIDERED FOR RESULT = > 11
INITiALLY A IS RESET TO ZERO:0 0 0 0 0 0

Divisor (M) : 0 0 0 1 1 0
2'C Divisor (M) : 1 1 1 0 1 0
Dividend (Q) : 1 1 0 0 1

BITS CONSIDERED:[ A ] [ M ]
0 0 0 0 0 0 1 1 0 0 1

Left Shift 0 0 0 0 0 1 1 0 0 1
A< -A-M 1 1 1 0 1 1 1 0 0 1
BIT Q:1 Q0< -0 1 1 1 0 1 1 1 0 0 1
A< -A+M 0 0 0 0 0 1 1 0 0 1 0

Left Shift 0 0 0 0 1 1 0 0 1 0
A< -A-M 1 1 1 1 0 1 0 0 1 0
BIT Q:1 Q0< -0 1 1 1 1 0 1 0 0 1 0
A< -A+M 0 0 0 0 1 1 0 0 1 0 0

Left Shift 0 0 0 1 1 0 0 1 0 0
A< -A-M 0 0 0 0 0 0 0 1 0 0
BIT Q:0 Q0< -1 0 0 0 0 0 0 0 1 0 0 1

Left Shift 0 0 0 0 0 0 1 0 0 1
A< -A-M 1 1 1 0 1 0 1 0 0 1
BIT Q:1 Q0< -0 1 1 1 0 1 0 1 0 0 1
A< -A+M 0 0 0 0 0 0 1 0 0 1 0

Left Shift 0 0 0 0 0 1 0 0 1 0
A< -A-M 1 1 1 0 1 1 0 0 1 0
BIT Q:1 Q0< -0 1 1 1 0 1 1 0 0 1 0
A< -A+M 0 0 0 0 0 1 0 0 1 0 0
< < QUOTIENT IN BITS>> :0 0 1 0 0
OUOTIENT IN DECIMAL :4
< < REMAINDER IN BITS>>:0 0 0 0 0 1
REMAINDER IN DECIMAL :1 0 0 0 0 1
DO YOU WANT TO CONTINUE PRESS 0-ESC 1-CONT.:0

¦-----------------------------------------------¦
¦ PROGRAM FOR DIVISION ¦
¦-----------------------------------------------¦

ENTER DIVIDEND : 19
ENTER DIVISOR : 7

TOTAL BITS CONSIDERED FOR RESULT = > 11
INITiALLY A IS RESET TO ZERO:0 0 0 0 0 0

Divisor (M) : 0 0 0 1 1 1
2'C Divisor (M) : 1 1 1 0 0 1
Dividend (Q) : 1 0 0 1 1

BITS CONSIDERED:[ A ] [ M ]
0 0 0 0 0 0 1 0 0 1 1

Left Shift 0 0 0 0 0 1 0 0 1 1
A< -A-M 1 1 1 0 1 0 0 0 1 1
BIT Q:1 Q0< -0 1 1 1 0 1 0 0 0 1 1
A < -A+M 0 0 0 0 0 1 0 0 1 1 0

Left Shift 0 0 0 0 1 0 0 1 1 0
A< -A-M 1 1 1 0 1 1 0 1 1 0
BIT Q:1 Q0< -0 1 1 1 0 1 1 0 1 1 0
A< -A+M 0 0 0 0 1 0 0 1 1 0 0

Left Shift 0 0 0 1 0 0 1 1 0 0
A< -A-M 1 1 1 1 0 1 1 1 0 0
BIT Q:1 Q0< -0 1 1 1 1 0 1 1 1 0 0
A< -A+M 0 0 0 1 0 0 1 1 0 0 0

Left Shift 0 0 1 0 0 1 1 0 0 0
A< -A-M 0 0 0 0 1 0 1 0 0 0
BIT Q:0 Q0< -1 0 0 0 0 1 0 1 0 0 0 1

Left Shift 0 0 0 1 0 1 0 0 0 1
A< -A-M 1 1 1 1 1 0 0 0 0 1
BIT Q:1 Q0< -0 1 1 1 1 1 0 0 0 0 1
A< -A+M 0 0 0 1 0 1 0 0 0 1 0
< < QUOTIENT IN BITS>> :0 0 0 1 0
OUOTIENT IN DECIMAL :2
< < REMAINDER IN BITS>>:0 0 0 1 0 1
REMAINDER IN DECIMAL :5 0 0 1 0 1
DO YOU WANT TO CONTINUE PRESS 0-ESC 1-CONT.:0

*/

Thursday, January 17, 2008

Booth's Algorithm

/* C Program For Implementation Of Signed Multiplication
OR
C Program for the Implementation Of Booths Algorithm */

#include< stdio.h>
#include< conio.h>
#include< process.h>
#include< math.h>

int get(int a)
{
char ch='B';
int flag=0;
if(a==1)
ch='A';
do
{
printf("│ ENTER VALUE OF %c: ",ch);
scanf("%d",&a);
if(a< 0)
{
a = a * -1;
flag = 1;
}
if(9< =a)
printf("│\n\t!INVALID NUMBER.ENTER VALUE (-9 < A < 9)!");
}while(9< =a);
if(flag)
a = a *-1;
return(a);
}

void add(int *a,int *b)
{
int x,i,c=0;
for(i=3;i>=0;i--)
{
x=a[i];
a[i]=c^x^b[i];
if(((c==1)&&(x==1))||((x==1)&&(b[i]==1))||((b[i]==1)&&(c==1)))
c = 1;
else
c = 0;
}
}

void binary(int x,int*arr)
{
int i,p=x,c[4]={0,0,0,1};
for(i=0;i< 4;i++)
arr[i] = 0;
if(x < 0)
x = x *-1;
i = 3;
do
{
arr[i]=x%2;
x = x/2;
i--;
}while(x!=0);
if(p< 0)
{
for(i=0;i< 4;i++)
arr[i]=1-arr[i];
add(arr,c);
}
printf("\n\nTHE BINARY EQUIVALENT OF %d IS : ",p);
for(i=0;i< 4;i++)
printf("%d",arr[i]);
}

void rshift(int x,int *y)
{
int i;
for(i=3;i>0;i--)
y[i] = y[i-1];
y[0] = x;
}

void main()
{
int q=0,i,j,a,b,A[4]={0,0,0,0},C[4]={0,0,0,1},C1[8]={0,0,0,0,0,0,0,1};
int s=0,z=0,Q[4],M[4],temp,temp1[4],ans[8],y,x=0,c=0;
clrscr();
printf("\n│----------------------------------------------------\n");
a = get(1);
b=get(0);
printf("\n│---------------------------------------------------\n");
binary(a,M);
binary(b,Q);
printf("\n\n---------------------------------------------------\n");
printf(" OPERATION\t\t A\t Q\tQ'\t M");
printf("\n\n INITIAL\t\t");
for(i=0;i< 4;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 4;i++)
printf("%d",Q[i]);
printf("\t");
printf("%d\t",q);
for(i=0;i< 4;i++)
printf("%d",M[i]);
for(j=0;j< 4;j++)
{
if((Q[3]==0)&&(q==1))
{
printf("\n A:=A+M \t\t");
add(A,M);
for(i=0;i< 4;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 4;i++)
printf("%d",Q[i]);
printf("\t%d\t",q);
for(i=0;i< 4;i++)
printf("%d",M[i]);
}
if((Q[3]==1)&&(q==0))
{
printf("\n A:=A-M \t\t");
for(i=0;i< 4;i++)
temp1[i] = 1-M[i];
add(temp1,C);
add(A,temp1);
for(i=0;i< 4;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 4;i++)
printf("%d",Q[i]);
printf("\t%d\t",q);
for(i=0;i< 4;i++)
printf("%d",M[i]);
}
printf("\n Shift \t\t\t");
y = A[3];
q = Q[3];
rshift(A[0],A);
rshift(y,Q);
for(i=0;i< 4;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 4;i++)
printf("%d",Q[i]);
printf("\t");
printf("%d\t",q);
for(i=0;i< 4;i++)
printf("%d",M[i]);
}
printf("\n\n---------------------------------------------------\n");
printf("\nTHE ANSWER IN BINARY IS : ");
for(i=0;i< 4;i++)
ans[i]=A[i];
for(i=0;i< 4;i++)
ans[i+4]=Q[i];
if(((a< 0)&&(b>0))||((a>0)&&(b< 0)))
{
for(i=0;i< 8;i++)
ans[i]=1-ans[i];
for(i=7;i>=0;i--)
{
x = ans[i];
ans[i]=c^x^C1[i];
if(((c==1)&&(x==1))||((x==1)&&(C1[i]==1))||((C1[i]==1)&&(c==1)))
c=1;
else
c=0;
}
}
for(i=0;i< 8;i++)
printf("%d",ans[i]);
for(i=7;i>=0;i--)
{
s = s + (pow(2,z) * ans[i]);
z = z+1;
}
if(((a< 0)&&(b>0))||((a>0)&&(b< 0)))
printf("\nTHE ANSWER IN DECIMAL IS : -%d\n",s);
else
printf("\nTHE ANSWER IN DECIMAL IS : %d\n",s);
getch();
}

/********************** OUTPUT **********************
│-------------------------------------------------------------
│ ENTER VALUE OF A: 7
│ ENTER VALUE OF B: -3
│-------------------------------------------------------------

THE BINARY EQUIVALENT OF 7 IS : 0111

THE BINARY EQUIVALENT OF -3 IS : 1101

---------------------------------------------------
OPERATION A Q Q' M

INITIAL 0000 1101 0 0111
A:=A-M 1001 1101 0 0111
Shift 1100 1110 1 0111
A:=A+M 0011 1110 1 0111
Shift 0001 1111 0 0111
A:=A-M 1010 1111 0 0111
Shift 1101 0111 1 0111
Shift 1110 1011 1 0111

---------------------------------------------------

THE ANSWER IN BINARY IS : 00010101
THE ANSWER IN DECIMAL IS : -21

Unsigned Multiplication - C Program

/* C Program For Implementation of Unsigned Multiplication Of Two Numbers */

#include< stdio.h>
#include< conio.h>

#define max 16

void convert1(int *n1,int *n2)
{
int i,z,j,d=0,n=1;
z=(2*max);
for(i=max-1;i>=0;i--)
{
z--;
if(n1[i]==1)
{
n=1;
for(j=1;j< =z;j++)
n = n * 2;
d = d + n;
}
}
n = 1;
for(i=max-1;i>=0;i--)
{
if(n2[i]==1)
{
n = 1;
for(j=1;j< =i;j++)
n = n * 2;
d = d + n;
}
}
printf("\n\n\tIN DECIMAL FORM: %d",d);
}

void convert2(int x,int *arr)
{
int q=0,j=x;
while(j>=0 && q< max)
{
x=j%2;
arr[q++]=x;
j = j/2;
}
}

int addbin(int *n1,int *n2,int *arr3)
{
int i,c=0,g,p,j,arr[max];
for(i=max-1;i>=0;i--)
{
g = n1[i]&n2[i];
p = n1[i]^n2[i];
arr[i] = p^c;
c = g|(p&c);
}
c = 0;
for(i=0;i< max;i++)
{
if(c==0)
{
if(n1[i]==0 && n2[i]==0)
arr3[i]=0;
if(n1[i]==0 && n2[i]==1)
arr3[i]=1;
if(n1[i]==1 && n2[i]==0)
arr3[i]=1;
if(n1[i]==1 && n2[i]==1)
{
arr3[i] = 0;
c = 1;
}
}
else if(c==1)
{
if(n1[i]==0 && n2[i]==0)
{
arr3[i] = 1;
c = 0;
}
if(n1[i]==0 && n2[i]==1)
arr3[i]=0;
if(n1[i]==1 && n2[i]==0)
arr3[i]=0;
if(n1[i]==1 && n2[i]==1)
arr3[i]=1;
}
}
return c;
}

void shift(int *n1,int *n2,int *arr3)
{
int c=0,i,j,k,temp[max];
for(i=0;i< max;i++)
{
if(arr3[0]==1)
{
c = addbin(n1,n2,temp);
for(j=0;j< max;j++)
n1[j] = temp[j];
}
for(j=0;j< max-1;j++)
arr3[j] = arr3[j+1];
arr3[max-1] = n1[0];
for(j=0;j< max-1;j++)
n1[j] = n1[j+1];
n1[max-1] = c;
}
}

void main()
{
int i,j,a[max],b[max],q[max],x,y;
clrscr();
printf("\n _________________________________________________");
printf("\n|\t\tUnsigned Multiplication\n");
printf("|----------------------------------------------------------------------------");
printf("\n|Enter The First No\t: ");
scanf("%d",&x);
printf("|Enter The Second No\t: ");
scanf("%d",&y);
printf("|_________________________________________________");
while(x>0 && y>0)
{
convert2(0,a);
printf("\n\nTHE 16-bit BINARY FORM OF 0\t: ");
for(i=max-1;i>=0;i--)
printf("%d ",a[i]);
convert2(x,b);
printf("\nTHE 16-bit BINARY FORM OF %d\t: ",x);
for(i=max-1;i>=0;i--)
printf("%d ",b[i]);
convert2(y,q);
printf("\nTHE 16-bit BINARY FORM OF %d\t: ",y);
for(i=max-1;i>=0;i--)
printf("%d ",q[i]);
shift(a,b,q);
printf("\n\n\tCalculating %d * %d ",x,y);
printf("In 16-bit BINARY FORM :\n\n\t");
for(i=max-1;i>=0;i--)
printf("%d ",a[i]);
printf("\n\t");
for(j=max-1;j>=0;j--)
printf("%d ",q[j]);
convert1(a,q);
x = -1;
getch();
}
}

/***************** OUTPUT ********************
_________________________________________________
| Unsigned Multiplication
|-------------------------------------------------------------------------
|Enter The First No : 56
|Enter The Second No : 12
|_________________________________________________

THE 16-bit BINARY FORM OF 0 : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
THE 16-bit BINARY FORM OF 56 : 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0
THE 16-bit BINARY FORM OF 12 : 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0

Calculating 56 * 12 In 16-bit BINARY FORM :

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0

IN DECIMAL FORM: 672 */

C Program - Look Ahead Carry Adder

/* C Program For Implementation Of Look Ahead Carry Adder */

#include< stdio.h>
#include< conio.h>
#include< process.h>
#include< math.h>

int get1(int a)
{
char ch='B';
if(a==1)
ch='A';
do
{
printf("\n\tENTER VALUE OF %c:",ch);
scanf("%d",&a);
if(a< =0)
printf("\n\t\t!INVALID NUMBER.ENTER VALUE (0< A)!");
}while(a< =0);
return(a);
}

int and(int a,int b)
{
int c;
if(a< b)
c=a;
else
c=b;
return (c);
}

int or(int a,int b)
{
int x;
if(a>b)
x=a;
else
x=b;
return x;
}

int exor(int a,int b)
{
int x;
if(a==b)
x=0;
else
x=1;
return x;
}


void add()
{
int i=7,A,B,a,b,cin,num;
int n1[8],n2[8],cg[8],cp[8],sum[8];
for(i=0;i< =7;i++)
{
n1[i]=0; // Num 1
n2[i]=0; // Num 2
cg[i]=0; // Gi
cp[i]=0; // Pi
sum[i]=0; // Sum
}
A = a = get1(1);
B = b = get1(0);
i=7;
do
{
n1[i]=a%2;
a=a/2;
n2[i]=b%2;
b=b/2;
i--;
}while((a!=0)||(b!=0));
i=0;
printf("\n\t\t Binary Form",A);
printf("\n\t A = %d : ",A);
for(i=0;i< =7;i++)
printf("%d ",n1[i]);
printf("\n\t B = %d : ",B);
for(i=0;i< =7;i++)
printf("%d ",n2[i]);
cin=0;
for(i=7;i>=0;i--)
{
sum[i]=exor(cin,exor(n1[i],n2[i])); // Sum Pi (+) Bi
cg[i]=and(n1[i],n2[i]); // Gi = Ai . Bi
cp[i]=or(n1[i],n2[i]); // Pi = Ai (+) Bi
cin=or(cg[i],and(cp[i],cin)); // Cin =Gi + PiCi
}
printf("\n\n\t\t SUM: ");
num=0;
for(i=0;i< =7;i++)
{
printf(" %d",sum[i]);
num=num + (sum[i]*pow(2,7-i));
}
printf("\n\n\t\t SUM: %d + %d= %d\n",A,B,num);
printf("\t\t The Carry Is : %d\n\n",cin);
}

void main()
{
int ch,a,b,c,d;
clrscr();
while(1)
{
M: printf("******** MENU FOR LOOK AHEAD CARRY ADDER ********");
printf("\n\t\t1.ADDITION OF TWO NUMBER");
printf("\n\t\t2.EXIT\n");
printf("*************************************************");
printf("\n\t\tEnter Your Option:");
scanf("%d",&ch);
switch(ch)
{
case 1:
add();
getch();
break;

case 2: exit(0);
break;

default:
clrscr();
printf("ERROR!!!!!!!!! INVALID ENTRY...\n");
printf("Back To Main Menu\n\n");
goto M;
}
}
}


/*------------------ OUTPUT --------------------

******** MENU FOR LOOK AHEAD CARRY ADDER ********
1.ADDITION OF TWO NUMBER
2.EXIT
*************************************************
Enter Your Option:1

ENTER VALUE OF A:2

ENTER VALUE OF B:3

Binary Form
A = 2 : 0 0 0 0 0 0 1 0
B = 3 : 0 0 0 0 0 0 1 1

SUM: 0 0 0 0 0 1 0 1

SUM: 2 + 3= 5
The Carry Is : 0

******** MENU FOR LOOK AHEAD CARRY ADDER ********
1.ADDITION OF TWO NUMBER
2.EXIT
*************************************************
Enter Your Option:2 */

Logical Operation On Numbers - C Program

/* C Program For Implementation Of Logical Operations on Two Numbers, The Logical Operations Include Logical AND, Logical OR and Logical NOT */


#include<>
#include<>
#include<>

void result(int,int,int);
void main()
{
int n1,n2,ch,c; clrscr();
do
{
printf("Enter Two Numbers : \n");
scanf("%d%d",&n1,&n2);
printf("************* MENU FOR LOGICAL OPERATIONS ***********\n");
printf("1. AND\n2. OR\n3. NOT\n\nEnter Your Choice ");
scanf("%d",&ch);
result(n1,n2,ch);
printf("\nDo you want to continue?\n1. Yes\n2. No\n");
scanf("%d",&c);
}
while(c == 1);
getch();
}
void result(int a,int b,int c)
{
int b1[8], b2[8], res[8], c1[8], c2[8];
int i,n,s,s1,s2,x,y;
x = a;
y = b;
if(c!=1 && c!=2 && c!=3)
{
clrscr();
printf("ERROR!!!!!!!!! INVALID ENTRY!!\n");
main();
}
/* Conversion to binary */
if(x< x =" x+256;" y =" y+256;" i="0;i<" x="="0)" x="x/2;" i="0;i<" y="="0)" y="y/2;" c="="1)" n="0;" c="="2)" n="1;" i="0;i<" c="="3)" i="0;i<" c="="1" c="="2)" s="0;" i="0;i<" s =" s" c="="3)" s1="0;" i="0;i<" s1 =" s1" s2="0;" i="0;i<" s2 =" s2" i="7;i">=0;i--)
printf("%d",b1[i]);
printf("\t\t%d",a);
printf("\t\n\t Second\t\t\t");
for(i=7;i>=0;i--)
printf("%d",b2[i]);
printf("\t\t%d",b);
printf("\t\n\t Result\t\t\t");
if(c==1 c==2)
{
for(i=7;i>=0;i--)
printf("%d",res[i]);
printf("\t\t%d",s);
}
if(c==3)
{
for(i=7;i>=0;i--)
printf("%d",c1[i]);
printf("\t\t%d",s1);
printf("\t\n\tResult 2\t\t\t");
for(i=7;i>=0;i--)
printf("%d",c2[i]);
printf("\t\t%d",s2);
}
printf("\t\n\t--------------------------------");
printf("---------------------------------\n");
}


/******************* OUTPUT *****************
Enter Two Numbers :
5
2
************* MENU FOR LOGICAL OPERATIONS ***********
1. AND
2. OR
3. NOT
Enter Your Choice 1
___________________________________________
Number Binary Decimal
-----------------------------------------------------------------
First 00000101 5
Second 00000010 2
Result 00000000 0
-----------------------------------------------------------------
Do you want to continue?
1. Yes
2. No
1
Enter Two Numbers :
6
3
************* MENU FOR LOGICAL OPERATIONS ***********
1. AND
2. OR
3. NOT
Enter Your Choice 2
_________________________________________________________________
Number Binary Decimal
-----------------------------------------------------------------
First 00000110 6
Second 00000011 3
Result 00000111 7
-----------------------------------------------------------------
Do you want to continue?
1. Yes
2. No
1
Enter Two Numbers :
8
5
************* MENU FOR LOGICAL OPERATIONS ***********
1. AND
2. OR
3. NOT
Enter Your Choice 3
_________________________________________________________________
Number Binary Decimal
-----------------------------------------------------------------
First 00001000 8
Second 00000101 5
Result 11110111 247
Result 2 11111010 250
-----------------------------------------------------------------
Do you want to continue?
1. Yes
2. No 2 */

Binary Operation On Two Numbers

/* C Program For Implementation Of Binary Operations on Two Numbers

Operations Included
Binary Addition
Binary Subtraction

Binary Subtraction Is Done Making Use Of Two's Complement */


#include< stdio.h>
#include< conio.h>
#include< math.h>
int exor(int n1, int n2)
{
if(n1==n2)
return 0;
else
return 1;
}
int min(int n1, int n2)
{
if(n1< n2)
return n1;
else
return n2;
}
int max(int n1, int n2)
{
if(n1>=n2)
return n1;
else
return n2;
}
void main()
{
int a1[10],a2[10],a3[10],a4[10],a5[10],a7[10],a8[10],a11[10];
int j=0,i=0,p=0,n1=0,n3=0,n4=0,n5=0,n6=0,n7=0,n8=0;
int a,b,c,h,v,o, n,q,r,x,y,S,Z,c1,c2,c3,c4,s1,carry;
int g[8],p1[10],p2,p3[10],p4[10],s[8],w[8],z[8];
clrscr();
do
{ printf("\t\tCALCULATIONS OF BINARY ARITHMETIC OPERATIONS\n");
printf("\t --------------------------------------------\n");
printf("1: BINARY ADDITION \n");
printf("2: BINARY SUBTRACTION\n");
L:
printf("\nENTER THE OPERATION YOU WOULD LIKE TO CARRY OUT:");
scanf("%d",&n);
switch(n)
{case 1:
{M:
printf("\nEnter The First Number :");
scanf("%d",&x);
if(x>0)
printf("x = %d\n",x);
else
{ printf("The Entered Number Is Invalid\n");
printf("Please Enter the valid value\n");
goto M;
}
for(i=0;i< =7;i++)
{ z[i]=0; s[i]=0; }
i=7;
do
{ z[i]=x%2;
x=x/2;
i--;
}while(x!=0);
printf("The binary equivalent of x is: ");
for(j=0;j< =7;j++)
{ printf("%d",z[j]); }
N:
printf("\n\nEnter the second number : ");
scanf("%d",&y);
if(y>=0)
printf("y = %d\n",y);
else
{ printf("The entered number is invalid\n");
printf("Please enter the valid value\n");
goto N;
}
i=7;
do
{ s[i]=y%2;
y=y/2;
i--;
}while(y!=0);
printf("The binary equivalent of y is: ");
for(p=0;p< =7;p++)
{ printf("%d",s[p]); }
printf("\n\nBinary of x: ");
for(i=0;i< =7;i++)
{printf("%d",z[i]); }
printf("\nBinary of y: ");
for(i=0;i< =7;i++)
{printf("%d",s[i]); }
for(h=7;h>=0;h--)
{s1=exor(carry,exor(z[h],s[h]));
c1=min(z[h],s[h]);
c2=min(s[h],carry);
c3=min(z[h],carry);
c4=max(c3,max(c1,c2));
carry=c4;
a1[h]=s1; }
printf("\nSum : ");
for(b=0;b< =7;b++)
{ printf("%d",a1[b]); }
for(v=0;v< =7;v++)
{ o=pow(2,7-v);
n1=n1+(a1[v]*o); }
printf("\n\nDecimal Equivalent of Sum : ");
printf("%d",n1);
break;
}
case 2:
{ printf("\nEnter the first number : ");
scanf("%d",&x); Z=x;
printf("x = %d",x);
for(i=0;i< =7;i++)
{ z[i]=0; s[i]=0; }
if(Z>0)
{i=7;
do
{ z[i]=x%2;
x=x/2;
i--;
}while(x!=0);
printf("\nThe binary equivalent of x is: ");
for(j=0;j< =7;j++)
{ printf("%d",z[j]); }
}
if(Z< 0)
{i=7;
do
{ z[i]=x%2;
x=x/2;
if(z[i]< 0)
z[i]=-z[i];
i--;
}while(x!=0);
printf("\nThe binary equivalent of x is: ");
for(j=0;j< =7;j++)
{ printf("%d",z[j]); }
for(h=0;h< =7;h++)
{ w[h]=z[h];p3[h]=1-w[h]; }
printf("\n\nThe 1'S Complement of x is: ");
for(b=0;b< =7;b++)
{ printf("%d",p3[b]); }
if(p3[7]==1)
{ p3[7]=0;carry=1;}
else
if(p3[7]==0)
{p3[7]=1;carry=0;}
for(h=6;h>=0;h--)
{ s1=exor(carry,p3[h]);
c1=min(p3[h],carry);
carry=c1;
p3[h]=s1;
}
printf("\nThe 2'S Complement of x is: ");
for(b=0;b< =7;b++)
{ printf("%d",p3[b]); }
}
printf("\n\nEnter the second number : ");
scanf("%d",&y);S=y;
printf("y = %d",y);
if(S< 0)
{p=7;
do
{ s[p]=y%2;
y=y/2;
if(s[p]< 0)
s[p]=-s[p];
p--;
}while(y!=0);
printf("\nThe binary equivalent of y is: ");
for(q=0;q< =7;q++)
{ printf("%d",s[q]); }
printf("\n________________________________________________");
}
if(S>0)
{p=7;
do
{ s[p]=y%2;
y=y/2;
p--;
}while(y!=0);
printf("\nThe binary equivalent of y is: ");
for(q=0;q< =7;q++)
{ printf("%d",s[q]); }
for(h=0;h< =7;h++)
{ w[h]=s[h];
p4[h]=1-w[h];
}
printf("\nThe 1'S Complement of y is: ");
for(b=0;b< =7;b++)
{ printf("%d",p4[b]); }
if(p4[7]==1)
{ p4[7]=0;carry=0;}
else
if(p4[7]==0)
{ p4[7]=1;carry=0; }
for(h=6;h>=0;h--)
{ s1=exor(carry,p4[h]);
c1=min(p4[h],carry);
carry=c1;
p4[h]=s1; }
printf("\nThe 2'S Complement of y is: ");
for(b=0;b< =7;b++)
{ printf("%d",p4[b]); }
printf("\n_______________________________________________");
}
if(Z< 0 && S< 0)
{ printf("\n2'S Complement of x: ");
for(h=0;h< =7;h++)
{ printf("%d",p3[h]); }
printf("\nBinary of y : ");
for(h=0;h< =7;h++)
{ printf("%d",s[h]); }
for(h=7;h>=0;h--)
{ s1=exor(carry,exor(p3[h],s[h]));
c1=min(p3[h],s[h]);
c2=min(s[h],carry);
c3=min(p3[h],carry);
c4=max(c3,max(c1,c2));
carry=c4;
a2[h]=s1;
}
if(carry==0)
{ printf("\nDifference : ");
for(y=0;y< =7;y++)
{ printf("%d",a2[y]);
a2[y]=1-a2[y]; }
printf("\nAnswer is Negative and is in 2'S Complement form");
printf("\n1'S Complement : ");
for(y=0;y< =7;y++)
{ printf("%d",a2[y]); }
if(a2[7]==1)
{ a2[7]=0; carry=1;}
else
{ a2[7]=1; carry=0; }
for(i=6;i>=0;i--)
{ c1=exor(carry,a2[i]);
c2=min(a2[i],carry);
carry=c2;
a2[i]=c1;
}
printf("\n2'S Complement : ");
for(y=0;y< =7;y++)
{
printf("%d",a2[y]);
n3=n3+(a2[y] *pow(2,7-y));
}
printf("\nDecimal Equivalent of Difference : ");
printf("%d",n3);
}
else
{
if(carry==1)
{
printf("\nDifference : ");
for(y=0;y< =7;y++)
{
printf("%d",a2[y]);
}
for(y=0;y< =7;y++)
{
n4=n4+(a2[y] *pow(2,7-y));
}
printf("\nDecimal Equivalent of Difference : ");
printf("%d",n4);
}
}

}
if(Z>0 && S>0)
{
printf("\nBinary of x : ");
for(h=0;h< =7;h++)
{
printf("%d",z[h]);
}
printf("\n2'S Complement of y : ");
for(h=0;h< =7;h++)
{
printf("%d",p4[h]);
}
for(h=7;h>=0;h--)
{
s1=exor(carry,exor(z[h],p4[h]));
c1=min(z[h],p4[h]);
c2=min(p4[h],carry);
c3=min(z[h],carry);
c4=max(c3,max(c1,c2));
carry=c4;
a5[h]=s1;
}
if(carry==1)
{
printf("\nDifference : ");
for(y=0;y< =7;y++)
{
printf("%d",a5[y]);
}
for(y=0;y< =7;y++)
{
n5=n5+(a5[y] *pow(2,7-y));
}
printf("\nDecimal Equivalent of difference : ");
printf("%d",n5);
}
else
if(carry==0)
{
printf("\nDifference : ");
for(y=0;y< =7;y++)
{
printf("%d",a5[y]);
a5[y]=1-a5[y];
}
printf("\nAnswer is Negative and is in 2'S Complement form");
printf("\n1'S Complement is : ");
for(y=0;y< =7;y++)
printf("%d",a5[y]);
if(a5[7]==1)
{
a5[7]=0; carry=1;
}
else
{
a5[7]=1; carry=0;
}
for(i=6;i>=0;i--)
{
c1=exor(carry,a5[i]);
c2=min(a5[i],carry);
carry=c2;
a5[i]=c1;
}
printf("\n2'S Complement is : ");
for(y=0;y< =7;y++)
{
printf("%d",a5[y]);
n6=n6+(a5[y] *pow(2,7-y));
}
printf("\nDecimal Equivalent of difference : ");
printf("%d",n6);
}
}
if(Z< 0 && S>0)
{
printf("\n2'S Complement of x : ");
for(h=0;h< =7;h++)
{
printf("%d",p3[h]);
}
printf("\n2'S Complement of y : ");
for(h=0;h< =7;h++)
{
printf("%d",p4[h]);
}
for(h=7;h>=0;h--)
{
s1=exor(carry,exor(p3[h],p4[h]));
c1=min(p3[h],p4[h]);
c2=min(p4[h],carry);
c3=min(p3[h],carry);
c4=max(c3,max(c1,c2));
carry=c4;
a8[h]=s1;
}
printf("\nDifference : ");
for(y=0;y< =7;y++)
{
printf("%d",a8[y]);
a8[y]=1-a8[y];
}
printf("\nAnswer is Negative and is in 2'S Complement form");
printf("\n1'S Complement is : ");
for(y=0;y< =7;y++)
printf("%d",a8[y]);
if(a8[7]==1)
{
a8[7]=0; carry=1;
}
else
{
a8[7]=1; carry=0;
}
for(i=6;i>=0;i--)
{
c1=exor(carry,a8[i]);
c2=min(a8[i],carry);
carry=c2;
a8[i]=c1;
}
printf("\n2'S Complement is : ");
for(y=0;y< =7;y++)
{
printf("%d",a8[y]);
n7=n7+(a8[y] *pow(2,7-y));
}
printf("\nDecimal Equivalent of difference : ");
printf("%d",n7);
}
if(Z>0 && S< 0)
{
printf("\nBinary of x : ");
for(h=0;h< =7;h++)
{ printf("%d",z[h]); }
printf("\nBinary of y : ");
for(h=0;h< =7;h++)
{ printf("%d",s[h]); }
for(h=7;h>=0;h--)
{
s1=exor(carry,exor(z[h],s[h]));
c1=min(z[h],s[h]);
c2=min(s[h],carry);
c3=min(z[h],carry);
c4=max(c3,max(c1,c2));
carry=c4;
a11[h]=s1;
}
printf("\nDifference : ");
for(y=0;y< =7;y++)
{ printf("%d",a11[y]); }
for(y=0;y< =7;y++)
{ n8=n8+(a11[y] *pow(2,7-y)); }
printf("\nDecimal Equivalent of difference : ");
printf("%d",n8);
}
break;
default:
clrscr();
printf("ERROR!!!!!!!\nINVALID ENTRY\n");
}
}
printf("\n-------------------------------------------------");
printf("\nWould You Like To Continue\n");
printf("Please Enter '1' For 'YES' And '0' For 'NO': ");
scanf("%d",&r);
if(r==0)
{ printf("\n************** THANK YOU ***************\n") }
}while(r==1);
getch();
}


/******************** OUTPUT **********************
CALCULATIONS OF BINARY ARITHMETIC OPERATIONS
--------------------------------------------
1: BINARY ADDITION
2: BINARY SUBTRACTION

ENTER THE OPERATION YOU WOULD LIKE TO CARRY OUT:1

Enter The First Number :5
x = 5
The binary equivalent of x is: 00000101

Enter the second number : 2
y = 2
The binary equivalent of y is: 00000010

Binary of x: 00000101
Binary of y: 00000010
Sum : 00001001

Decimal Equivalent of Sum : 9
-------------------------------------------------
Would You Like To Continue
Please Enter '1' For 'YES' And '0' For 'NO': 1

CALCULATIONS OF BINARY ARITHMETIC OPERATIONS
--------------------------------------------
1: BINARY ADDITION
2: BINARY SUBTRACTION

ENTER THE OPERATION YOU WOULD LIKE TO CARRY OUT: 2
Enter the first number : 4
x = 4
The binary equivalent of x is: 00000100

Enter the second number : 3
y = 3
The binary equivalent of y is: 00000011
The 1'S Complement of y is: 11111100
The 2'S Complement of y is: 11111101
_______________________________________________
Binary of x : 00000100
2'S Complement of y : 11111101
Difference : 00000001
Decimal Equivalent of difference : 1
-------------------------------------------------
Would You Like To Continue
Please Enter '1' For 'YES' And '0' For 'NO': 1

CALCULATIONS OF BINARY ARITHMETIC OPERATIONS
--------------------------------------------
1: BINARY ADDITION
2: BINARY SUBTRACTION

ENTER THE OPERATION YOU WOULD LIKE TO CARRY OUT:2

Enter the first number : 4
x = 4
The binary equivalent of x is: 00000100

Enter the second number : 9
y = 9
The binary equivalent of y is: 00001001
The 1'S Complement of y is: 11110110
The 2'S Complement of y is: 11110111
_______________________________________________
Binary of x : 00000100
2'S Complement of y : 11110111
Difference : 11111011
Answer is Negative and is in 2'S Complement form
1'S Complement is : 00000100
2'S Complement is : 00000101
Decimal Equivalent of difference : 5
-------------------------------------------------
Would You Like To Continue
Please Enter '1' For 'YES' And '0' For 'NO': 1

CALCULATIONS OF BINARY ARITHMETIC OPERATIONS
--------------------------------------------
1: BINARY ADDITION
2: BINARY SUBTRACTION

ENTER THE OPERATION YOU WOULD LIKE TO CARRY OUT:2
Enter the first number : 6
x = -6
The binary equivalent of x is: 00000110

The 1'S Complement of x is: 11111001
The 2'S Complement of x is: 11111010

Enter the second number : 5
y = 5
The binary equivalent of y is: 00000101
The 1'S Complement of y is: 11111010
The 2'S Complement of y is: 11111011
_______________________________________________
2'S Complement of x : 11111010
2'S Complement of y : 11111011
Difference : 11110101
Answer is Negative and is in 2'S Complement form
1'S Complement is : 00001010
2'S Complement is : 00001011
Decimal Equivalent of difference : 11
-------------------------------------------------
Would You Like To Continue
Please Enter '1' For 'YES' And '0' For 'NO': 0

************** THANK YOU ***************** */
Your Ad Here