C language - binary (original code, inverse code, complement code)

Integer occupies 4 bytes (32 bits)

#include<stdio.h>
int main()
{int a=10;
//00000000000000000000000000001010——原码
//00000000000000000000000000001010——反码
//00000000000000000000000000001010——补码
int b=-10;
//10000000000000000000000000001010——原码
原码——>补码:取反加一
//11111111111111111111111111110101——反码
//11111111111111111111111111110110——补码
return 0;
}

According to the positive or negative of a number, the original code is obtained by directly writing its binary representation.

What is stored in memory is two's complement.

The highest bit in binary is the sign bit, 0 means a positive number and 1 means a negative number.

The original code, inverse code, and complement code of positive and unsigned numbers are the same.

The original code, inverse code, and complement code of negative numbers need to be calculated:

The inverse code can  be obtained by inverting the sign bit of the original code while the other bits are reversed .

The complement code can be obtained by complement code +1 .

Formula: original code→complement code: take the inverse and add one

           Complementary code→Original code: Negate plus one/minus one and negate (both are ok)

Guess you like

Origin blog.csdn.net/outdated_socks/article/details/129456476