Go The original code, anti-code and complement of C language

Source: Internet
Author: User

Original code, anti-code and complement

1). Data is stored in memory in the form of binary storage.

int num = 10;

The original code, anti-code, and complement are binary. It's just a different representation of the binary.

The data is stored in complement binary.

2). 1 variables of type int. Occupies 4 bytes, 32 bits in memory.

00000000 00000000 00000000 00000000

Regardless of the positive or negative circumstances. 1 variables of type int can represent nearly 43e of data.

In order to be able to represent positive and negative. Use the highest to indicate the positive and negative of this number.

If the maximum is 0, then that number is 1 positive.

If the maximum is 1 then the number is 1 negative.

So, to represent only 31 bits of data. So, 1 variables of type int.

The minimum value is:-2147483648 The maximum value is: 2147483647

3). Original code

The highest bit represents the sign bit. The number of digits remaining. Is the absolute value of this number binary.

10 of the original code.

00000000 00000000 00000000 00001010

-8 of the original code.

10000000 00000000 00000000 00001000

Absolute value: The absolute value of a positive number is itself, minus the negative value.

-20.

10000000 00000000 00000000 00010100

4). Anti-code

The inverse code of a positive number is its original code.

The inverse code of a negative number is the symbol bit unchanged on the base of its original code, and the other bits are reversed.

10 of the anti-code:

10 The original code: 00000000 00000000 00000000 00001010

10 Anti-code: 00000000 00000000 00000000 00001010

-8

-8 of the original code: 10000000 00000000 00000000 00001000

-8 Anti-code: 11111111 11111111 11111111 11110111

5). Complement

The complement of a positive number is its original code.

The complement of a negative number is based on its inverse code +1

10.

10 The original code: 00000000 00000000 00000000 00001010

10 Anti-code: 00000000 00000000 00000000 00001010

10 Complement: 00000000 00000000 00000000 00001010

-8

-8 of the original code: 10000000 00000000 00000000 00001000

-8 Anti-code: 11111111 11111111 11111111 11110111

-8 Complement: 11111111 11111111 11111111 11111000

6). Any data is stored in memory in the form of a binary complement.

int num =-8;

7). Why should the data be stored in the form of a complement?

Only addition is not subtracted from the computer. To calculate the result at a lower cost, use the complement to store the data.

3 + 2;

3-2; The subtraction operation is 3 + (-2) for the computer; 1

Use the original code calculation.

3 Original Code 00000000 00000000 00000000 00000011

-2 of the original code 10000000 00000000 00000000 00000010

----------------------------------------------------

10000000 00000000 00000000 00000101 The result is that 1 negative numbers are obviously wrong.

Use the inverse code calculation.

3 Anti-code: 00000000 00000000 00000000 00000011

-2 anti-code: 11111111 11111111 11111111 11111101

--------------------------------------------------

00000000 00000000 00000000) 00000000 0

Use the complement calculation

3 Complement: 00000000 00000000 00000000 00000011

-2 Complement: 11111111 11111111 11111111 11111110

-------------------------------------------------

00000000 00000000 00000000) 00000001 1

Note: Using the complement is the highest computational efficiency.

Go The original code, anti-code and complement of C language

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.