Java 习题 (18)

题目:
以一个所有位都为1的二进制数字开始,先左移它,然后用无符号右移操作符对其进行右移,直至所有的二进制位都被移出为止,每移一位都要使用Integer.toBinaryString() 显示结果。

解答:
这道题跟上一篇博客不一样,所有得小心读题。

import java.util.*;

public class chapterThree {
    public static void main(String[] args){
    	// 因为int 型值只有32位,所有符合要求的就这个
    	// 博主还没有去试其他类型
        int i = -1;
        System.out.println("i << 1: " + Integer.toBinaryString(i<<1));
        System.out.println("用无符号右移操作符");
        for(int a = 0; a<31; a++){
            i >>>= 1;
            System.out.println(Integer.toBinaryString(i));
        }
    }
}

结果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/BSCHN123/article/details/107297702
今日推荐