代码改变世界
[登录 · 注册]
  • java 判断两个数是否异号
  • java 整型int占4个字节32位,两个数异或后移动31位判断结果,如果是1则异号,如果是0则同号

     1 public class ShowEnviromentViarible {
     2 
     3     public static void main(String[] args) {
     4         int num1 = 1;
     5         int num2 = -1;
     6         System.out.println("num1 = " + num1);
     7         System.out.println("num2 = " + num2);
     8         System.out.println((num1 ^ num2) >>> 31);    //无符号移动31位,最左边补0
     9         num1 = 1;
    10         num2 = 1;
    11         System.out.println("num1 = " + num1);
    12         System.out.println("num2 = " + num2);
    13         System.out.println((num1 ^ num2) >>> 31);
    14         num1 = -1;
    15         num2 = -1;
    16         System.out.println("num1 = " + num1);
    17         System.out.println("num2 = " + num2);
    18         System.out.println((num1 ^ num2) >>> 31);
    19     }
    20 
    21 }

    output:

    num1 = 1
    num2 = -1
    1
    num1 = 1
    num2 = 1
    0
    num1 = -1
    num2 = -1
    0
  • 【推广】 阿里云小站-上云优惠聚集地(新老客户同享)更有每天限时秒杀!
    【推广】 云服务器低至0.95折 1核2G ECS云服务器8.1元/月
    【推广】 阿里云老用户升级四重礼遇享6.5折限时折扣!
  • 原文:https://www.cnblogs.com/luckygxf/p/4228346.html
Copyright 2008-2020 晋ICP备12007731号-1