Bitwise operation and usage

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

    Think of 0 as false and 1 as true. Then bitwise and(&) and or(|) work just like regular and and or except they do all of the bits in the value at once. Typically you will see them used for flags if you have 30 options that can be set (say as draw styles on a window) you don't want to have to pass in 30 separate boolean values to set or unset each one so you use | to combine options into a single value and then you use & to check if each option is set. This style of flag passing is heavily used by OpenGL. Since each bit is a separate flag you get flag values on powers of two(aka numbers that have only one bit set) 1(2^0) 2(2^1) 4(2^2) 8(2^3) the power of two tells you which bit is set if the flag is on.

    Also note 2 = 10 so x|2 is 110(6) not 111(7) If none of the bits overlap(which is true in this case) | acts like addition.

    0 讨论(0)
  • 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}
    
    0 讨论(0)
  • 2020-11-22 01:06

    i didnt see it mentioned, This example will show you the (-) decimal operation for 2 bit values: A-B (only if A contains B)

    this operation is needed when we hold an verb in our program that represent bits. sometimes we need to add bits (like above) and sometimes we need to remove bits (if the verb contains then)

    111 #decimal 7
    -
    100 #decimal 4
    --------------
    011 #decimal 3
    

    with python: 7 & ~4 = 3 (remove from 7 the bits that represent 4)

    001 #decimal 1
    -
    100 #decimal 4
    --------------
    001 #decimal 1
    

    with python: 1 & ~4 = 1 (remove from 1 the bits that represent 4 - in this case 1 is not 'contains' 4)..

    0 讨论(0)
  • 2020-11-22 01:07

    One typical usage:

    | is used to set a certain bit to 1

    & is used to test or clear a certain bit

    • Set a bit (where n is the bit number, and 0 is the least significant bit):

      unsigned char a |= (1 << n);

    • Clear a bit:

      unsigned char b &= ~(1 << n);

    • Toggle a bit:

      unsigned char c ^= (1 << n);

    • Test a bit:

      unsigned char e = d & (1 << n);

    Take the case of your list for example:

    x | 2 is used to set bit 1 of x to 1

    x & 1 is used to test if bit 0 of x is 1 or 0

    0 讨论(0)
  • 2020-11-22 01:07

    Another common use-case is manipulating/testing file permissions. See the Python stat module: http://docs.python.org/library/stat.html.

    For example, to compare a file's permissions to a desired permission set, you could do something like:

    import os
    import stat
    
    #Get the actual mode of a file
    mode = os.stat('file.txt').st_mode
    
    #File should be a regular file, readable and writable by its owner
    #Each permission value has a single 'on' bit.  Use bitwise or to combine 
    #them.
    desired_mode = stat.S_IFREG|stat.S_IRUSR|stat.S_IWUSR
    
    #check for exact match:
    mode == desired_mode
    #check for at least one bit matching:
    bool(mode & desired_mode)
    #check for at least one bit 'on' in one, and not in the other:
    bool(mode ^ desired_mode)
    #check that all bits from desired_mode are set in mode, but I don't care about 
    # other bits.
    not bool((mode^desired_mode)&desired_mode)
    

    I cast the results as booleans, because I only care about the truth or falsehood, but it would be a worthwhile exercise to print out the bin() values for each one.

    0 讨论(0)
  • 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]),)
    
    0 讨论(0)
提交回复
热议问题