smali

суббота, 24 сентября 2016 г.

One’s Complement and Two’s Complement


https://delightlylinux.wordpress.com/2014/10/13/binary-lesson-12-ones-complement-and-twos-complement/

One’s complement and two’s complement are two important binary concepts. Two’s complement is especially important because it allows us to represent signed numbers in binary, and one’s complement is the interim step to finding the two’s complement.

Two’s complement also provides an easier way to subtract numbers using addition instead of using the longer, more involving subtraction method discussed in Lesson 10.
We will save the two’s complement subtraction for another lesson, but here, we will look at what these terms mean and how to calculate them.

One’s Complement

If all bits in a byte are inverted by changing each 1 to 0 and each 0 to 1, we have formed the one’s complement of the number.
Original       One's Complement
-------------------------------
10011001  -->  01100110
10000001  -->  01111110
11110000  -->  00001111
11111111  -->  00000000
00000000  -->  11111111

Converting to one's complement.
Converting to one’s complement.
And that is all there is to it! One’s complement is useful for forming the two’s complement of a number.


Two’s Complement (Binary Additive Inverse)

The two’s complement is a method for representing positive and negative integer values in binary. The useful part of two’s complement is that it automatically includes the sign bit.
Rule: To form the two’s complement, add 1 to the one’s complement.
Step 1: Begin with the original binary value

    10011001  Original byte

Step 2: Find the one's complement

    01100110  One's complement

Step 3: Add 1  to the one's complement

    01100110  One's complement
  +        1  Add 1
 -----------
    01100111  <---  Two's complement

Converting to two's complement.
Converting to two’s complement.

What is -65d in binary?

Two’s complement allows us to represent signed negative values in binary, so here is an introductory demonstration on how to convert a negative decimal value to its negative equivalent in binary using two’s complement.
Step 1: Convert 65d to binary. Ignore the sign for now. Use the absolute value. The absolute value of -65d is 65d.
65d  -->  01000001b
Step 2: Convert 01000001 to its one’s complement.
01000001  -->  10111110
Step 3: Convert 10111110b to its two’s complement by adding 1 to the one’s complement.
    10111110
   +       1
   ---------
    10111111  <---  Two's complement
10111111b is -65d in binary. We know this it true because if we add 01000001b (+65d) to 10111111b (-65d) andignore the carry bit, the sum is 0, which is what we obtain if we add +65 + (-65) = 0.
    01000001  +65
 +  10111111  -65
---------------------
   100000000b   0d
   ^
   |
   Ignore the carry bit for now. What matters is that original number of bits (D7-D0) are all 0.
We will examine signed binary values in more detail later. For now, understand the difference between one’s complement and two’s complement and practice converting between them.

Practice

Convert to one’s complement:
  1. 1010
  2. 11110000
  3. 10111100 11000000
  4. 10100001
Convert to two’s complement:
  1. 1010
  2. 11110000
  3. 10000000
  4. 011111111
Convert these negative decimal values to negative binary using two’s complement:
  1. -192d
  2. -16d
  3. -1d
  4. -0d

Answers

One’s complement:
  1. 0101
  2. 00001111
  3. 01000011 00111111
  4. 01011110
Two’s complement:
  1. 0110 (1010 –> 0101 + 1 = 0110)
  2. 00010000
  3. 10000000 (Result is the same as the original value.)
  4. 10000001
Negative decimal to negative binary:
  1. 01000000b (192d = 11000000b –> 00111111 + 1 = 01000000b)
  2. 11110000b (16d = 00010000b –> 11101111 + 1 = 11110000b)
  3. 11111111b (1d = 00000001b –> 11111110 + 1 = 11111111b) Tricky? Before converting from binary to decimal, we must know ahead of time if the binary value is signed or not because a signed binary value will not convert properly using the place value chart we have seen so far. If seen by itself, 11111111b = 255d, not -1d. As a rule, assume that a binary value, such as 11111111b, is a positive integer unless context specifies otherwise. Since we are dealing with negative binary values in this problem set, then 11111111b is -1d, not 255d.
  4. 0. There is no such thing as negative zero (-0). Nothing is always nothing and does not have a sign. We can convert anyway: 0d = 00000000 –> 11111111 + 1 = 1 00000000b. We still arrive at 0 in binary for the eight relevant bits. Ignore the ninth carry bit.

Комментариев нет:

Отправить комментарий