Decimal to Binary & Binary to Decimal - Bitwise Left-shift Operator
The Java code has two number conversion operations; first is decimal to binary conversion and second binary to decimal conversion. The bitwise left shift operator used in these both operation. Conditional switch selects the case, one of the two conversion operation is to carry out from choice input given to program. If choice is 1, switch selects case1 (binary to decimal conversion) and if choice is 2, switch selects case2 (decimal to binary conversion).
Binary to Decimal Conversion
Assume that, choice is 1 and switch selects binary to decimal conversion operation. The user is prompted enter 8 binary bits as input which is to be converted into a decimal value.Assume that, binary input given is 01100000 and it is stored into a string object “bstr” and initialize decval = 0.
bstr =”[01100000]” index start 0 from left to right.
decval =0;
iteration n=0
decval = decval <<1; decval leftshift by 1 bit (result decval=0)
if bstr[n] == 1 ; check bstr[n] ==’1’ if true , do binary OR operation with decval |1,otherwise move iteration
decval = decval | 1 ;
iteration n=1
decval = decval <<1; decval leftshift by 1 bit (decval =0)
if bstr[n] == 1 ; check bstr[n] ==’1’ if true , do binary OR operation with decval |1, otherwise move next iteration
decval = decval | 1 ; (decval =1)
iteration n=2
decval = decval <<1; decval leftshift by 1 bit and result (decval =2)
if bstr[n] == 1 ; check bstr[n] ==’1’ if true, do binary OR operation with decval |1, otherwise move next iteration
decval = decval | 1 ; ; result (decval=3)
The string bstr has none bits is one index from 3 to 7 and if condition becomes false from iteration n=3 to 7, therefore no chance for OR operation.
Iteration n=3
deval = devcal <<1 ; result (decval =6)
Iteration n=4
deval = devcal <<1 ; result (decval =12)
Iteration n=5
decval = devcal <<1 ; result (decval =18)
Iteration n=6
decval = devcal <<1 ; result (decval =48)
Iteration n=7
decval = devcal <<1 ; result (decval =96)
finally the result of deval =96
Decimal to Binary Conversion
Assume that, choice is 2 and switch selects decimal to binary conversion operation. The user is prompted enter decimal value between [0 - 255] as input, which is to be converted into 8 bit binary.
Assume that, binary input given is 143 and it is stored into decval and a stringbuffer object “bstr” initialized empty constructor.
Bstr =”” empty string
decval =143;
char c = ( decval & 128 ) ==128 ? ‘1’ : ‘0’
do binary AND operation decval & 128 and result compared with 128 if it is true, char c= ‘1’ otherwise char c= ‘0’.
iteration n=0
(decval & 128) == 128 ? ‘1’ :’0’ condition is true, char c set ‘1’
bstr.append(c) ; append char c into bstr
decval << 1 ; decval , left shift by 1 bit result (decval =30)
iteration n=1
(decval & 128) == 128 ? ‘1’ :’0’ condition is false, char c set ‘0’
bstr.append(c) ; append char c into bstr
decval << 1 ; decval , left shift by 1 bit result (decval =60)
The same process iterated until n< 8. The stringbuffer object bstr contains results of 8 bits binary.
bstr =”10001111”
binary to decimal conversion output
decimal to binary conversion output
Related post
Binary to Decimal Conversion
Assume that, choice is 1 and switch selects binary to decimal conversion operation. The user is prompted enter 8 binary bits as input which is to be converted into a decimal value.Assume that, binary input given is 01100000 and it is stored into a string object “bstr” and initialize decval = 0.
bstr =”[01100000]” index start 0 from left to right.
decval =0;
iteration n=0
decval = decval <<1; decval leftshift by 1 bit (result decval=0)
if bstr[n] == 1 ; check bstr[n] ==’1’ if true , do binary OR operation with decval |1,otherwise move iteration
decval = decval | 1 ;
iteration n=1
decval = decval <<1; decval leftshift by 1 bit (decval =0)
if bstr[n] == 1 ; check bstr[n] ==’1’ if true , do binary OR operation with decval |1, otherwise move next iteration
decval = decval | 1 ; (decval =1)
iteration n=2
decval = decval <<1; decval leftshift by 1 bit and result (decval =2)
if bstr[n] == 1 ; check bstr[n] ==’1’ if true, do binary OR operation with decval |1, otherwise move next iteration
decval = decval | 1 ; ; result (decval=3)
The string bstr has none bits is one index from 3 to 7 and if condition becomes false from iteration n=3 to 7, therefore no chance for OR operation.
Iteration n=3
deval = devcal <<1 ; result (decval =6)
Iteration n=4
deval = devcal <<1 ; result (decval =12)
Iteration n=5
decval = devcal <<1 ; result (decval =18)
Iteration n=6
decval = devcal <<1 ; result (decval =48)
Iteration n=7
decval = devcal <<1 ; result (decval =96)
finally the result of deval =96
Decimal to Binary Conversion
Assume that, choice is 2 and switch selects decimal to binary conversion operation. The user is prompted enter decimal value between [0 - 255] as input, which is to be converted into 8 bit binary.
Assume that, binary input given is 143 and it is stored into decval and a stringbuffer object “bstr” initialized empty constructor.
Bstr =”” empty string
decval =143;
char c = ( decval & 128 ) ==128 ? ‘1’ : ‘0’
do binary AND operation decval & 128 and result compared with 128 if it is true, char c= ‘1’ otherwise char c= ‘0’.
iteration n=0
(decval & 128) == 128 ? ‘1’ :’0’ condition is true, char c set ‘1’
bstr.append(c) ; append char c into bstr
decval << 1 ; decval , left shift by 1 bit result (decval =30)
iteration n=1
(decval & 128) == 128 ? ‘1’ :’0’ condition is false, char c set ‘0’
bstr.append(c) ; append char c into bstr
decval << 1 ; decval , left shift by 1 bit result (decval =60)
The same process iterated until n< 8. The stringbuffer object bstr contains results of 8 bits binary.
bstr =”10001111”
import java.util.Scanner;
public class LeftShiftOpr
{
public static final int nbits=8;
public static String dec2bin(int decval) {
StringBuffer bufstr = new StringBuffer();
for(int n=0;n<LeftShiftOpr.nbits;n++)
{
char c=( (decval & 128) == 128) ? '1' : '0';
bufstr.append(c);
decval <<=1;
}
return bufstr.toString();
}
public static int bin2dec(String bstr)
{
int decval=0;
for(int n=0;n<LeftShiftOpr.nbits;n++)
{
decval <<= 1;
if ( bstr.charAt(n) == '1' )
decval = decval | 1;
}
return decval;
}
public static void main(String[] args)
{
int decval=0; String bstr="";
System.out.println("nt Decimal to Binary using <<
LeftShift Operator");
System.out.println("n Select Choice 1 - Binary2Dec
2- Dec2Binary n ");
Scanner sc=new Scanner(System.in);
int choice =sc.nextInt();
switch ( choice ) {
case 1:
System.out.println("Enter 8 bit Binary numbers");
bstr= sc.next();
decval = LeftShiftOpr.bin2dec(bstr);
System.out.println("nDecimal value :" + decval);
break;
case 2:
System.out.println("Enter Decimal number range [0 - 255]");
decval = sc.nextInt();
bstr = LeftShiftOpr.dec2bin(decval);
System.out.println("nBinary bits :" + bstr);
break;
}
sc.close();
}
}
binary to decimal conversion output
Decimal to Binary using << LeftShift Operator Select Choice 1 - Binary2Dec 2- Dec2Binary 1 Enter 8 bit Binary numbers 01000011 Decimal value :67
decimal to binary conversion output
Decimal to Binary using << left-shift Operator Select Choice 1 - Binary2Dec 2- Dec2Binary 2 Enter Decimal number range [0 - 255] 143 Binary bits :10001111
Related post
Comments
Post a Comment