Represent positive and negative numbers in two's complement

12.1.1.4 represent positive and negative numbers in two's complement and specify the range of n bits.  

12.1.1.5 perform subtraction using two's complement 

Represent positive and negative numbers in two's complement

Problem. The new car move straight on and use posive speed but when the car move back what speed it has?

If the value of miles on the counter of the car is 000000 and we go back one mile, then the counter will show the value 999999, but we cannot say that we have driven 999999 miles :-)
We actually drove 1 mile. How is this issue solved in the representation of binary numbers in a computer? Let's figure it out.

Signed and unsigned binary numbers.

An unsigned representation of a binary number can only represent positive numbers

A signed representation can represent both positive and negative numbers.
For example, data type shotint has range [-128..127]. 

Two's complement

Two's complement binary works in a similar way to numbers on an analogue counter. Moving the car forwards one give us number 000 001, turn back one - 999 999. It means 999 999 is interpreted as -1.

If in denary 000 000 - 1 = 999 999 

What result in binary 0000 0000 - 1?

Answer
  • 0000 0000 - 1 = 1111 1111
  • 0000 0000 - 2 = 1111 1110
  • 0000 0000 - 3 = 1111 1101

For signed representation is used sign bit

Question. How represent min and max number of data type shortint
  • Answer.
    -128=1000 0000
    127=0111 1111

Converting a negative decimal number to binary

Start by working out the positive equivalent of the number, flip all of the bits and add 1.

For example,

Negative decimal number -9
Represent positive binary 0000 1001
Flip the bits 1111 0110
Add one               1
Result 1111 0111

 

Converting a negative two's complement binary number to decimal

The same method work the other way. Flip all of the bits and add 1. Then work out the result in decimal using the normal method.

For example, to convert the binary number 1110 0101 to decimal:

Negative binary number 1110 0101
Flip the bits 0001 1010
Add one                1
Convert -0001 1011
Result  -27

Binary substruction using two's complement

Binary substraction is best done by using the negative two's complement number and then adding the second number.

For example, decimal 17 - 14 would be:

14 = 0000 1110
-14 = 1111 0010
17 = 0001 0001
17 + (-14) = (1) 0000 0011

The carry on the addition is ignored, and the correct answer is given.

Additionally Addition and Subtraction using 2's complement

Questions:

  1. What difference between signed and unsigned binary numbers?
  2. What is the largest positive decimal value that can be represented using 8-bit two's complement binary?
  3.  

Exercises:

Ex. 1

Ex. 2

Exam questions:

Категория: Representing numbers | Добавил: bzfar77 (04.02.2021)
Просмотров: 129 | Теги: sign bit, two's complement, negative number, unsigned binary, signed binary | Рейтинг: 5.0/1
Всего комментариев: 0
avatar