php binary bit operation and

Binary

    Binary 1 and 0 is used to refer to a computer language, 0 may represent any number, which is closed on a binary

The decimal (i.e. digits) is converted into a binary calculation step

           There are three key concepts in binary

         

              

             

         ▶ bit computing

              

         Bitwise operation rules:

              Bitwise AND &: Two all 1, the result is 1

              Bitwise or |: there is a two to one, the result is 1

              Bitwise XOR ^: two in one of 0, a is 1, the result is a

              Bitwise ~: take 0 0 Take 1,1

       According to the rules, calculating a number of steps:

          Calculation example: 2 =?

             1. Locate 2's complement, [because of the way the computer is operational complement to carry out operations]

               2 of the original code: 00000000 00000000 0,000,000,000,000,010

              // string from left to right above the first number is the number 0, which is a positive number, and a positive there is a principle: positive original code symbol = = trans complement, the complement is above 2 this series of numbers

             2. And then negated

              2 ~ negated: 1,111,111,111,111,111 1,111,111,111,111,101

             // get a string above complement, in order to get the results we want will complement -> anti-code -> original code

             3. Complement -> inverted [(complement -1) is inverted]

              Anti-code: 1,111,111,111,111,111 1,111,111,111,111,100

             4. inverted -> original code [symbol bit change, i.e. from left to right above the first string constant number]

              Original code: 1,000,000,000,000,000 0,000,000,000,000,011 = 0 + 1 * 1 * 2 ^ 2 ^ 2 = 1 + 1 = -3 // 1 for a negative front of the red, the result is -3

               //^表示次方,2^0表示2的0次方,从右往左开始计算:即1(上面这串数字从右往左数的第一个1)*2^0+1(上面这串数字从右往左数的第二个1)*2^1

          例如:2&3=?

              1.找到2和3的补码

                 2的补码: 00000000 00000000 00000000 00000010

                 3的补码: 00000000 00000000 00000000 00000011

                            //按位与&的运算:两位都为1,结果为1

             2&3= 00000000 00000000 00000000 00000010  // 前面的字串为补码,由于其第一个数字为0,所以其为正数,正数的补码=原码,所以结果为2

                

          终上:计算一个数的步骤就是原码-->反码-->补码-->根据位运算符计算得到补码-->反码-->原码-->再得到我们想要的值

       ▶ 位移运算

         在php中位移运算符有两种:>>(右移)和<<(左移)

         运算的规则:

            右移:低位溢出,符号位不变,并用符号位补溢出的高位   [通俗点就是将最右边的数溢出,用最左边的数(符号数)补溢出的个数,放在最左边]

            左移:符号位不变,低位补0    [通俗点就是将最左边的数溢出,用0来补溢出的个数,放在最右边]

         根据规则,下面写几个案例:

                 A. $a=1>>2;//将1向右移动两位

                  1.找出1的补码

                     00000000 00000000 00000000 00000001

                  1>>2

                    00000000 00000000 00000000 00000000

                  $a=1>>2=0

                 

                   B. $a=1<<2;//将1向左移动两位

                  1.找出1的补码

                     00000000 00000000 00000000 00000001

                  1<<2

                    00000000 00000000 00000000 00000100

                  $a=1<<2=4

Guess you like

Origin www.cnblogs.com/wmm123/p/11227583.html