a<< n or -a<<n
import java.io.*;
class MyFirstProg
{
public static void main(String args[])
{
int i=31,x=1,sum=0,j;
int bin[] = new int[32];
int bin1[] = new int[32];
Console con = System.console();
System.out.print("Enter a Value");
int num = Integer.parseInt(con.readLine());
System.out.print("Enter a Shift");
int b = Integer.parseInt(con.readLine());
if(num>0)
{
// convert decimal to binary and print
while(num!=0)
{
bin[i]=num%2;
num=num/2;
i--;
}
for(i=31;i>=0;i--)
{
System.out.print(bin[i]);
}
System.out.println();
// left shift and print
for(i=31,j=31-b;j>=0;i--,j--)
{
bin1[j]=bin[i];
}
for(i=31;i>=0;i--)
{
System.out.print(bin1[i]);
}
System.out.println();
// convert binary to decimal
for(i=31;i>=0;i--)
{
sum=sum+x*bin1[i];
x=x*2;
}
System.out.println("Number after Shift"+sum);
}
else
{
// convert binary to decimal and print
num=num*(-1);
while(num!=0)
{
bin[i]=num%2;
num=num/2;
i--;
}
for(i=31;i>=0;i--)
{
System.out.print(bin[i]);
}
System.out.println();
// 1's and 2's compliment and print
for(i=31;i>=0;i--)
{
if(bin[i]==0)
bin[i]=1;
else
bin[i]=0;
}
for(i=31;i>=0;i--)
{
System.out.print(bin[i]);
}
System.out.println();
for(i=31;i>=0;i--)
{
if(bin[i]==1)
bin[i]=0;
else
{
bin[i]=1;
break;
}
}
for(i=31;i>=0;i--)
{
System.out.print(bin[i]);
}
System.out.println();
// left shift and print
for(i=31,j=31-b;j>=0;i--,j--)
{
bin1[j]=bin[i];
}
for(i=31;i>=0;i--)
{
System.out.print(bin1[i]);
}
System.out.println();
// 1's and 2's compliment and print
for(i=31;i>0;i--)
{
if(bin1[i]==0)
bin1[i]=1;
else
bin1[i]=0;
}
i=31;
while(i>=0)
{
if(bin1[i]==1)
bin1[i]=0;
else
{
bin1[i]=1;
break;
}
i--;
}
for(i=31;i>=0;i--)
{
System.out.print(bin1[i]);
}
System.out.println();
// binary to decimal convert
for(i=31;i>0;i--)
{
sum=sum+x*bin1[i];
x=x*2;
}
sum=sum*(-1);
System.out.println("Number after Shift"+sum);
}
}
}
Comments
Post a Comment