Bit operations (AND, OR, XOR)

Bit operations

Bit operations are unary and binary operations on arrays or binary numbers in programming. On many older microprocessors, bitwise operations are slightly faster than addition and subtraction, and generally bitwise operations are much faster than multiplication and division. In modern architectures, bitwise operations often operate at the same speed as addition (still faster than multiplication). The data in the computer are stored in binary form in the memory. Bit arithmetic is to directly operate on the binary bits of the integer in the memory, so its execution efficiency is very high.

and(&)

The result is 1 only if both bits involved in the operation are 1, otherwise it is 0

  • 1&1=1
  • 1&0=0
  • 0&0=0

Example 1:

 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1001  (9 在内存中的存储)
& 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 0101  (5 在内存中的存储)
-----------------------------------------------------------------------------------
 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 0001  (1 在内存中的存储)

Example 2:
Determine whether the value a is an odd number or an even number?

//思路:只要根据数的最后一位是0还是1来决定即可,为0就是偶数,为1就是奇数
if((a&1)==0){
    Debug.Log("偶数");
}else{
    Debug.Log("奇数");
}

or (|)

If one of the two bits involved in the operation is 1, the result is 1, otherwise it is 0

  • 1|1=1
  • 1|0=1
  • 0|0=0

example:

   0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1001  (9 在内存中的存储)
 | 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 0101  (5 在内存中的存储)
-----------------------------------------------------------------------------------
   0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1101  (13 在内存中的存储)

Rebellion (~)

Negate the bits involved in the operation, 0 becomes 1 and 1 becomes 0

  • ~1=0
  • ~0=1

example:

~ 1111 1111 -- 1111 1111 -- 1111 1111 -- 1111 0111  (-9 在内存中的存储)
-----------------------------------------------------------------------------------
 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1000  (8 在内存中的存储)

XOR (^)

Compare the bits involved in the operation. If they are the same, 0 will be taken; if they are different, 1 will be taken.

  • 1^1=0
  • 1^0=1
  • 0^0=0

example:

   0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1001  (9 在内存中的存储)
 ^ 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 0101  (5 在内存中的存储)
-----------------------------------------------------------------------------------
   0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1100  (12 在内存中的存储)

Example 2:
Swap two numbers without creating a third temporary variable?

int a =10;
int b =35;
a=a^b;
b=a^b;
a=a^b;
Debug.Log($"a={a}");
Debug.Log($"b={b}");

The output result is:
a=35
b=10

explain:

  1. a=a^b
  2. b=a b b b^b is 0, so the result is a
  3. a=a^b= a b a a^a is 0, so the result is b

shift

Shift left (<<)

Shift all binary bits of the data to the left by a certain number of bits, discard the high bits, and fill the low bits with 0s.

example:

3<<2
1. 3的二进制数为:0000 0000 0000 0000 0000 0000 0000 0011
2. 把该数字高位(左侧)的两个零移出,其他的数字都朝左平移2位,最后在低位(右侧)的两个空位补零。
3. 得到的最终结果是0000 0000 0000 0000 0000 0000 0000 1100,则转换为十进制是12。

Mathematical meaning:

  • As long as the number does not overflow, for positive and negative numbers, shifting one bit to the left is equivalent to multiplying by 2 raised to the power of 1, and shifting n bits to the left is equivalent to multiplying by 2 raised to the nth power.

Move right (>>)

Shift all the binary bits of the data to the right by a certain number of bits, discard the low bits, and add 0 or 1 to the high bits. If the highest bit of the data is 0, then add 0; if the highest bit is 1, then add 1.

example:

11 >> 2
1. 11的二进制形式为:0000 0000 0000 0000 0000 0000 0000 1011
2. 把低位的最后两个数字移出,因为高位是0,所以在高位补0
3. 得到的最终结果是0000 0000 0000 0000 0000 0000 0000 0010。转换为十进制是2。

Mathematical meaning:

  • Shifting one bit to the right is equivalent to dividing by 2, and shifting n bits to the right is equivalent to dividing by 2 raised to the nth power.

Guess you like

Origin blog.csdn.net/weixin_42498461/article/details/131069988