通过移位与或非运算获取整形最大值,最小值,以及获取输入的int类型整数的二进制表示

以上是最终效果

实现类:

package com.corejava.chap02;

public class IntBin {
    private int value;

    public IntBin(int value) {
        this.value = value;
    }

    public void display() {
        for (int i=31; i>=0; i--) {
            System.out.print((value >> i)&1);
        }
        System.out.println();
    }

    public static int Max() {
        int max = 1;
        for (int i=1 ;i<31 ;i++){
            max = (max<<1) | 1;
        }
        return max;
    }

    public static int Min() {
        int min = 1;
        return min << 31;
    } 
}            

测试类:

package com.corejava.chap02;

import com.corejava.chap02.IntBin;

public class Test {
    public static void main(String[] args){
        IntBin ib = new IntBin(10);
        ib.display();
        System.out.println(IntBin.Max());
        System.out.println(IntBin.Min());
    }
}

 

posted @ 2016-07-22 14:30  时光舟  阅读(311)  评论(0编辑  收藏  举报