Find out the larger number of two numbers without any comparison, and find out the larger number in two numbers without any comparison.

Source: Internet
Author: User

Without any comparison, find out the larger number of two numbers public class twonumwithoutjudge{//Judge a number of symbols (return 0 negative, 1 positive) public static int getsign (int x)   
	   {return ((x>>31) ^1) &1;
	   	   }//Method 1 (limited premise A is not equal to B, may overflow) public static int GetMax01 (int a,int b) {int c=a-b;

	   	   int d=b-a;
	   	   int c=getsign (C);
           
	   	   int d=getsign (D);
           
	   return c*a+d*b;
	   }//XOR or operation public static int Getflip (int n) {return n^1;
	   ///Get a number of symbols (1 is positive, 0 is negative) public static int sign (int n) {return ((n>>31) &1) ^1;
           }//Method 2 public static int GetMax02 (int a,int b) {int c=a-b;
           int Sa=sign (a);
           int sb=sign (b);
           int sc=sign (c);
           int DIFSAB=SA^SB;
           int Samesab=getflip (DIFSAB);
           int RETRUNA=DIFSAB*SA+SAMESAB*SC;
           int Retrunb=getflip (Retruna);
	   return A*RETRUNA+B*RETRUNB; } PubLic static void Main (String[]args) {//system.out.println ("Hello");
       	       int a = 100;
       	       int b=120;
       	       System.out.println (GetMax01 (a,b));

       	       
       System.out.println (GetMax02 (a,b)); }
}


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.