/* File Name : substract.java
Program to Implement Binary Subtraction
Learner:-ABDUL MUGEESH
RollNo:-12CO104 */
import java.io.*;
class substract
{
public static void main(String args[])throws IOException
{
BufferedReader obj= new BufferedReader(new InputStreamReader(System.in));
int a,b,n,i,c;
System.out.print("\nEnter the Number of Bits :: ");
n=Integer.parseInt(obj.readLine());
System.out.println("\nEnter Two Number :: ");
a=Integer.parseInt(obj.readLine());
System.out.println();
b=Integer.parseInt(obj.readLine());
int x[]=new int [n];
int y[]=new int[n];
for(i=0;i<=n-1;i++)
{
int temp1=a%2;
a=a/2;
x[i]=temp1;
int temp2=b%2;
b=b/2;
y[i]=temp2;
}
System.out.print("\nThe Binary form of the Two Numbers is :: ");
for(i=n-1;i>=0;i--)
{
System.out.print(x[i]);
}
System.out.println();
for(i=n-1;i>=0;i--)
{
System.out.print(y[i]);
}
System.out.println();
for(i=0;i<=n-1;i++)
{
if(y[i]==0)
{
y[i]=1;
}
else if (y[i]==1)
{
y[i]=0;
}
}
System.out.print("\nThe 1's Complement is :: ");
for(i=n-1;i>=0;i--)
{
System.out.print(y[i]);
}
y[0]=y[0]+1;
for(i=0;i<=n-1;i++)
{
if (y[i]==2)
{
y[i]=0;
y[i+1]=y[i+1]+1;
}
}
System.out.println();
System.out.print("\nThe 2's Complement is :: ");
for(i=n-1;i>=0;i--)
{
System.out.print(y[i]);
}
System.out.println();
int sub[]= new int [n];
for(i=0;i<=n-1;i++)
{
sub[i]=x[i]+y[i];
if(sub[i]==2)
{
sub[i]=0;
if(i<n-1)
x[i+1]=x[i+1]+1;
}
if (sub[i]==3)
{
sub[i]=1;
if(i<n-1)
x[i+1]=x[i+1]+1;
}
}
System.out.print("\nSubstraction is :: ");
for(i=n-1;i>=0;i--)
{
System.out.print(sub[i]);
}
System.out.println();
}
}
/*
OUTPUT:
Enter the no. of bits
8
Enter two nos.
6
3
The binary form of the two nos. is
00000110
00000011
The 1's complement is
11111100
The 2's complement is
11111101
Substraction is
00000011
*/
Program to Implement Binary Subtraction
Learner:-ABDUL MUGEESH
RollNo:-12CO104 */
import java.io.*;
class substract
{
public static void main(String args[])throws IOException
{
BufferedReader obj= new BufferedReader(new InputStreamReader(System.in));
int a,b,n,i,c;
System.out.print("\nEnter the Number of Bits :: ");
n=Integer.parseInt(obj.readLine());
System.out.println("\nEnter Two Number :: ");
a=Integer.parseInt(obj.readLine());
System.out.println();
b=Integer.parseInt(obj.readLine());
int x[]=new int [n];
int y[]=new int[n];
for(i=0;i<=n-1;i++)
{
int temp1=a%2;
a=a/2;
x[i]=temp1;
int temp2=b%2;
b=b/2;
y[i]=temp2;
}
System.out.print("\nThe Binary form of the Two Numbers is :: ");
for(i=n-1;i>=0;i--)
{
System.out.print(x[i]);
}
System.out.println();
for(i=n-1;i>=0;i--)
{
System.out.print(y[i]);
}
System.out.println();
for(i=0;i<=n-1;i++)
{
if(y[i]==0)
{
y[i]=1;
}
else if (y[i]==1)
{
y[i]=0;
}
}
System.out.print("\nThe 1's Complement is :: ");
for(i=n-1;i>=0;i--)
{
System.out.print(y[i]);
}
y[0]=y[0]+1;
for(i=0;i<=n-1;i++)
{
if (y[i]==2)
{
y[i]=0;
y[i+1]=y[i+1]+1;
}
}
System.out.println();
System.out.print("\nThe 2's Complement is :: ");
for(i=n-1;i>=0;i--)
{
System.out.print(y[i]);
}
System.out.println();
int sub[]= new int [n];
for(i=0;i<=n-1;i++)
{
sub[i]=x[i]+y[i];
if(sub[i]==2)
{
sub[i]=0;
if(i<n-1)
x[i+1]=x[i+1]+1;
}
if (sub[i]==3)
{
sub[i]=1;
if(i<n-1)
x[i+1]=x[i+1]+1;
}
}
System.out.print("\nSubstraction is :: ");
for(i=n-1;i>=0;i--)
{
System.out.print(sub[i]);
}
System.out.println();
}
}
/*
OUTPUT:
Enter the no. of bits
8
Enter two nos.
6
3
The binary form of the two nos. is
00000110
00000011
The 1's complement is
11111100
The 2's complement is
11111101
Substraction is
00000011
*/
Comments
Post a Comment