Both ' s complement

Source: Internet
Author: User

Contents and Introduction
    • Contents and Introduction
    • Conversion from "s complement
    • Conversion to both ' s complement
    • Arithmetic with both ' s complement
    • Why inversion and Adding one Works

Both ' s complement is not a complicated scheme and are not well served by anything lengthly. Therefore, after the introduction, which explains what's complement is and how to use it, there is mostly examples.

The complement is the every computer I know from chooses to represent integers. To get the "s complement negative notation of an integer, you write the" number in binary. Invert the digits, and add one to the result.

Suppose we ' re working with 8 bit quantities (for simplicity ' s sake) and suppose we want to find how-28 would is expressed In both ' s complement notation. First we write out, in binary form.

00011100

Then we invert the digits. 0 becomes 1, 1 becomes 0.

11100011

Then we add 1.

11100100

That's how one would write-28 in 8 bit binary.

Conversion from "s complement

Use the number 0xFFFFFFFF as an example. In binary, which is:

1111 1111 1111 1111 1111 1111 1111 1111

What can we say on this number? It's first (leftmost) bit is 1, which means that's represents a number that's negative. That ' s just the the-the-the-things is-in-the-complement:a leading 1 means the number is negative, a leading 0 means the N Umber is 0 or positive.

To see "What's This number" is a negative of, we reverse. But how does that? The class notes say (on 3.17) so reverse the sign you simply invert the bits (0 goes to 1, and 1 to 0) and add one to The resulting number.

The inversion of that binary number is, obviously:

0000 0000 0000 0000 0000 0000 0000 0000

Then we add one.

0000 0000 0000 0000 0000 0000 0000 0001

So the negative of 0xFFFFFFFF are 0x00000001, more commonly known as 1. So 0xFFFFFFFF is-1.

Conversion to both ' s complement

Note that this works both ways. If you have-30, and want to represent it in 2 ' s complement, you take the binary representation of 30:

0000 0000 0000 0000 0000 0000 0001 1110

Invert the digits.

1111 1111 1111 1111 1111 1111 1110 0001

and add one.

1111 1111 1111 1111 1111 1111 1110 0010

Converted back to Hex, this is 0xffffffe2. And indeed, suppose you has this code:

#include <stdio.h>int main () {    int myInt;    MyInt = 0xffffffe2;    printf ("%d\n", myInt);    return 0;}

That should yield an output of-30. Try it out if you like.

Arithmetic with both ' s complement

One of the nice properties of both ' s complement is that addition and subtraction are made very simple. With a system like the "s complement, the circuitry for addition and subtraction can unified, whereas otherwise they wou LD has to be treated as separate operations.

In the examples of this section, I do addition and subtraction in the complement, but you'll notice that every time I do Actual operations with binary numbers I am always adding.

Example 1

Suppose we want to add, numbers and together. If we ' re to use decimal, we see the sum is 81. But let's use binary instead, since that's what the computer uses.

1 1   
Carry Row
  0000 0000 0000 0000 0000 0000 0100 0101
(69)
+ 0000 0000 0000 0000 0000 0000 0000 1100
(12)
  0000 0000 0000 0000 0000 0000 0101 0001
(81)
Example 2

Now suppose we want to subtract 69. Now, 69-12 = 69 + (-12). To get the negative of the A we take it binary representation, invert, and add one.

0000 0000 0000 0000 0000 0000 0000 1100

Invert the digits.

1111 1111 1111 1111 1111 1111 1111 0011

and add one.

1111 1111 1111 1111 1111 1111 1111 0100

The last is the binary representation for-12. As before, we ll add the numbers together.

1111 1111 1111 1111 1111 1111 1    1   
Carry Row
  0000 0000 0000 0000 0000 0000 0100 0101
(69)
+ 1111 1111 1111 1111 1111 1111 1111 0100
(-12)
  0000 0000 0000 0000 0000 0000 0011 1001
(57)

We result in the which is 69-12.

Example 3

Lastly, we'll subtract from 12. Similar to We operation in Example 2, 12-69 = 12 + (-69). The ' s complement representation of the following. I assume you ' ve had enough illustrations of inverting and adding one.

1111 1111 1111 1111 1111 1111 1011 1011

So we add this number to 12.

111     
Carry Row
  0000 0000 0000 0000 0000 0000 0000 1100
(12)
+ 1111 1111 1111 1111 1111 1111 1011 1011
(-69)
  1111 1111 1111 1111 1111 1111 1100 0111
(-57)

This results in 12-69 = -57, which is correct.

Why inversion and Adding one Works

Invert and add one. Invert and add one. It works, and want to know. If you don ' t care, skip this, as it's hardly essential. This is a intended for those curious as-to-why, rather strange technique actually makes mathematical sense.

Inverting and adding one might sound like a stupid thing to does, but it ' s actually just a mathematical shortcut of a rather Straightforward computation.

Borrowing and subtraction

Remember The old trick we learned in first grade of "borrowing one's" from future Ten's places to perform a subtraction? You could not be, so I'll go over it. As an example, I-ll do 93702 minus 58358.

  93702-58358-------

Now, then, what's the answer to this computation? We ll start at the least significant digit, and subtract term by term. We can ' t subtract 8 from 2, so we'll borrow a digit from the next more significant place (the "tens Place") to make it mi NUS 8. Minus 8 is 4, and we note a 1 digit above the Ten's column to signify so we must remember to subtract by one on the N Ext iteration.

     1  93702-58358-------      4

This next iteration is 0 minus 5, and minus 1, or 0 minus 6. Again, we can ' t do 0 minus 6, so we borrow from the next most significant figure once + to make that minus 6, which is 4.

      93702-58358-------     44

This next iteration is 7 minus 3, and minus 1, or 7 minus 4. This is 3. We don ' t have to borrow this time.

      93702-58358-------    344

This next iteration is 3 minus 8. Again, we must borrow to make thi minus 8, or 5.

  1  93702-58358-------   5344

This next iteration is 9 minus 5, and minus 1, or 9 minus 6. This is 3. We don ' t have to borrow this time.

  1  93702-58358-------  35344

So 93702 minus 58358 is 35344.

Borrowing and it ' s relevance to the negative of a number

When you want to find the negative of a number, you take the number, and subtract it from zero. Now, suppose we ' re really stupid, like a computer, and instead of simply writing a negative sign in front of a number a WH En we subtract a from 0, we actually go through the steps of subtracting a from 0.

Take the following idiotic computation of 0 minus 3:

000000-    3------
    1000000-    3------     7
   11000000-    3------    97
  111000000-    3------   997
1111000000-    3------  9997

Et cetera, et cetera. We ' d wind up with a number composed of a 7 in the one's digit, a 9 in every digit more significant than the "place."

The same in Binary

We can do more or less the same thing with binary. In this example I use 8 bit binary numbers, but the principle are the same for both 8 bit binary numbers (chars) and Binary numbers (ints). I take the number, in 8 bit binary, that is 010010112, and subtract that from zero.

Sometimes I am in the position where I am subtracting 1 from zero, and also subtracting another borrowed 1 against it.< /p>

 00000000-01001011----------
Pre class= "notspaced" > 1 00000000-01001011----------1
 11       00000000-01001011----------
 111 00000000-01001011----------
  101 
 1111 00000000-01001011----------0101 
 11111 00000000-01001011----------10101 
 111111 00000000-01001011----------110101 
 1111111 00000000-01001 011----------0110101 
 11111111 00000000-01001011----------10110101 
td>

If We wanted we could go further, but there would is no point. Inside of a computer The result of this computation would is assigned to an eight bit variable, so any bits beyond the Eig HTH would be discarded.

With the fact that we'll simply disregard any extra digits on mind, what's difference would it make to the end result to hav E subtracted 01001011 from 100000000 (a one bit followed by 8 zero bits) rather than 0? There is none. If We do, we wind up with the same result:

11111111 100000000-01001011----------010110101

So-to-find the negative of a n-bit number in a computer, subtract the number from 0 or subtract it from 2n. In binary, the this power of both would be a one-bit followed by n zero bits.

In the case of 8-bit numbers, it'll answer just as well if we subtract our number from (1 + 11111111) rather than 100000 000.

         1+ 11111111-01001011----------

In binary, if we subtract a number a from a number of all 1 bits, what we ' re doing is inverting the bits of a. The subtract operation is the equivalent of inverting the bits of the number. Then, we add one.

So, to the computer, taking the negative of a number, that's, subtracting a number from 0, is the same as inverting the B Its and adding one, which are where the trick comes from.

Both ' s complement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.