Special Uses operator

Foreword

Using these operators, here default environment in php

A ^

Introduction: ^ denotes exclusive OR operation
XOR operation rules: the XOR converted to binary decimal

0^0 = 0
1^0 = 1
0^1 = 1
1^1 = 0
即同为假,异为真(同假异真)

For example: 10 ^ 24
calculation:

10^24运算步骤:
(1)先将1024分别转换为二进制:1010^11000,转化一下就是01010^11000
(2)按规则进行异或运算:
01010
11000
结果为:10010,转换为十进制为18

Tip: 0 ^ unknown, the results are unknown

Second, the ~

Introduction: ~ denotes inversion calculation, i.e. the inverse of the binary, for example: 0000, 1111 after the go anti, but not so often used, generally as follows:
For example: ~9but its value is -10, why?
Inversion operation rules:
1, the original code representation in front of a sign bit value increases (i.e., the highest bit is the sign bit): This bit is a positive number of 0, 1 bit is negative the
2's complement is calculated
(1) n number complement representation: complement positive numbers = the original code
(2) complement negative original code symbol bits = {} + {value unchanged bits after the bit inversion by the +1 = {} or not the original code symbol bits variable} + {0 values remain unchanged from the number to the right of the first 1 and on the right, left} bitwise
specific reference: https://zhidao.baidu.com/question/148760603.html
calculation:

~9计算过程:
(1)9转二进制:0 1001	//左边的0表示9这个数字是正数
(2)计算补码:0 1001	//因为9是正数,所以补码为原码,注意,原码是1001
(3)按位取反运算:1 0110	//符号位也要取反,这是取反的补码
(4)接上面再次取反:1 1001	//此时符号位不需要取反,只需要取一次即可
(5)末位加11 1010	//1001最后一位加1,由于是2,大于1,就变0,向前进1,前面的0变1
(6)还原源码:-10	//1 1010中1表示负号,1010二进制转十进制表示10,所以~9最后的结果为-10
~-9计算过程
(1)9转二进制:1 1001	//左边的1表示-9这个数字是负数
(2)计算补码:1 0111	//因为-9是负数,所以补码为{原码符号位不变}+{数值位从右边数第一个1及其右边的0保持不变,左边按位取反},即1 0111;注意,原码是1001
(3)取反运算:0 1000	//符号位也要取反,这是取反的补码
(4)再次取反:0 0111	//此时符号位不需要取反,只需要取一次即可
(5)末位加10 1000	//0111最后一位加1
(6)还原源码:8	//0 1000中0表示正号,1000二进制转十进制表示8,所以~-9最后的结果为8

Tip: To sum up, if you want to specify 666 or 999, etc., you can directly ~~666或~~999
reference: https://blog.csdn.net/liu940204/article/details/51115704

Third, << or >>

Introduction: left shift operation is operand one bit shifted to the left by the specified number of digits of movement, out of the bits are discarded, the vacancies all 0s on the right. A right shift operation is a bit of the operand in number of bits specified rightward movement, out of the bits are discarded, vacancy left out all 0s or complement sign bit, which is determined by the different machines.
Reference: Baidu Encyclopedia
Example: 10 10 >> and << 3 3
Calculation:

10<<3运算步骤:
(1)10转换为二进制:1010
(2)二进制数全部向左移动三位,被移动的位被丢弃,右边的空位一律补0,在这里相当于最右边的0移到了最左边1的位置:1010000,这里也就是在右边加30
(3)最后将二进制转换为十进制,结果为80

10>>3
(1)10转换为二进制:1010
(2)二进制数全部向右移动3位,被移动的位被丢弃,左边移出的空位一律补0,在这里相当于1走到了最右边0的位置:0001
(3)最后将二进制转十进制,结果为1

Tip: to which side shift, the first number on the binary value of the shift in the opposite direction of the movement as a starting point, not better understood, for example >> to the right, I will take the leftmost binary number the first number as a starting point to move

four,&

Introduction: & bits representing operator, the values converted to binary numbers, and the operation
rules and operation: 0 & 0 = 0; 0 & 1 = 0; 1 & 0 = 0; 1 & 1 = 1, i.e., two with the same 1, the result is a 0 otherwise
For example: 10 & 24
calculation:

10&24运算步骤
(1)转二进制:1010&11000,即01010&11000
(2)与运算:
01010
11000
结果为:01000,转换为十进制为8

Reference: https://www.cnblogs.com/net-safe/p/8488605.html

Five, |

Introduction: | represents OR operation, the value is converted to a binary number, and performing an OR operation
or calculation rule: 0 & 0 = 0; 0 & 1 = 1; 1 & 0 = 1; 1 & 1 = 1, i.e., or two operands as long as there is a value 1, then the result is an
embodiment: 10 | 24
calculation:

10|24
(1)转二进制:1010&11000,即01010&11000
(2)或运算:
01010
11000
结果为:11010,转换为十进制为26
Published 148 original articles · won praise 61 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_41617034/article/details/105251741