Converting between integer data types

  • Recall:  

    • There are many different 2s complement codes

    Example: in Java

    • The 8 bits 2s complement codes is used in the byte data type
    • The 16 bits 2s complement codes is used in the short data type
    • The 32 bits 2s complement codes is used in the int data type
    • The 64 bits 2s complement codes is used in the long data type

  • Conversion between different 2s complement codes

    • When a variable is assigned to another variable of a different data type, the 2s complement code must be converted

    Example:

       Value      8 bits 2s compl code     16 bits 2s compl code
      -------------------------------------------------------------
        -3           11111101               1111111111111101
    

Converting between integer data types

  • Example of a Java program when conversion of 2s complement codes takes place:

       public static void main(String[] arg) 
       {
          byte  b;
          short s;
          int   i; 
    
          b = -3;   // Stores   11111101  in b
          s = b;    // Converts 11111101 to 1111111111111101
          i = b;    // Converts 11111101 to 11111111111111111111111111111101
       }
    

DEMO: /home/cs255001/demo/java/ShowIntConv.java

The need to convert between different data representations

Background information on the CPU:

  • The CPU performs arithmetic (and logic) operations

  • Arithmethic (and logic) operations uses two operate on binary numbers in the CPU

  • Circuitries in the CPU can only operate on binary numbers of the same length

    Example:

        00000011  +  00000100
        0000000000001000  -  0000000000000111
    

Consequently:

  • When we operate on values from 2 different representations:

    • We must convert one representation into the other data representation before we can perform the operation.

The data representation conversion problem explained in diagrams

The computer can represent signed values using the 8 bits 2s complement code:

The computer can perform arithmetic (e.g.: +) operation on two 8 bits 2s complement codes

The data representation conversion problem explained in diagrams

The computer can represent signed values using the 16 bits 2s complement code:

The computer can perform arithmetic (e.g.: +) operation on two 16 bits 2s complement codes

The data representation conversion problem explained in diagrams

However:

The computer can not perform arithmetic (e.g.: +) operation on mixed 2s complement codes

The data representation conversion problem explained in diagrams

Solution:

The computer first converts one 2s complement represention to the other 2s complement represention

The goal of a conversion operation
 

The goal of conversion is:

  • Find the representation in another encoding method that represent the same value (or approximately the same value) as the original encoding method

  • Examples:

                   8 bits                16 bits
       Value       2s complement code    2s complement code
      ------------------------------------------------------
         3         00000011              0000000000000011
        -3         11111101              1111111111111101
    

  • Question:

    • How do you convert between two different 2s complement codes ???

How to convert a byte representation to an int representation

List of values and their 2s compl representation in byte and int data type:

   Value   byte repr      int repr
  -------  ----------   --------------------------------- 
   127     01111111     00000000000000000000000001111111
   ...
     3     00000011     00000000000000000000000000000011
     2     00000010     00000000000000000000000000000010
     1	   00000001     00000000000000000000000000000001
     0	   00000000     00000000000000000000000000000000
    -1	   11111111     11111111111111111111111111111111
    -2	   11111110     11111111111111111111111111111110
    -3	   11111101     11111111111111111111111111111101
   ...
  -128     10000000     11111111111111111111111110000000
   

Can you see how to convert an byte repr of some value into an int repr ?

How to convert a byte representation to an int representation

Conversion: Extend the sign bit of the byte representation to fill 32 bits

The sign bit is the left-most bit in the byte representation

   Value   byte repr      int repr
  -------  ----------   --------------------------------- 
   127     01111111     00000000000000000000000001111111
   ...
     3     00000011     00000000000000000000000000000011
     2     00000010     00000000000000000000000000000010
     1	   00000001     00000000000000000000000000000001
     0	   00000000     00000000000000000000000000000000
    -1	   11111111     11111111111111111111111111111111
    -2	   11111110     11111111111111111111111111111110
    -3	   11111101     11111111111111111111111111111101
   ...
  -128     10000000     11111111111111111111111110000000
   

We call this operation: sign extension

How to convert an int representation to a byte representation

Notice that:
     for the value in the range [−128, 127], the last 8 bits in the int repr and byte repr are equal:

   Value   byte repr      int repr
  -------  ----------   --------------------------------- 
   ...
   128     Not possible 00000000000000000000000100000000
   127     01111111     00000000000000000000000001111111
   ...
     3     00000011     00000000000000000000000000000011
     2     00000010     00000000000000000000000000000010
     1     00000001     00000000000000000000000000000001
     0     00000000     00000000000000000000000000000000
    -1     11111111     11111111111111111111111111111111
    -2     11111110     11111111111111111111111111111110
    -3     11111101     11111111111111111111111111111101
   ...
  -128     10000000     11111111111111111111111110000000
  -129     Not possible 11111111111111111111111101111111
   ...

How to convert an int representation to a byte representation

Conversion: truncate the int repr to a byte by keeping the right-most 8 bits

However: this conversion is only safe when the value of the int repr is in the range [−128, 127]

   Value   byte repr      int repr
  -------  ----------   --------------------------------- 
   ...
   128     Not possible 00000000000000000000000100000000
   127     01111111     00000000000000000000000001111111
   ...
     3     00000011     00000000000000000000000000000011
     2     00000010     00000000000000000000000000000010
     1	   00000001     00000000000000000000000000000001
     0	   00000000     00000000000000000000000000000000
    -1	   11111111     11111111111111111111111111111111
    -2	   11111110     11111111111111111111111111111110
    -3	   11111101     11111111111111111111111111111101
   ...
  -128     10000000     11111111111111111111111110000000
  -129     Not possible 11111111111111111111111101111111
   ...   

Where do you find int to byte conversion operations used ?
 

One place where int to byte conversion (and vice versa) is performed is in assignment statements:

 int i;
 byte b;

 i = b;  // Converts byte repr in b tnto int repr
         // before storing the representation in i

 b = (byte) i;  // Converts int repr in i tnto byte repr
                // before storing the representation in b  
   

Quiz on int --> byte casting

Quiz:

  • Given that: 4100 (decimal) is represented by the 2s complement code 00000000000000000001000000000100.

    What is the value of the byte variable b in this statement:

            int i = 4100;
            byte b;
      
            b = (byte) i;                                   
      

Quiz on int --> byte casting

Quiz:

  • Given that: 4100 (decimal) is represented by the 2s complement code 00000000000000000001000000000100.

    What is the value of the byte variable b in this statement:

            int i = 4100;
            byte b;
      
            b = (byte) i;                                   
      

Answer:  4 (because b contains 00000100)
Demo: /home/cs255001/demo/java/JavaByteIntCast.java

How to convert a short representation to an int representation

List of values and their 2s compl representation in short and int data type:

   Value   short repr              int repr
  -------  -----------------    --------------------------------- 
   32767   0111111111111111     00000000000000000111111111111111
   ...
     3     0000000000000011     00000000000000000000000000000011
     2     0000000000000010     00000000000000000000000000000010
     1	   0000000000000001     00000000000000000000000000000001
     0	   0000000000000000     00000000000000000000000000000000
    -1	   1111111111111111     11111111111111111111111111111111
    -2	   1111111111111110     11111111111111111111111111111110
    -3	   1111111111111101     11111111111111111111111111111101
   ...
  -32768   1000000000000000     11111111111111111000000000000000
   

Can you see how to convert an short repr of some value into an int repr ?

How to convert a short representation to an int representation

Conversion: Extend the sign bit of the short representation to fill 32 bits

The sign bit is the left-most bit in the short representation

   Value   short repr              int repr
  -------  -----------------    --------------------------------- 
   32767   0111111111111111     00000000000000000111111111111111
   ...
     3     0000000000000011     00000000000000000000000000000011
     2     0000000000000010     00000000000000000000000000000010
     1	   0000000000000001     00000000000000000000000000000001
     0	   0000000000000000     00000000000000000000000000000000
    -1	   1111111111111111     11111111111111111111111111111111
    -2	   1111111111111110     11111111111111111111111111111110
    -3	   1111111111111101     11111111111111111111111111111101
   ...
  -32768   1000000000000000     11111111111111111000000000000000
   

This operation is: sign extension

How to convert an int representation to a short representation

Notice that:
     for the value in the range [−32768, 32767], the last 16 bits in the int repr and short repr are equal:

   Value   short repr              int repr
  -------  -----------------    --------------------------------- 
   32767   0111111111111111     00000000000000000111111111111111
   ...
     3     0000000000000011     00000000000000000000000000000011
     2     0000000000000010     00000000000000000000000000000010
     1	   0000000000000001     00000000000000000000000000000001
     0	   0000000000000000     00000000000000000000000000000000
    -1	   1111111111111111     11111111111111111111111111111111
    -2	   1111111111111110     11111111111111111111111111111110
    -3	   1111111111111101     11111111111111111111111111111101
   ...
  -32768   1000000000000000     11111111111111111000000000000000
   

How to convert an int representation to a short representation

Conversion: truncate the int repr down to 16 bits keeping the right-most bits

However: this conversion is only safe when the value of the int repr is in the range [−32768, 32767]

   Value   short repr              int repr
  -------  -----------------    --------------------------------- 
   32767   0111111111111111     00000000000000000111111111111111
   ...
     3     0000000000000011     00000000000000000000000000000011
     2     0000000000000010     00000000000000000000000000000010
     1	   0000000000000001     00000000000000000000000000000001
     0	   0000000000000000     00000000000000000000000000000000
    -1	   1111111111111111     11111111111111111111111111111111
    -2	   1111111111111110     11111111111111111111111111111110
    -3	   1111111111111101     11111111111111111111111111111101
   ...
  -32768   1000000000000000     11111111111111111000000000000000
   

Quiz on int --> short casting

Quiz:

  • Given that: 262150 (decimal) is represented by the 2s complement code 00000000000001000000000000000110.

    What is the value of the short variable s in this statement:

            int i = 262150;
            short s;
      
            s = (short) i;                               
      

Quiz on int --> short casting

Quiz:

  • Given that: 262150 (decimal) is represented by the 2s complement code 00000000000001000000000000000110.

    What is the value of the short variable s in this statement:

            int i = 262150;
            short s;
      
            s = (short) i;                               
      

Answer:  6 (because s contains 0000000000000110)
Demo: /home/cs255001/demo/java/JavaShortIntCast.java