Bitwise operation and usage

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

    I think that the second part of the question:

    Also, what are bitwise operators actually used for? I'd appreciate some examples.

    Has been only partially addressed. These are my two cents on that matter.

    Bitwise operations in programming languages play a fundamental role when dealing with a lot of applications. Almost all low-level computing must be done using this kind of operations.

    In all applications that need to send data between two nodes, such as:

    • computer networks;

    • telecommunication applications (cellular phones, satellite communications, etc).

    In the lower level layer of communication, the data is usually sent in what is called frames. Frames are just strings of bytes that are sent through a physical channel. This frames usually contain the actual data plus some other fields (coded in bytes) that are part of what is called the header. The header usually contains bytes that encode some information related to the status of the communication (e.g, with flags (bits)), frame counters, correction and error detection codes, etc. To get the transmitted data in a frame, and to build the frames to send data, you will need for sure bitwise operations.

    In general, when dealing with that kind of applications, an API is available so you don't have to deal with all those details. For example, all modern programming languages provide libraries for socket connections, so you don't actually need to build the TCP/IP communication frames. But think about the good people that programmed those APIs for you, they had to deal with frame construction for sure; using all kinds of bitwise operations to go back and forth from the low-level to the higher-level communication.

    As a concrete example, imagine some one gives you a file that contains raw data that was captured directly by telecommunication hardware. In this case, in order to find the frames, you will need to read the raw bytes in the file and try to find some kind of synchronization words, by scanning the data bit by bit. After identifying the synchronization words, you will need to get the actual frames, and SHIFT them if necessary (and that is just the start of the story) to get the actual data that is being transmitted.

    Another very different low level family of application is when you need to control hardware using some (kind of ancient) ports, such as parallel and serial ports. This ports are controlled by setting some bytes, and each bit of that bytes has a specific meaning, in terms of instructions, for that port (see for instance http://en.wikipedia.org/wiki/Parallel_port). If you want to build software that does something with that hardware you will need bitwise operations to translate the instructions you want to execute to the bytes that the port understand.

    For example, if you have some physical buttons connected to the parallel port to control some other device, this is a line of code that you can find in the soft application:

    read = ((read ^ 0x80) >> 4) & 0x0f; 
    

    Hope this contributes.

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

    Whilst manipulating bits of an integer is useful, often for network protocols, which may be specified down to the bit, one can require manipulation of longer byte sequences (which aren't easily converted into one integer). In this case it is useful to employ the bitstring library which allows for bitwise operations on data - e.g. one can import the string 'ABCDEFGHIJKLMNOPQ' as a string or as hex and bit shift it (or perform other bitwise operations):

    >>> import bitstring
    >>> bitstring.BitArray(bytes='ABCDEFGHIJKLMNOPQ') << 4
    BitArray('0x142434445464748494a4b4c4d4e4f50510')
    >>> bitstring.BitArray(hex='0x4142434445464748494a4b4c4d4e4f5051') << 4
    BitArray('0x142434445464748494a4b4c4d4e4f50510')
    
    0 讨论(0)
  • 2020-11-22 01:15

    This example will show you the operations for all four 2 bit values:

    10 | 12
    
    1010 #decimal 10
    1100 #decimal 12
    
    1110 #result = 14
    

    10 & 12
    
    1010 #decimal 10
    1100 #decimal 12
    
    1000 #result = 8
    

    Here is one example of usage:

    x = raw_input('Enter a number:')
    print 'x is %s.' % ('even', 'odd')[x&1]
    
    0 讨论(0)
  • 2020-11-22 01:19

    Bitwise operators are operators that work on multi-bit values, but conceptually one bit at a time.

    • AND is 1 only if both of its inputs are 1, otherwise it's 0.
    • OR is 1 if one or both of its inputs are 1, otherwise it's 0.
    • XOR is 1 only if exactly one of its inputs are 1, otherwise it's 0.
    • NOT is 1 only if its input is 0, otherwise it's 0.

    These can often be best shown as truth tables. Input possibilities are on the top and left, the resultant bit is one of the four (two in the case of NOT since it only has one input) values shown at the intersection of the inputs.

    AND | 0 1     OR | 0 1     XOR | 0 1    NOT | 0 1
    ----+-----    ---+----     ----+----    ----+----
     0  | 0 0      0 | 0 1       0 | 0 1        | 1 0
     1  | 0 1      1 | 1 1       1 | 1 0
    

    One example is if you only want the lower 4 bits of an integer, you AND it with 15 (binary 1111) so:

        201: 1100 1001
    AND  15: 0000 1111
    ------------------
     IS   9  0000 1001
    

    The zero bits in 15 in that case effectively act as a filter, forcing the bits in the result to be zero as well.

    In addition, >> and << are often included as bitwise operators, and they "shift" a value respectively right and left by a certain number of bits, throwing away bits that roll of the end you're shifting towards, and feeding in zero bits at the other end.

    So, for example:

    1001 0101 >> 2 gives 0010 0101
    1111 1111 << 4 gives 1111 0000
    

    Note that the left shift in Python is unusual in that it's not using a fixed width where bits are discarded - while many languages use a fixed width based on the data type, Python simply expands the width to cater for extra bits. In order to get the discarding behaviour in Python, you can follow a left shift with a bitwise and such as in an 8-bit value shifting left four bits:

    bits8 = (bits8 << 4) & 255
    

    With that in mind, another example of bitwise operators is if you have two 4-bit values that you want to pack into an 8-bit one, you can use all three of your operators (left-shift, and and or):

    packed_val = ((val1 & 15) << 4) | (val2 & 15)
    
    • The & 15 operation will make sure that both values only have the lower 4 bits.
    • The << 4 is a 4-bit shift left to move val1 into the top 4 bits of an 8-bit value.
    • The | simply combines these two together.

    If val1 is 7 and val2 is 4:

                    val1            val2
                    ====            ====
     & 15 (and)   xxxx-0111       xxxx-0100  & 15
     << 4 (left)  0111-0000           |
                      |               |
                      +-------+-------+
                              |
    | (or)                0111-0100
    
    0 讨论(0)
  • 2020-11-22 01:22

    I didn't see it mentioned above but you will also see some people use left and right shift for arithmetic operations. A left shift by x is equivalent to multiplying by 2^x (as long as it doesn't overflow) and a right shift is equivalent to dividing by 2^x.

    Recently I've seen people using x << 1 and x >> 1 for doubling and halving, although I'm not sure if they are just trying to be clever or if there really is a distinct advantage over the normal operators.

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

    To flip bits (i.e. 1's complement/invert) you can do the following:

    Since value ExORed with all 1s results into inversion, for a given bit width you can use ExOR to invert them.

    In Binary
    a=1010 --> this is 0xA or decimal 10
    then 
    c = 1111 ^ a = 0101 --> this is 0xF or decimal 15
    -----------------
    In Python
    a=10
    b=15
    c = a ^ b --> 0101
    print(bin(c)) # gives '0b101'
    
    0 讨论(0)
提交回复
热议问题