two's complement,2的补码

Let's start with one question.

Q: What's the output of below program?

#include <stdio.h>

void main(void)
{
    char a = -1;
    printf("a = %d\n", (unsigned char)a);
}

Hope the rest of this article can help you answer this question. 😃

Q: What is two's complement?

The two's complement of a is 2^N-a.

Q: What is one's complement?

The one's complement of a is (2^N-1)-a. (2^N-1) is all 1, e.g. 2^4-1 is 1111. So this is called one's complement.

Q: In address 0x100, data 1010 1100 (let's name the value as a) is stored. From the value, can we know it's signed or unsigned type?

The answer is definitely NO. a can be considered as unsigned char (value will be -84), it can be also considered as signed char (value will be 172). There is also no another flag indicating us how to consider it, so we don't know the memory a should be considered as signed or unsigned.

We don't know, and the computer also don't know.
But CPU has two flags (OF and CF), the overflow flag based on signed type is OF, the flag based on unsigned type is CF.
Except the overflow flag, add and sub operation don't distinguish type of data.

Example 1

For example,

  • memory address 0x001 stores 1111 1100(let's name it as c),
  • memory address 0x002 stores 0000 0101 (let's name it as d).
    When the CPU do c+d, what will happen?
                signed   unsigned    name
   1111 1100     -4       252     :   c
+  0000 0101      5        5      :   d 
--------------
  10000 0001      1       257
                 No       Yes     :  overflow?

In upper example,

  • the result doesn't overflow if we consider them as signed number. So the result is right.
  • the result overflows if we consider them as unsigned number, so the result is wrong.

Which result we should use?
The answer depends on you, not depend on the CPU. The good thing is that the CPU will tell you the overflow flag based on signed and unsigned together.

Example 2

Another example as below.

#include <stdio.h>

int main(void)
{
    /* range of char is [-128, 127], so `a` doesn't overflow.
       The `a` in memory is `0111 1111`(MSB -> LSB in binary format).
     **/
    char a = 127;

    /* C compiler knows `a` is `char` type, so it will decode
       memory value of `a` according to `char` type.
     **/
    printf("char a = 127; ==> a is %d\n", a);
    
    /* When perform `a = a + 1`, CPU just add `1` to the memory
       value of `a`. Value of `a` in memory is `1000 0000` after add operation.
     **/
    a = a + 1;
    printf("char a = a+1; ==> \n");

    /* C compiler will decode the memory value of `a` as `char` type.
       `1000 0000` in `char type` is `-128`.
     **/
    printf("\t1). a is %d (overflowed!!)\n", a); 

    /* C comiler will decode the memory value of `a` as `unsigned char` type,
       because we explicitly say by `(unsigned char)a`.
       `1000 0000` in `unsigned char` type is `128`.
     **/
    printf("\t2). (unsigned char)a is %d\n", (unsigned char)a); 

}

The output of upper program as below.

char a = 127; ==> a is 127
char a = a+1; ==> 
    1). a is -128 (overflowed!!)
    2). (unsigned char)a is 128

本文为作者原创,允许转载,但必须注明原文地址:https://www.cnblogs.com/byronxie/p/10117265.html


本文的数字签名如下:

MGUCMQDYymaHZ/G/34Ds5GegHhsHgdMsoiGJ+Fu/An/IDiL3O1TwxbiPWMlTvCvdBiM9wR0CMDVUVIgk/YfWRRzyHQkKGp3DSiO5DKLpm8XSQg8HX0Ua+BcdHLOWiZOjPj/RzgmjNA==
--- 2019-7-20 9:34:50

数字签名验证方法

posted @ 2018-12-13 23:56  佰夜  阅读(1721)  评论(0编辑  收藏  举报