CPP -- positive code, reverse code, and complement ~ With the discussion of integer overflow, cpp -- anti-code

Source: Internet
Author: User

CPP -- positive code, reverse code, and complement ~ With the discussion of integer overflow, cpp -- anti-code

 

The long dispute (http://www.cnblogs.com/dotnetcrazy/p/8059210.html) was mentioned before, so we don't need long to give an example here. Use int.

Can take a look at this article (http://www.cnblogs.com/dotnetcrazy/p/6743530.html), better understanding of this article (this article does not continue to explore the size of the end of the question of alignment, only research the title content)

 

1. I will not describe the details of the post image.:

Speaking of QWORD, which has been misled on the Internet before, many of them refer to unsigned word on the Internet. According to the Convention, if it is unsigned word, the bytes occupied should be the same as word.

Verification: there are no symbols, and the Occupied bytes remain unchanged.

Later I found that the latest version of Win10 has a calculator (you can install it through the app marketplace) ==="

Pass7Let's talk about these "Words" (I will also use this case later when I talk about Code complement computing)

1 Byte = 8bit (a binary is a bit)

7 = "0000 0111

1 WORD = 2 Byte,7 ="0000000000000111

1 DWORD = 4 Byte,7 ="0000 00000000 00000000 00000000 0111

The point is, if QWORD is really a WORD unsigned WORD, it should only occupy 2 bytes like WORD.

However, the fact =, the PaPa sound played by the face, 1 QWORD = 8 Byte, 7 =, the following words are omitted: One Thousand Words

2. Original code, reverse code, and complement code

In a computer, there are three types of notation for the number of symbols: original code, reverse code, and complement code.

Original code: Indicates the Binary Fixed Point Of a number in a computer.The highest digit is the sign bit (positive digit: 0, negative digit: 1)Other BITs indicate the value size.

Reverse code: The anticode of a positive number is the same as the original code, and the anticode of a negative number is:The symbol is not moved, and the other values are reversed.

Complement:The positive complement is the same as the original one, and the negative complement is:The symbol bit does not move, the other is reversed, and the last plus 1 (Equivalent to: complement + 1)

The advantage of Code complement: enables the symbol bit and the valid value part to participate in the operation to simplify the operation rules; enables the symbol bit and the valid value part to participate in the operation together, thus simplifying the operation rules

More intuitive:

Come and come, practice verification:

Analysis: According to the inverse process of finding a negative complement code, the value part should be a percentile minus 1, and then take the inverse.
However, for the binary number, the result obtained by first subtracting 1 and then obtaining the inverse is the same as that obtained by first obtaining the inverse and then adding 1. Therefore, you can still use the method of inverse adding 1.

These are relatively simple.Benefits of code complementing:

 

Experience Analysis

7-6 = 1;-7 + 6 =-1

7-6 = 1

7 complement: 0000 0111

-6 makeup: 1111 1010

  0000 0111  1111 1010  ---------1 0000 0001

Discard carry (8 bits in total, and no overflow ),0000 0001 => 1(The positive complement is his own)

-7 + 6 =-1

-7 complement: 1111 1001

6 complement: 0000 0110

1111 10010000 0110---------1111 1111

Wood has a forward position, complement code: 1111 1111, positive code (symbol bit does not move, other reverse, last + 1 ):1000 0001 =>-1

Extension(If you are interested, you can study the overflow of complement codes.): https://baike.baidu.com/item/ ke.baidu. 5

3. Integer Overflow

Intmax = 0x7FFFFFFF;(2147483647)

You can see this picture if you are not clear about it,The first is the symbol bit, followed by the numerical value, so the first is 7 and the other is F.

Use Int. Max in Net for verification:2147483647

I'm not happy with what I need. After all, I'm not a student. I just ran a program to check it out.

int main() { int i = 0x7fffffff; printf("i=%X==>%d\n", i, i); i += 1; printf("i+1=%d==>%X\n", i, i); return 0;}

CentOS_X64: I + 1 =-2147483648 => 0x80000000

Win10_X64: I + 1 =-2147483648 => 0x80000000

I remember that I was puzzled when I was a student, and I couldn't explain it to the teacher. I studied it this time when I saw it.

Come and come, Net for a benefit: I will study why

If this parameter is left blank, the binary data goes through one:

0111 1111 1111 1111  1111 1111 1111 1111                                       +11000 0000 0000 0000  0000 0000 0000 0000———— ———— ———— ————  ———— ———— ———— ———— 8    0    0    0     0    0    0     0

Intmax + 1 =0x80000000. Programmers prefer hexadecimal ~ Convenient

Maybe you haven't transferred it yet, (⊙ o ⊙ )..., Okay, let's use the Complement Method to Calculate it again.

0111 1111 1111 1111  1111 1111 1111 11110000 0000 0000 0000  0000 0000 0000 0001---- ---- ---- ----  ---- ---- ---- ----1000 0000 0000 0000  0000 0000 0000 0000

Convert to positive code:
1000 0000 0000 0000 0000 0000 0000 0000 makeup
1111 1111 1111 1111 1111 1111 1111 1111 positive code

2 ^ 31 =>2147483648. If the symbol bit is 1, the final result is-2147483648.

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

The ticket is closed. If you are interested, you can use the Win10 computing artifact (What about QWORD? You can think for yourself. It's very interesting.)

A little bit: word is 2 byte, intmax is 4 bit, so it is not enough

Select HEX and enter the calculated number.

 

Related Article

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.