Java bit operations (bitwise operators)

Java bit operations (bitwise operators) directly to the integer type of bit operation, these types include integer long, int, short, char, and byte, bitwise operators in the following table:image.png

Left Shift (<<) 

program:


public class LeftMoving{

    public static void main(String[] args){

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

    }

}


Output: 

5<<3=40 

calculation process: 

0000 0000 0000 0000 0000 0000 0000 0101 ? 5 

0000 0000 0000 0000 0000 0000 0010 1000 ? 40


Right shift (>>) 

positive number 

program:


public class PlusRightMoving{

    public static void main(String[] args){

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

    }

}


Output: 

5>>1=2 

calculation process: 

0000 0000 0000 0000 0000 0000 0000 0101 ? 5 

0000 0000 0000 0000 0000 0000 0000 0010 ? 2


negative number 

program:


public class NegativeRightMoving{

    public static void main(String[] args){

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

    }

}


Output: 

-5>>1=-3 

calculation process: 

1111 1111 1111 1111 1111 1111 1111 1011 ? -5 

1111 1111 1111 1111 1111 1111 1111 1101 ? -3


Unsigned right shift (>>>) 

program:


public class UnsignedRightMoving{

    public static void main(String[] args){

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

    }

}


Output: 

-5>>>1=2147483645 

calculation process: 

1111 1111 1111 1111 1111 1111 1111 1011 ? -5 

0111 1111 1111 1111 1111 1111 1111 1101 ? 2147483645


Supplement a basic knowledge of Java data types.

image.png

Included here are two float float and double, it will not be considered in this article, because bit integer arithmetic is against. When the bit operation, in addition to long type and other types will be automatically converted to an int, after the conversion, pharmaceutically right operand length is 32. When bit operation, always first convert short integer type and byte integer value to a further shift operation. 

program:


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));  

    }  

}  


Output: 

b<<3=1016 

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


program:


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);    

    }  

}  


Output: 

a=-1 

b=-1 

c=0 

calculation process: 

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

1000 0000 0000 0000 0000 0000 0000 0000? A = a << after 31, there is deemed to be negative 

1,111,111,111,111,111 1,111,111,111,111,111? After a = a >> 31, the result is -1 

1111 1111 1111 1111 1111 1111 1111 1111? A = a >> 1, the results at -1 

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

0000 0000 0000 0000 0000 0000 0000 0000? After c = c >> 31 0 

0000 0000 0000 0000 0000 0000 0000 0000? 0 left 31 still 0


Guess you like

Origin blog.51cto.com/14028890/2425385