week01_day03_左移&右移

首先,左移右移都是针对int型的整数,即32位(4字节)的整数
1.左移:a<
无论带符号的整数还是不带符号的整数,(在不发生溢出的情况下)所有操作如下:
将a写成二进制形式,a的二进制表达式所有位数向左移动b位,然后右侧缺省处补b个0.
a<

**2.带符号位右移:a>>b.**右移运算中操作数为负数时的情况操作如下:(操作数为正整数时的操作其实相当于a>>>b的操作,不用转化成补码形式再求,因为正数的补码就是其原码)
将a转化成二进制形式a1
将a1转化成补码形式a2.
将a2右移b位,高位补符号位(负数补1,正数补0),值为a3.
将a3转化成原码形式a4.
在不发生溢出的情况下且能a/b能除尽的情况下: a>>b的结果为a/b,至于其他情况,还是老老实实按照上述步骤计算吧。
week01_day03_左移&右移_第1张图片
-2>>2时会发生下溢,下溢的结果不是很严重,-2>>10,-10>100d的结果依然为-1。

3.无符号位右移:a>>>b.(一般用于处理int型正整数,也可以处理负整数),操作如下:(其操作其实同左移操作)
将a写成二进制形式,a的二进制表达式所有位数向左移动b位,然后右侧缺省处补b个0.

注:在计算机中,所有的运算都是以补码形式进行的,而上述有些操作直接用原码进行计算只不过是为了方面我们快速的求出值,因为正数的补码就是其原码,其本质上还是用 补码进行运算的。

补充: 看了https://blog.csdn.net/mr_hcw/article/details/84262060的博客之后,感觉比老师讲的更清晰,但以下都是在没有溢出的情况下。

week01_day03_左移&右移_第2张图片
week01_day03_左移&右移_第3张图片
·········································································································································································

按位 &、按位 |
6&3=2,6|3=7,这两个运算将6和3转化成原码形式就能轻易算出,那-6&-3,-6|-3呢?我们应当将其转化成补码形式进行运算。
week01_day03_左移&右移_第4张图片

按位^,按位~
week01_day03_左移&右移_第5张图片

week01_day03_左移&右移_第6张图片

·········································································································································································

Scanner注意事项:
1.用完之后记得关闭:sc.close();
2. 推荐做法:如果即要读取普通数据(int、long、boolean等),也要读取nextLine(读取一行字符串),用两个Scanner对象。
Scanner sc = new Scanner(System.in); //读int、long、boolean等
Scanner strSc = new Scanner(System.in); //读取一行字符串

·········································································································································································
如何导入老师的项目:
1.File-----Close Project
2.Import Project
3.点击next
week01_day03_左移&右移_第7张图片
4.点击n个next后,遇到如下问题,点击Reuse
5.点击next----finish
注:左侧一栏中有个泛红的out文件夹,里面存放的是已经编译好的字节码文件.class

·········································································································································································

作业:

  1. 有三个int变量,a, b, c假设三个变量中有两个变量的值相同,请问如何快速求出,那个和其他两个变量不同的第三个变量的值?
public class Homework01 {
     

    public static void main(String[] args) {
     
        System.out.println(diff(1, 1, 2));
    }

    //low货做法
    private static int diff(int num1, int num2, int num3) {
     
        if (num1 == num2)
            return num3;
        else if (num1 == num3)
            return num2;
        else
            return num3;
    }

    //大佬写法
    private static int diff2(int num1, int num2, int num3) {
     
        return num1 ^ num2 ^ num3;  //a^a=0,0^a=a
    }
}
  1. 任给一个int类型的正整数,如何判断该整数的值,是否是2的整数次幂?
    week01_day03_左移&右移_第8张图片
public class Homework02 {
     

    public static void main(String[] args) {
     
        System.out.println(func(14));
        System.out.println(func2(128));
    }

    //low货做法
    private static boolean func(int num){
     
        while(num>0&&num%2==0)
            num=num/2;
        return (num==1)? true:false;
    }

    //大佬写法
    private static boolean func2(int num){
     
        if((num&(num-1))==0)
            return true;
        return false;
    }
}

附加题:用位运算符求一个整数(int)的绝对值。
week01_day03_左移&右移_第9张图片

public class HomeworkAdditional {
     

    public static void main(String[] args) {
     
        System.out.println(abs(-5));
    }

    private static int abs(int num) {
     
        return (num ^ (num >> 31)) - (num >> 31);
    }
}

你可能感兴趣的:(JavaSE)