What happened to unsigned variables during calculation

Source: Internet
Author: User

Running environment: CentOS release 5.8 (Final)

 

# Include <stdio. h ># include <iostream> using namespace std; int main () {unsigned short u = 10; unsigned int n = 0; // Style left: // u = u-11; // n = u; // Style right: n = u-11; cout <"n =" <n <endl; return 0 ;}

 

Running result:

 

Style left:

$./A. out

N = 65535

 

Style right:

$./A. out

N = 4294967295

 

The following is a compilation of these two methods:

Unsigned variables first subtract (sub), and then assign the value to n unsigned variables first expand, then perform the subtraction operation, and finally assign the value to non-unsigned variables

 

Note: movzx is a variant of the assembly language data transfer command MOV. Unsigned extension and transfer.

Movzx is mainly used to extend a variable to the register eax.

 

1 Binary bit is called 1 bit (bit) 1 bit

Eight bits are called 1 Byte (Byte) 1 Byte = 8 bit

2 bytes means 1 Word (machine Word) 1 Word = 2 Byte

2 words are 1 DWord (machine dual Word) 1 DWord = 2 Word

 

WORD indicates two bytes, namely, sizeof (unsigned short ). Unsigned short: [0, 65536)

DWORD indicates four bytes, sizeof (unsigned int ). Unsigned int: [0, 4294967296)

 

(1) method on the left side: calculate the value first and assign the value in two steps.

// Style left: u = u-11; n = u;

The corresponding compilation is:

Sub word ptr [ebp-0xc], 0 xbmovzx dowrd ptr [ebp-0x8], eax

[Explanation ]:

Because the first step is to perform auto-subtraction on the unsigned variable u, no compiler extension is required.

The value range from 10 to 11 is-1, that is, 0 xffff (the maximum range of unsigned short can be 4 f ).

Step 2 involves the assignment operation,

Before the compiler assigns a value, it first extends the expression = on the right of the assignment operator to the compiler:

Extend 0 xffff to 0x0000ffff, and the variable that accepts the value assignment is a DWORD,

According to the DWORD lengthInterception,DWORD length can accept 0x0000ffff,

Convert to a 10-digit system, that is, 65535

 

[Conclusion ]:

After the compiler first calculates the value, it expands by WORD, so the maximum value of the WORD range is obtained (65535)

 

(2) method on the right: Computation and assignment are combined in one step.

// Style right: n = u-11;

The corresponding compilation is:

Movzx eax, word ptr [ebp-0xc] sub eax, 0xb

Because the range on the right of the value assignment operator and the variables passed in are not of the same type, the compiler first expands the value on the right of the value assignment operator =,

10-11 =-1, compiler extension to 0x0000ffffffff (can only be extended to DWORD ?)

After receiving the DWORD on the right side, convert it to decimal format 4294967295.

 

[Conclusion ]:

The compiler expands according to DWORD after calculation, so the maximum value of DWORD range is obtained (4294967295)

What happened to unsigned variables during calculation

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.