码迷,mamicode.com
首页 > 编程语言 > 详细

java 判断两个数是否异号

时间:2015-01-16 13:02:45      阅读:735      评论:0      收藏:0      [点我收藏+]

标签:

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

 

java 判断两个数是否异号

标签:

原文地址:http://www.cnblogs.com/luckygxf/p/4228346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!