One or two binary
A = 1
Bin (a)-->ob1 #python内置方法
OB denotes binary integer format
Second, difficult symbols
1, the bit of the displacement binary
>> Right Shift , imagine cut meat cut to the last one
For example, x >> y #先转成二进制再位移
Calculation formula: x/(2**y)
<< left shift total left shift after 0
such as X<<y
Calculation formula: x* (2**y)
2, & Bitwise AND: whether all are 1, right alignment, front 0, there is a 1 is 0
0110
1000
-------
0000
3, | Bitwise OR as long as one is 1, you get 1.
0110
1000
------
1110
4.^ Bitwise XOR: Two values that participate in the operation, if two corresponding bit bits are the same, the result is 0, otherwise 1
Several common uses of bitwise XOR:
(1) Turn some specific bits upside down
For example, the 2nd and 3rd bits of the logarithm 10100001 are flipped, and the number can be bitwise XOR or operated with 00000110.
10100001^00000110 = 10100111
(2) The interchange of two values is implemented without the use of temporary variables.
For example, a value of two integer a=10100001,b=00000110 can be exchanged by the following statement:
A = A^b;//a=10100111
b = b^a;//b=10100001
A = A^b;//a=00000110
(3) In assembly language is often used to set the variable 0:
XOR A,a
(4) quickly determine whether two values are equal
Example 1: To determine whether the two integers a and b are equal, it can be implemented by the following statements:
Return ((a ^ b) = = 0)
4.~ bitwise REVERSE (FLIP): 1 to 0,0 to 1, the formula is ~x =-(x+1)
PS: Complement 1, in the computer system, the values are all in the complement to indicate (storage).
The main reason: the use of complement, the symbol and other bits can be processed uniformly, and subtraction can also be processed by addition. In addition, two complementary
2, the complement and the original code conversion process is almost the same.
(2) The complement of the negative number: The sign bit is 1, the remainder is the absolute value of the original code bitwise negation; Then the whole number is added to 1.
Other usage1. Judging odd even numbers4&1 0 is even 1 is odd2, calculate the hard disk capacity11866 (bytes) >>1011k>> Ten0M
Python Self-study note (vi) binary and displacement