Find out the larger number of two numbers without any comparative judgment.

Source: Internet
Author: User
Tags sca

find out the larger number of two numbers without any comparative judgment.

Topic

Given two 32-bit integers a and B, returns the larger of A and B.

Requirements

Without any comparative judgment.

Troubleshooting

There are two ways to solve this, both methods are the same principle, but the second is the first optimization.

The function of the sign function is to return the symbol of the integer n, positive number and 0 return 1, the function of negative return 0.flip function is to do N xor operation, n is 1 return 0,n is 0 return 1.

method One:

We can not directly compare, then use subtraction to judge, A-B value of the symbol, the symbol is a large, the symbol is negative. SCA represents the C symbol, SCB represents the opposite sign of C, and SCA is 1 returns A,SCA is 0 returns B. But there are limitations, that is, the value of a-a May overflow and the returned result is incorrect.

Method Two:

The symbol of A and b two numbers is compared first, the symbol is different difsab==1,samesab==0; The direct return is the number that the symbol is positive.

If a is 0 or positive, then B is negative (sa==1,sb==0), then A is returned;

If a is negative, then B is 0 or positive (sa==0,sb==1), then B is returned;

If the symbol is the same difsab==0,samesab==1, in which case the value of a-B will never overflow, then look at the C symbol.

If C=a-b, returns a for positive;

If c=a-b, returns b for negative;


The code is as follows:

public class Completetwonum {/** * XOR operation */public int flip (int n) {return n ^ 1;
	}/** * N is positive or 0 returns 1 n is a negative number returns 0 */public int sign (int n) {return Flip ((n >>) & 1);
		} public int GetMax1 (int a, int b) {int c = a-D;
	int ScA = sign (c);//returns the C symbol, positive number and 0 is 1, negative number is 0 int scB = Flip (ScA);//returns the opposite sign of C sign return a * ScA + b * ScB; }/** * If the symbol A, B is the same (+,+), (-,-), DIFSAB=0,RETURNA=SC; if SC is 1, return a, otherwise return b * if the symbols of a, B are different (+,-), (-,+), Dissab=1,returna=sa; if SA
		is 1, returns a, otherwise returns B */public int getMax2 (int a, int b) {int c = a-D;
		int SA = sign (a);//returns a symbol int SB = signs (b);//returns b symbol int sc = sign (c);//return c symbol int DIFSAB = sa ^ sb;
		int Samesab = Flip (Difsab);
		int Returna = Difsab * SA + Samesab * SC;
		int returnb = Flip (Returna);

	Return a * Returna + b * RETURNB;
		} public static void Main (string[] args) {completetwonum twonum = new Completetwonum ();
		Scanner s = new Scanner (system.in);
		int a = S.nextint ();

		int b = S.nextint (); System.out.priNtln ("Method one: a=" + A + ", b=" + B + "Large number is:" + Twonum.getmax1 (A, b));
		
System.out.println ("Method two: A=" + A + ", b=" + B + "Large number is:" + TWONUM.GETMAX2 (A, b)); System.out.println (Integer.max_value);//maximum integer 2147483647//System.out.println (Integer.min_value);//-

	2147483648 is the smallest int that can be represented in the 32 machine. }
}
The results of the operation are as follows:

123 456
Method One:   a=123, b=456  large number is: 456
method Two: a=123   , b=456  large number is: 456

The results of the method one result overflow are as follows:

-2147483648 8
Method One:   a=-2147483648, b=8  large number is: -2147483648
method Two:   a=-2147483648, b=8  large number is: 8










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.