A pen test for C + +

Source: Internet
Author: User

void Swap_int (int *a,int *b) {  *a=*a+*b;  *b=*a-*b;  *a=*a-*b;}

The following statements are correct:
    • The result is not correct, because it will overflow, the use of bits and the way there is no problem
    • The result is correct, even if it overflows
    • The result is correct and does not overflow

    • None of the other options are correct
The bit representation of the shaping variable *a, *b is *a = n toN -··· N0*b = m toM -··· M0Overflow only occurs when *a > 0 && *b > 0 or *a < 0 && *b < 0 o'clock. Similar to each other, only proved to be greater than 0 o'clock in the case. The additional one must be extended to accommodate the correct result, ' | ' The left side is the extension bit. *a = 0|0n -··· N0= N -* * -+ N in* * in+ ··· + N0* *0= N*b = 0|0m -··· M0 =M -* * -+ M in* * in+ ··· + M0* *0= m if and overflow, the 33-bit representation must be *a + *b = 0|1b -··· B0=-2 toE -* * -+ b in* * in+ ··· + b0* *0=2 to+ B①The computer will get the 33-bit result truncate back to the original 32 bits, that is, discard the 33rd bit (0) into: *a + *B = 1b -··· B0=-2 toE -* * -+ b in* * in+ ··· + b0* *0= -2 to+ B②The correct true value is ①, the overflow result is ②, visibleoverflow result = True Value -2 +
Then *b = *a-*b =②-*b =①-2 +-*b = *a + *b-2 +-*b = 2 ++ *a last step, see-2 ++ *a = = *a established no? 0 < *a < 2 to, then-2 +<-2 ++ *a <-2 to, and still need to expand the 1-bit side to represent: *a = 0|0n -··· N0= N -* * -+ N in* * in+ ··· , I0* *0= N-2 += 1|0000 00 and the bit is represented as-2 ++ *a = 1|0n -··· N0= N -* * -+ N in* * in+ ··· + N0* *0Similarly, the computer truncate 33-bit results back to 32 bits (discarding the 33rd bit) to get: 2 ++ *a = 0n -··· N0= N -* * -+ N in* * in+ ··· + N0* *0= *a Visible-2 ++ *a = = *a is established. So despite the overflow, it can still be exchanged correctly.
According to our inertial thinking, add and subtract will cause overflow, but first add and subtract after the original overflow and overflow back, so the result is the correct operation. in the future PEN/interview process also pay attention to problems such as Operation overflow. There is a break in the switch, or a short circuit of the operation, the code after the fork is to berepeated implementation of the problem, these problems usually programming will pay attention to, but the topic is still very confusing.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A pen test for C + +

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.