mathajax

Binary to Decimal & Decimal to Binary Bitwise Right-shift Operator

The Java code has two number conversion operations; one is decimal to binary conversion and second binary to decimal conversion. These two operations are done by bitwise right shift operator.

 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).


decimal to binary by right shift operator
Decimal 2 Binary by Right shift 

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 011000000 and it is stored into a string object “bstr” and initialize decval  = 0.  

Bstr  =”[01100000]”   characters indexed (0,1,2,.)  from left to right 
decval =0;
the iteration variable n start from 8-1  to 0 for rightshift operator,>>.

If    bstr[n]  == 1 ;    check bstr[n] ==’1’  if true ,   do binary OR operation  with decval |128, 


iteration  n=7
decval   = decval>>1;     decval  rightshift by 1 (move  1 bit  toward right)  ( result decval=0)
  if bstr[7]  == ‘1’ ;         condition is false ,   move to next iteration
                                                    
iteration  n=6
decval   = decval >>1;     decval  rightshift by 1  ( result decval=0)
if bstr[6]  == ‘1’ ;               condition is false ,   move to next iteration  move next  iteration

The string bstr has none bits is one index from 7 to 3 (reverse order) and condition becomes false from iteration n=7 to 3, therefore no chance for OR operation.

Iteration  n=2
 decval   = decval >>1;     decval  rightshift by 1  ( result decval=0)
 if bstr[2]  == ‘1’ ;               condition is true ,  do binary OR operation  with decval |128, 
   decval = decval |128;
 move to next iteration 

Iteration  n=1
 decval   = decval >>1;     decval  rightshift by 1  ( result decval=64)
 if bstr[1]  == ‘1’ ;               condition is true ,  do binary OR operation  with decval |128, 
   decval = decval |128;
 move to next iteration 

Iteration  n=0
decval   = decval >>1;     decval  rightshift by 1  ( result decval=96)
 if bstr[0]  == ‘1’ ;               condition is false,  
move to next iteration  and loop end.

Finally the result decval =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 & 1 ) ==1  ? ‘1’ : ‘0’
set char c= ‘1’ or  ‘0’ by doing binary AND operation   decval  &  1 and  result compared with 1  if it is true, c=’1’ otherwise c=’0’.

iteration  n=0
(decval  & 1) == 1 ? ‘1’ :’0’     condition is true, char c set ‘1’ 
bstr.insert(c,0)   ;       insert  c=’1’ into bstr at index 0   
decval =decval >> 1  ;       decval  right shift by 1 bit and result decval =71

iteration  n=1
(decval  & 1) == 1 ? ‘1’ :’0’     condition is true, char c set ‘1’ 
bstr.insert(c,0)   ;       append  c=’1’ into bstr at index 0   
decval =decval >> 1 ;     decval  right shift by 1 bit  and result decval =35

iteration  n=2
(decval  & 1) == 1 ? ‘1’ :’0’     condition is true, char c set ‘1’ 
bstr.insert(c,0)   ;       append  c=’1’ into bstr at index 0   
decval =decval >> 1 ;     decval  right shift by 1 bit and result  decval =17

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 RightShiftOpr 
  {
 
 public final  static int nbits=8;
 
 public static String dec2bin(int decval) 
 {  
      StringBuffer bufstr = new StringBuffer();        
  for(int n=0;n<RightShiftOpr.nbits;n++) 
  {
     char c=( (decval & 1) == 1) ? '1' : '0';
     bufstr.insert(0, c);
     decval >>=1;
  }
    return bufstr.toString();
 }

 public static int bin2dec(String bstr) 
 {
  int decval=0;
  for(int n=RightShiftOpr.nbits-1;n>=0;n--) 
  {
     decval >>= 1;
     if ( bstr.charAt(n) == '1' )
      decval =  decval | 128;              
  }
  return decval;
 }
 
public static void main(String[] args) 
{
            
    Scanner sc=new Scanner(System.in);               
    int decval=0; String bstr="";   
    System.out.println("nt Decimal to Binary using >> 
                                             RightShift Operator");
    System.out.println("n Select Choice nt 1 - Binary2Dec 
                                             2- Dec2Binary  ");
    int choice =sc.nextInt();
         
       switch ( choice ) {
           
    case 1:
       System.out.println("Enter 8 bit Binary numbers");
       bstr= sc.next();
       decval = RightShiftOpr.bin2dec(bstr);
       System.out.println("nDecimal value :" + decval);
       break; 
     case 2:
       System.out.println("Enter Decimal number range [0 - 255]");
       decval = sc.nextInt();
       bstr = RightShiftOpr.dec2bin(decval);
       System.out.println("nBinary bits :" + bstr);
       break;          
     }
      
     sc.close();   

 }

}



Output
  Positive Integer to Binary using >>RightShift Operator

 Select Choice 
  1 - Binary2Dec 
  2- Dec2Binary 
 
1
Enter 8 bit Binary numbers
10001111

Decimal value :143

Output 2
 Positive Integer to Binary using >> RightShift Operator

 Select Choice 
  1 - Binary2Dec 
  2- Dec2Binary 
 
2
Enter Decimal number range [0 - 255]
19

Binary bits :00010011


Related post

Comments

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Matrix Determinant by Upper Triangular Matrix

Chebyshev distance between two points