Bitwise operation and usage

前端 未结 16 1610
无人及你
无人及你 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:23

    I hope this clarifies those two:

    x | 2
    
    0001 //x
    0010 //2
    
    0011 //result = 3
    

    x & 1
    
    0001 //x
    0001 //1
    
    0001 //result = 1
    

提交回复
热议问题