mathajax

Signed and Unsigned Decimal to Binary number

It explains how to converts a number from decimal number (base 10) to binary numbers (base 2). The decimal value can be a unsigned (positive) or a signed (negative) decimal numbers. The binary operation, 2's complements used in conversion signed decimal value to binary.

signed decimal to binary conversion

signed/unsigned decimal to Binary Conversion - algorithm

  • Initialize binary-bits array
  • Read a decimal-value (positive or negative)
  • value2 = abs(decimal-value)
  • binary-bits = converts value2 into binary
  • IF decimal-value >= 0
  •    print binary-bits
  • ELSE  binary-bits_2'sc = take 2's_complement on binary-bits
  •    print binary-bits_2'sc

unsigned decimal to Binary Conversion - example

   The given positive decimal value is  decval=5; converts into binary  4 bits             
        decimal to binary 4 bit conversion steps
              1. bits = decval % 2;  
              2. decval = decval/2

           bits[4]  = 5  % 2  = 1;  decval = 5/2 = 2
           bits[3]  = 2  % 2  = 0;  decval = 2/2 = 1
           bits[2]  = 1  % 2  = 1;   decval = 1/2 = 0
           bits[1]  = 0  % 2  = 0;   decval = 1/2 = 0

      IF the decimal value is positive , no need 2's complement operation     
           then conversion result is binary bits = 0101              
      

signed decimal to Binary Conversion - example

   The given negative decimal value is  decval=-5; converts into binary  4 bits             

        decimal to binary 4 bit conversion steps
              1. take abs(decval)
              2. bits = decval % 2;  
              3. decval = decval/2
           
           decval=abs(decval) =5

           from above positive decimal to binary conversion  
                    bits[1-4]  = 0101  
          

  IF the given decimal value is negative, then takes 2's complement operation 
                                                 on converted binary bits
        
        2's complement on binary bits steps
            1. take 1's complement 
            2. binary addition  - add 1 with 1's complement result 
   
         Decimal to binary conversion results bits = 0101

            1's complement on bits 0101  = 1010

            add 1 with 1's complement results

                                     1010
                                     0001
                                    -------  
                  2's complement =   1011 
            
       signed decimal value -5 binary bits is =1011              
      

signed/unsigned Decimal To Binary Conversion - Java program

The Java program converts positive or negative decimal into binary numbers. For simplification. It is limited to binary 4 bits conversion. therefore, a decimal positive value must be between [0 to 8] and negative value between [-1 to -7].

 
import java.util.Scanner;

public class SignDecimal 
{
 
     public static String int2binary(int decval)
      {  
        StringBuffer sbuf =new StringBuffer();
        int nbits=4;
  
       for(int n=0;n<nbits;n++) 
        {
        sbuf.append( decval % 2 ) ;
        decval = decval /2;      
        }      
       return sbuf.reverse().toString();
    }
   
    public static void main(String[] args) 
     {    
      Scanner sc=new Scanner(System.in);
      System.out.println("signed and unsigned decimal to Binary Conversion");
  
      System.out.println("Enter Integer between [-7 to 8] ");  
      int val=sc.nextInt();
      sc.close();
  
      String binary="";
     if ( val < 0  ) 
      {    
       int val2 = Math.abs(val);
       binary=SignDecimal.int2binary(val2);     
       binary=Complement.twos(binary);       
     }
    else
    {
     binary=SignDecimal.int2binary(val);
    }
  
    System.out.println("Integer value :" + val);
    System.out.println("Binary Bits : " + binary);      
  }

}


 

signed & unsigned Decimal To Binary Conversion - Java program output


signed & unsigned decimal to Binary Conversion
Enter Integer between [-7 to 8] 
-6
Integer value :-6
Binary Bits : 1010

signed & unsigned decimal to Binary Conversion
Enter Integer between [-7 to 8] 
6
Integer value :6
Binary Bits : 0110

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

Chebyshev distance between two points

Binary 1's and 2's Complement