You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class031;
public class Code03_GetMax {
// n 0或1
// 0 -> 1 1 -> 0
public static int flip(int n) {
return n ^ 1;
}
// n 任意整数
// n非负的返回1
// n负的返回0
public static int sign(int n) {
// (n >> 31) & 1 n非负的 0 n负的 1
return flip((n >> 31) & 1);
}
public static int getMax1(int a, int b) {
int c = a - b;
int scA = sign(c); // a - b >= 0 scA = 1; a - b <0 scA = 0
int scB = flip(scA); // a - b >= 0 scB = 0; a - b <0 scB = 1
return a * scA + b * scB;
}
public static int getMax2(int a, int b) {
int c = a - b;
int sa = sign(a); // a的符号非负 1 负 0
int sb = sign(b); // b的符号非负 1 负 0
int sc = sign(c); // a-b的符号非负 1 负 0
int difSab = sa ^ sb; // 如果不一样1如果一样0
int sameSab = flip(difSab);// 如果一样1如果不一样0
int returnA = difSab * sa + sameSab * sc;
int returnB = flip(returnA);
return a * returnA + b * returnB;
}
public static void main(String[] args) {
int a = -16;
int b = -19;
System.out.println(getMax1(a, b));
System.out.println(getMax2(a, b));
a = 2147483647;
b = -2147480000;
System.out.println(getMax1(a, b)); // wrong answer because of overflow
System.out.println(getMax2(a, b));
}
}