Bitwise operation and usage

前端 未结 16 1391
无人及你
无人及你 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条回答
  •  梦毁少年i
    2020-11-22 01:06

    Sets

    Sets can be combined using mathematical operations.

    • The union operator | combines two sets to form a new one containing items in either.
    • The intersection operator & gets items only in both.
    • The difference operator - gets items in the first set but not in the second.
    • The symmetric difference operator ^ gets items in either set, but not both.

    Try It Yourself:

    first = {1, 2, 3, 4, 5, 6}
    second = {4, 5, 6, 7, 8, 9}
    
    print(first | second)
    
    print(first & second)
    
    print(first - second)
    
    print(second - first)
    
    print(first ^ second)
    

    Result:

    {1, 2, 3, 4, 5, 6, 7, 8, 9}
    
    {4, 5, 6}
    
    {1, 2, 3}
    
    {8, 9, 7}
    
    {1, 2, 3, 7, 8, 9}
    

提交回复
热议问题