About Binary
Any form of binary data are stored on your computer, you need to understand the following concepts:
- 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
- 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.
- 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,
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
- 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 - Or (|)
and a similar operation, the first two decimal numbers to binary conversion, and then ORed.
As: 5 | 9 = 13 - 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 - The negated (-)
binary number all the bits (including the sign bit) becomes 0 becomes 0 1,1. - 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 - 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: - 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
- Parity judgment
judgment last bit binary number is 0 or 1.
a & 1 == 0
A is a even number - Take
a pair modulo be & The result is the remainder.
The principle is returned number of subsequent bits
so much higher than taking efficiency. - Negation
~ a + 1 - Absolute value
a >> 31 == 0 a:? (~ A + 1)
principle is the highest bit binary negative determination.