The 2s complement code
 

The 2s complement code:

  • The 2s complement code is a similar encoding technique as the 10s complement code, but uses binary numbers

  • All computers today uses the 2s complement code to represent signed integer (whole) values stored inside the computer

  • Computer uses the following 2s complement codes:

    • 8 bits 2s complement code (e.g.: Java's byte data type)
    • 16 bits 2s complement code (e.g.: Java's short data type)
    • 32 bits 2s complement code (e.g.: Java's int data type)
    • 64 bits 2s complement code (e.g.: Java's long data type)

Intro to the 2s complement code
 

The 2s complement code is based on modular arithmetic....

Overview:

  • The 2s complement code uses a fixed number of (binary) digits

  • Each binary number of the 2s complement code is mapped to a signed value

    The mapping is exactly like the one used in the 10s complement code

    Only difference: use binary arithmetic

Let's construct the 8 bits 2s complement code
 

The 8 bits 2s complement code is a mapping between any 8 bits number and its signed value:

   3 digit number        Signed value
   --------------      --------------    
    0000000       <--->    ...
    0000001       <--->    ...
     

    1111111       <--->    ...
   

The mapping must has desirable computational properties !!

We can achieve these desriable computation properties using "wrap-around" arithmetic in binary - just like the 10s compl code

Operation of the car's odometer
 

Suppose we have a binary number odometer that is very special:

  • When the car goes forward, its odometer will increase by 1 after driving 1 mile

      forward 1 mile:  

  • When the car goes backwards (i.e.: drive in reverse), its odometer will decrease by 1 after driving 1 mile

      reverse 1 mile:  

$64,000 question

Question:

  • Starting with odometer reading 11111111 (binary):
                
    we drive forward 1 mile.

    What's the new odometer reading ?

$64,000 question

Question:

  • Starting with odometer reading 11111111 (binary):
                
    we drive forward 1 mile.

    What's the new odometer reading ?

Answer:   11111111 + 1 = 00000000 (because the odometer has 8 bits and cannot display 9 bits)

                

$64,000 question

Question:

  • Starting with odometer reading 00000000:
                
    we drive in reverse 1 mile.

    What's the new odometer reading ?

$64,000 question

Question:

  • Starting with odometer reading 00000000:
                
    we drive in reverse 1 mile.

    What's the new odometer reading ?

Answer:   00000000 − 1 = 11111111 !!!

                

The mapping between the 8 bits 2s compl code and their signed values
 

8 bits can represent 28 = 256 different values

Here is the mapping between 8 bits 2s compl code and the signed values between [−128, 127]:

  2s comp       Intrinsic        Decimal 
  Code          value            sign-magnitude repr
  ====================================================
  10000000                        -128  <--- largest negative value using 8 bits (−27)
  10000001                        -127
  .....         ....              ....
  11111000      ••••••••          -8
  11111001      •••••••           -7
  11111010      ••••••            -6
  11111011      •••••             -5
  11111100      ••••              -4
  11111101      •••               -3
  11111110      ••                -2
  11111111                       -1
  00000000      ( )                0
  00000001                        1
  00000010      ••                 2
  00000011      •••                3
  00000100      ••••               4
  00000101      •••••              5
  00000110      ••••••             6
  00000111      •••••••            7
  00001000      ••••••••           8
  .....         ....               ....
  01111111                         127  <--- largest positive value using 8 bits (27-1)  

The mapping between the 8 bits 2s compl code and their signed values

The mapping is based on modular addition and subtraction that you can see if we show the mapping on a circle:

DEMO that shows that the computer uses 2s complement code
 

Program that shows the 2s complement code and their sign-magnitude representations:

  • /home/cs255001/demo/java/ShowByteAdd.java

Background info:

  • The System.out.print( ) method in the Java library prints/presents the 2s complement code in sign-magnitude representation

  • I wrote a PrintBits( ) method that prints the 2s compl code in binary

Try these inputs:

 a = 2, b = 1
 a = 2, b = -1 (adding neg value works - i.e.: without subtraction !)
 a = -1, b = 2

 a = 127, b = 1   (overflow !)