Comparison operators are not required to compare the size of two numbers

Source: Internet
Author: User

To commemorate

You are not allowed to use any comparative operations in the function body, such as if, for, while, or call any library function.

 
Int max (int A, int B) {int SA, Sb, F; SA = (A/2) & 0x80000000)> 31; SB = (B/2) & 0x80000000)> 31; F =! (! (A/(B | 1); Return (SA ^ SB) * (*(! SA) + B *(! SB) + (! (SA ^ SB) * (A * (SA ^ f) + B *(! (SA ^ F )));}

Explanation:

(A/2) & 0x80000000)> 31; // obtain the symbol bit of.

(B/2) & 0x80000000)> 31; // obtain the symbol bit of B.

Unsigned and signed will have an impact on the negative number effect. At that time, this was not taken into account, but it was processed as unsigned number as required by the question.

After dividing by 2, the maximum bit is 0, while the number is still signed.

! (! (A/(B | 1); // The size flag of A and B

A/B, when a is large, it is a non-zero number; otherwise, it is 0, but when B = 0, it cannot be calculated.

Therefore, the minimum position of B is 1 to ensure that the computation can proceed. Although setting 1 will increase the number of low positions to 0 by 1, the result will not be affected.

The opposite result is obtained only to ensure that the result is 1 and 0 instead of other numbers.

The rest is the selective output of logical operations.

A symbol is recorded as SA

B symbol bits are recorded as sb

The AB result is compared to F.

There are six cases

Sa <sb a is a positive number, B is a negative number

Sa> Sb A is negative, B is positive

Sa = Sb AB with the same symbol

Sa = 0 is the same as positive number

Sa = 1 is negative

Sa <Sb, SA = Sb and SA = 0 and F = 1, SA = Sb and SA = 1 and F = 0 when a is large

Sa> Sb, SA = Sb and SA = 0 and f = 0, SA = Sb and SA = 1 and F = 1 are larger than B.

Combined output result (SA ^ SB) * (*(! SA) + B *(! SB) + (! (SA ^ SB) * (A * (SA ^ f) + B *(! (SA ^ F )));

Remove the intermediate variable.

 
Int max (int A, int B) {return (A/2) & 0x80000000)> 31) ^ (B/2) & 0x80000000)> 31) * (*(! (A/2) & 0x80000000)> 31) + B *(! (B/2) & 0x80000000)> 31) + (! (A/2) & 0x80000000)> 31) ^ (B/2) & 0x80000000)> 31 ))) * (A/2) & 0x80000000)> 31) ^ (! (! (A/(B | 1) + B *(! (A/2) & 0x80000000)> 31) ^ (! (! (A/(B | 1 )))))));}

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.