Java的位运算(bitwise operators)

Java的位运算(bitwise operators)直接对整数类型的位进行操作,这些整数类型包括long、int、short、char和 byte,位运算符具体如下表:image.png

左移位(<<) 

程序:


public class LeftMoving{

    public static void main(String[] args){

           System.out.println("5<<3="+(5<<3));

    }

}


输出结果: 

5<<3=40 

计算过程: 

0000 0000 0000 0000 0000 0000 0000 0101 ? 5 

0000 0000 0000 0000 0000 0000 0010 1000 ? 40


右移位(>>) 

正数 

程序:


public class PlusRightMoving{

    public static void main(String[] args){

           System.out.println("5>>1="+(5>>1));

    }

}


输出结果: 

5>>1=2 

计算过程: 

0000 0000 0000 0000 0000 0000 0000 0101 ? 5 

0000 0000 0000 0000 0000 0000 0000 0010 ? 2


负数 

程序:


public class NegativeRightMoving{

    public static void main(String[] args){

           System.out.println("-5>>1="+(-5>>1));

    }

}


输出结果: 

-5>>1=-3 

计算过程: 

1111 1111 1111 1111 1111 1111 1111 1011 ? -5 

1111 1111 1111 1111 1111 1111 1111 1101 ? -3


无符号右移位(>>>) 

程序:


public class UnsignedRightMoving{

    public static void main(String[] args){

        System.out.println("-5>>>1="+(-5>>>1));

    }

}


输出结果: 

-5>>>1=2147483645 

计算过程: 

1111 1111 1111 1111 1111 1111 1111 1011 ? -5 

0111 1111 1111 1111 1111 1111 1111 1101 ? 2147483645


补充个Java中的基本数据类型的相关知识。

image.png

这里包括了float和double两个浮点型,在本文中对其不予考虑,因为位运算是针对整型的。进行位操作时,除long型外,其他类型会自动转成int型,转换之后,可接受右操作数长度为32。进行位运算时,总是先将短整型和字节型值转换成整型值再进行移位操作的。 

程序:


public class ByteLeftMoving{  

public static void main(String[] args){  

        byte b = 127;  

        System.out.println("b<<3="+(b<<3));  

        System.out.println("(byte)(b<<3)="+(byte)(b<<3));  

    }  

}  


输出结果: 

b<<3=1016 

(byte)(b<<3)=-8


程序:


public class CharLeftMoving{  


public static void main(String[] args){  

        char c = 'l';  

        System.out.println("c<<3="+(c<<3));  

        System.out.println("(char)(c<<3)="+(char)(c<<3));   

    }  

}  


输出结果: 

c<<3=864 

(char)(c<<3)=?


以上两个例子全部编译通过,由此可以看出,当byte和char进行移位运算时不会发生错误,并且均按照整型进行计算,当计算结果超出byte或是char所能表示的范围时则进行相应的转换(分别输出了结果-8和?)。


位运算中的操作数 

在进行移位运算时要注意整型和长整型在内存中的位数(整型是32位,长整型是64位),如果移位操作数超出了该位数则取模计算,例如:int型数据是32位的,如果左移35位是什么结果? 

程序:


public class LeftMoving{  

    public static void main(String[] args){  

           System.out.println("5<<35="+(5<<35));  

    }  

}  


输出结果: 

5<<35=40 

该结果与5<<3完全相同。 

无论正数、负数,它们的右移、左移、无符号右移 32位都是其本身,比如 -5<<32=-5、-5>>32=-5、-5>>>32=-5。 

一个有趣的现象是,把 1 左移 31 位再右移 31位,其结果为 -1。 

计算过程如下: 

0000 0000 0000 0000 0000 0000 0000 0001 

1000 0000 0000 0000 0000 0000 0000 0000 

1111 1111 1111 1111 1111 1111 1111 1111 

位运算要求操作数为整数,操作数不能是字符串也不能是小数。 

如下列程序:


public class BitMath{  


    public static void main(String[] args){  

        String s = "Hello";  

        long l = 99;  

        double d = 1.11;  

        int i = 1;  

        int j = 0;  

        System.out.println("j<<s="+j<<s);    //编译错误语句    

        System.out.println("j<<d="+j<<d);    //编译错误语句  

        System.out.println("i<<j="+i<<j);    //编译可以通过  

        System.out.println("i<<l="+i<<l);    //编译可以通过   

    }   

}  


由于位运算是二进制运算,不要与一些八进制数搞混,java中二进制数没有具体的表示方法。


public class BitMath{

    public static void main(String[] args){

        System.out.println("010|4="+(010|4));

    }

}


输出结果: 

010|4=12 

计算过程: 

0000 0000 0000 0000 0000 0000 0000 1000 ?8 

0000 0000 0000 0000 0000 0000 0000 0100 ?4 

进行“或”计算结果为: 

0000 0000 0000 0000 0000 0000 0000 1100 ?12 

当位运算中遇见负数,必须把它转成补码(不知道什么是补码的补习功课去)再进行计算,而不是使用原码。 

程序:


public class BitMath{

    public static void main(String[] args){

        try {

            int x = -7;

            System.out.println("x>>1="+(x>>1));

        } catch(Exception e) {

            System.out.println("Exception");

        }

    }

}


输出结果: 

x>>1=-4 

计算过程: 

1111 1111 1111 1111 1111 1111 1111 1001 ?-7 

1111 1111 1111 1111 1111 1111 1111 1100 ?-4


public class BitMath{  


    public static void main(String[] args){  

        int i = 1;  

        int j = -1;  

        System.out.println("1>>>31="+(i>>>31));   

        System.out.println("-1>>31="+(j>>31));    

    }   

}  


输出结果: 

1>>>31=0 

-1>>31=-1 

程序:


public class BitMath{  


    public static void main(String[] args){    

        int a = 1;  

        a <<= 31;  

        a >>= 31;   

        a >>= 1;          

        System.out.println("a="+a);  

        int b = 1;   

        b <<= 31;    

        b >>= 31;    

        System.out.println("b="+b);   

        int c = 1;   

        c >>= 31;    

        c <<= 31;  

        System.out.println("c="+c);    

    }  

}  


输出结果: 

a=-1 

b=-1 

c=0 

计算过程: 

0000 0000 0000 0000 0000 0000 0000 0001 ?a=1 

1000 0000 0000 0000 0000 0000 0000 0000 ?a=a<<31后,这里被当作是负数 

1111 1111 1111 1111 1111 1111 1111 1111 ?a=a>>31后,结果为-1 

1111 1111 1111 1111 1111 1111 1111 1111 ?a=a>>1后,结果仍为-1 

0000 0000 0000 0000 0000 0000 0000 0001 ?c=1 

0000 0000 0000 0000 0000 0000 0000 0000 ?c=c>>31后为0 

0000 0000 0000 0000 0000 0000 0000 0000 ?0左移31位仍为0


猜你喜欢

转载自blog.51cto.com/14028890/2425385
今日推荐