Understanding and Application of binary bitwise operators and

About Binary


Any form of binary data are stored on your computer, you need to understand the following concepts:

  1. Original code: integer binary number according to the size of converted into the absolute value, referred to as the original code is converted into the negative original code according to the absolute value of a binary number, and 1 as the highest bit up: 7 is the original code: 00000000 00000000 0,000,000,000,000,111, -7 original code: 1,000,000,000,000,000 0,000,000,000,000,111
  2. Anti-Code: positive anti-code the same as the original code, negative except the inverted sign bit is inverted you on the basis of the original code such as: 1,000,000,000,000,000 0,000,000,000,000,111 inverse code 1,111,111,111,111,111 1,111,111,111,111,000.
  3. Complement: complement positive number and the same original code, negative complement to the inverted +1; for example: 1,000,000,000,000,000 0,000,000,000,000,111 complement 11111001. 11111111 1,111,111,111,111,111 negative is in the form of complement in the computer .

Can be found positive of the original code, anti-code and complement all the same, these concepts clearly negative provision. So what is the significance of the presence of complement is it?
First of all you know, in the computer, the maximum number of overflow +1 0, based on this knowledge, assuming a positive number n, n is assumed that one byte,
Already know : 0 = ( 11111111 + 1 ) n n = 11111111 , n + ( n ) = 0 n Known: 0 = (11111111 + 1) \\ n | \ sim n = 11111111, \\ n + (- n) = 0 \\ request -n
n = 0 n = ( 11111111 + 1 ) n = ( n n + 1 ) n = n + 1 -n=0-n=(11111111+1)-n=(n|\sim n+1)-n=\sim n+1
therefore, negative numbers with the corresponding positive number indicates negated +1, which is the complement.
'S complement, sign-bit sum may be unitary range; Meanwhile, addition and subtraction may be unitary.

Bit computing


Bitwise operators actually binary arithmetic

Suppose one byte number

  1. And (&)
    , such as: 6 & 7
    first two terms of decimal numbers into binary:
    5: 0101 0000
    9: 00001001
    is then ANDed with the corresponding bit, the result can be obtained as follows: 0000 0001
    and finally converted to decimal: 1
    i.e., 1 = 5 & 9
  2. Or (|)
    and a similar operation, the first two decimal numbers to binary conversion, and then ORed.
    As: 5 | 9 = 13
  3. XOR (^)
    XOR: the same is false, not simultaneously true.
    As: 5 ^ 9
    converted into binary bits corresponding to the exclusive OR obtained: 00001100
    it 5 = 12 ^ 9
  4. The negated (-)
    binary number all the bits (including the sign bit) becomes 0 becomes 0 1,1.
  5. Left (<<)
    bits all bits are shifted to the left a predetermined, low complement 0.
    As: 9 << 2
    binary 9: 00001001
    binary number is obtained: 00100100
    a value of 36
    is easy to know x < < n = x 2 n x<<n=x*2^n
  6. Right (>>)
    and left Similarly, all the bit shift to the right a predetermined number of bits, a high negative complement, the complement positive number 0.
    As: 9 >> 2
    binary number obtained: 00000010, value of 2
    - 9 >> 2
    -9 binary (complement): 11110111
    binary number obtained: 11111101
    original code: 10000011, is -3
    easy to know: x > > n = x / 2 n x>>n=x/2^n
  7. Unsigned right shift (>>>)
    difference is that the right time to fill the seats, whether positive or negative, are 0s.

Bitwise Operators Application


Using bitwise operators often make the program more simple and cool

  1. Parity judgment
    judgment last bit binary number is 0 or 1.
    a & 1 == 0A is a even number
  2. Take
    a pair 2 n 2^n modulo be a a & ( 2 n 1 ) (2^n-1) The result is the remainder.
    The principle is returned 2 n 2^n number of subsequent bits
    so much higher than taking efficiency.
  3. Negation
    ~ a + 1
  4. Absolute value
    a >> 31 == 0 a:? (~ A + 1)
    principle is the highest bit binary negative determination.

Reference:
Bitwise operators with common usage scenarios

Released seven original articles · won praise 2 · Views 214

Guess you like

Origin blog.csdn.net/qq_44000076/article/details/103957284
Recommended