当前位置:网站首页>Bit operation rules

Bit operation rules

2022-07-06 05:52:00 Python's path to immortality

An operator

An operator effect priority
~ According to the not 1
<< and >> Move left Move right 2
& Bitwise AND 3
^ Bitwise XOR 4
or Press bit or 5

Please refer to the link for details of hexadecimal conversion :https://blog.csdn.net/qq_48434912/article/details/122898217?spm=1001.2014.3001.5502
1、 According to the not (~)

Monocular operator , In binary ,1 change 0,0 change 1.

2、 Bitwise AND (&)、 Press bit or (|)

Binocular operator , In binary .

 1&1=1			1|1=1
 0&0=0			0|0=0
 1&0=0			1|0=1
 0&1=0			0|1=1

3、 Exclusive or (^)

Binocular operator , In binary .
Operational rules : The same XOR of two numbers is 0, Different XOR is 1.

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

characteristic :

  1. And 1 XOR has Overturnability (0 change 1,1 change 0)
  2. And 0 XOR has Keep sex ( XOR is itself )
  3. XOR with a binary number for two consecutive times and then restore the original value unchanged , have Restorative
  4. XOR operation has Exchangeability (a ^ b = b ^ a)

4、 Shift Operators ( Shift left <<)
Binocular operator , In binary .
The rules : High position regardless 0 or 1 All discarded , The low vacancy is in 0 fill .
features : There is nothing discarded in the high position 1 when , Move left i position , become 2^i times .

 20<<2
 20 Binary system :0001 0100
  Move two places to the left :0101 0000
  result :20*2^2=80

5、 Shift Operators ( Shift right >>)
The rules : Low regardless 0 or 1 All discarded , The high empty bit fills the sign bit for the signed number ( Fill in with a positive number 0, Negative number filling 1), For unsigned numbers, fill 0.
features : There is nothing discarded in the low order 1 when , Move right i position , become 2^-i times , That is, it shrinks 2^i times .

20>>2
 20 Binary system :0001 0100
  Move two places to the right :0000 0101
  result :20*2^-2=5

notes : If there is no assignment operation after bit operation , Then the value of the variable remains unchanged .

原网站

版权声明
本文为[Python's path to immortality]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132041410726.html