[Switch] Java &, &&, |, ||, ^, <<, >>, ~, >>> other operators

https://cloud.tencent.com/developer/article/1338265

Java operator are roughly classified into logical operators (&&, || ,!), arithmetic operators (+, -, *, /, + =), bitwise operators (^, |, &), other operators (C unary)

Students do not understand the binary operation I can see another article about binary operation binary operation

& (Bitwise AND)

Bitwise Operators

& Bitwise rules is to convert the number of sides of binary bits, and then computing the final value, the operation rule i.e. (two true to be true) 1 & 1 = 1, 1 & 0 = 0, 0 & 1 = 0, 0 & 0 = 0

Bit 3 is 00000011, bit 5 is 00000101, then it is 011 & 101, the bitwise AND rule that, equal to 0000 0001 001 101, the final value 1

Bit 7 is 00,000,111, that is equal to 111 101 101, i.e. 00,000,101, it is 5

&& (logical AND)

Logical Operators

&& logical and is also known as short circuit and logic, the first operation && expression on the left, once false, no matter how many subsequent expressions are not evaluated, is true, then evaluate the expression on the right, two is true only for the true.

| (Bitwise OR)

Bitwise Operators

| & Bitwise or bit and the binary conversion calculation are recalculated, except that the operation rules (that is true is true) 1 | 0 = 1, 1 | 1 = 1, 0 | 0 = 0, 0 | 1 = 1

00000110 6 bits, 2 bits 0000 0010, 110 | 010 110, the final value of 0000 0110, so 6 | 2 equals 6

|| (logical OR)

Logical Operators

|| logical OR operation is true is the rule is true, no follow-up is calculated, and then a calculation expression on the right is false.

^ (Exclusive OR operator)

Bitwise Operators

^异或运算符顾名思义,异就是不同,其运算规则为1^0 = 1 , 1^1 = 0 , 0^1 = 1 , 0^0 = 0

5的二进制位是0000 0101 , 9的二进制位是0000 1001,也就是0101 ^ 1001,结果为1100 , 00001100的十进制位是12

<<(左移运算符)

位运算符

5<<2的意思为5的二进制位往左挪两位,右边补0,5的二进制位是0000 0101 , 就是把有效值101往左挪两位就是0001 0100 ,正数左边第一位补0,负数补1,等于乘于2的n次方,十进制位是20

>>(右移运算符)

位运算符

凡位运算符都是把值先转换成二进制再进行后续的处理,5的二进制位是0000 0101,右移两位就是把101左移后为0000 0001,正数左边第一位补0,负数补1,等于除于2的n次方,结果为1

~(取反运算符)

位运算符

取反就是1为0,0为1,5的二进制位是0000 0101,取反后为1111 1010,值为-6

>>>(无符号右移运算符)

正数无符号右移

无符号右移运算符和右移运算符的主要区别在于负数的计算,因为无符号右移是高位补0,移多少位补多少个0。

15的二进制位是0000 1111 , 右移2位0000 0011,结果为3

负数无符号右移

-6的二进制是6的二进制取反再加1,6的二进制也就是0000 0000 0000 0000 0000 0000 0000 0110,取反后加1为1111 1111 1111 1111 1111 1111 1111 1010,右移三位0001 1111 1111 1111 1111 1111 1111 1111

至于这个负数的无符号右移为什么是4个字节位的移动,我也不太清楚,还望高手赐教,有所纰漏,欢迎留言,谢谢。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于 2018-09-13

Guess you like

Origin www.cnblogs.com/shoubianxingchen/p/11589414.html