Python言語のビット単位演算子

668 ワード

                    。Python          :
      ( bitwise and of x and y )
  &    : 5&3 = 1    : 101  11        1 ,     1
      ( bitwise or of x and y )
  |    : 5|3 = 7    : 101  11   1    1 1 1,     111
     ( bitwise exclusive or of x and y )
  ^    : 5^3 = 6    : 101  11     (   )  1 1 0,     110
     (the bits of x inverted )
  ~    : ~5 = -6    :      +1    -1, ~x = -(x+1),-(101 + 1) = -110
                。     3+~5       -3,  3~5    
     ( x shifted left by n bits )
  <<   :  5<<2 = 20   :101     2    10100 ,     2  0 
     ( x shifted right by n bits )
  >>   : 5>>2 = 1    :101     2    1,      2