Bitwise operation and usage

前端 未结 16 1370
无人及你
无人及你 2020-11-22 00:57

Consider this code:

x = 1        # 0001
x << 2       # Shift left 2 bits: 0100
# Result: 4

x | 2        # Bitwise OR: 0011
# Result: 3

x & 1              


        
16条回答
  •  渐次进展
    2020-11-22 01:07

    There may be a better way to find where an array element is between two values, but as this example shows, the & works here, whereas and does not.

    import numpy as np
    a=np.array([1.2, 2.3, 3.4])
    np.where((a>2) and (a<3))      
    #Result: Value Error
    np.where((a>2) & (a<3))
    #Result: (array([1]),)
    

提交回复
热议问题