如何得到一个整数的绝对值

来源:互联网 发布:linux迁移mysql数据库 编辑:程序博客网 时间:2024/06/05 10:51
package cn.edu.henu.test.mytest;    /**      * Java得到一个整数的绝对值,不使用任何判断和比较语句,包括API. <br>      * 1、不得使用任何API,如Math.abs()等。<br>                * 2、不得使用判断语句,如if、for、while、switch、?:等。<br>      * 3、不得使用比较语句,如:==、 <=、>=、!=、 <、>等。 <br>      */      public class LessionSystemArraycopy {        public static void main(String[] args) {          for (int i = -1000; i <= 1000; i++) {            System.out.println(abs(i));            }        }        public static int abs(int num) {          return num * (1 - ((num >>> 31)<<1));        }      }  

0 0