Directory:
first, machine number and truth valueSecond,
original code, anti-code and the basic concept of complement
Third,
Why to use the original code, anti-code and complement
Four, the original code, complement, anti-code and further
v. Data overflow testing
six
. Operation Description of bit operation
Simple application of the seven-bit arithmetic
first, machine number and truth valueMachine count (computer number) is a binary representation of numbers in a computer the number of machines has 2 features: one is the symbol digitization, the other is the size of its number by the machine word limit such as: +6 in decimal, the computer word length is 8 bits, converted to binary is 00000110, if 6, That's 10000110.00000110 and 10000110 is the number of machines because the first bit is the sign bit (positive digit is 0, negative number is 1, 0 points +0 and-0), so: the range of ① 8-bit binary numbers is: [1111 1111, 0111 1 The form value of the 111]② machine number is not equal to the true value. For the sake of differentiation, the true value corresponding to the number of machines with the sign bit is called the truth of the machine number. For example: 0000 0001 Truth value = +000 0001 = +1,1000 0001 True Value =–000 0001 =–1
second, original code, anti-code and the basic concept of complementFor a number, the computer is stored with a certain encoding. The original code, the inverse code, the complement is the machine to store a specific number encoding method [+1] = [00000001] original = [00000001] reverse = [00000001] complement [1] = [10000001] original = [11111110] anti = [11111111] Complement
1. Original CodeThe original code is the absolute value of the symbol bit plus the truth, that is, the first digit to represent the symbol, the remaining bits represent the values
2. Anti-codeThe inverse of a positive number is the inverse code of its own negative number is on the basis of its original code, the symbol bit is unchanged, the remaining bits take the inverse
3. ComplementA positive complement is the complement of its own negative number is in the anti-code based on +1 this shows that the original code of positive numbers are self-complement, negative anti-code, complement can not be visualized to see its value, need to convert to the original code and then calculate its value
third, why to use the original code, anti-code and complement for computers, the addition and subtraction multiplier is already the most basic computing, to design as simple as possible, and let the computer distinguish "sign bit" obviously will make the computer's basic circuit design becomes very complex. So people began to explore the symbolic pseudo-participation operation, and only to retain the method of addition if the original code to calculate the decimal subtraction: 1-1=0, the result is incorrect: 1 - 1 = 1 + ( -1) = [00000001] Hara + [ 10000001] Original = [10000010] = -2 use the inverse code, the truth of the result part is correct: 1 - 1 = 1 + ( -1) = [0000 0001] Original  + [1000 0001] Original = [0000 0001] anti- + [1111 1110] anti- = [1111 1111] Anti- = [1000 0000] original = -0 and anti-code problem is 0, the anti-code will have [0000 0000] original =+ 0 and [1000 0000] original = 2 encoding means 0, so there is a complement to solve the problem [1000 0000] complement (8-bit binary machine number, the complement can also represent a minimum number of -128=[1000 0000]): 1-1 = 1 + ( -1) = [0000 0001 ] Original + [1000 0001] Original = [0000 0001] + [1111 1111] [ = [0000 0000] bu =[0000 0000] original
Four, the original code, complement, anti-code and furtherx mod y equals x minus y multiplied by the quotient of X and y a number of inverse code, in fact, this number for a modulus of the same remainder, and this film is not our binary, but the maximum value can be represented due to the special case of 0, there is no way to represent 128, so the complement range is [-128 , 127]
v. Data overflow testingTo fill up ~ ~ ~
operation Description of the six-bit operation
1. & Bitwise AND "and"function: The corresponding two binary is 1 o'clock, the result is 1, otherwise 0 example: 9&5 = 1001&0101 = 0001, that is, 9&5=1 * law: binary and 1& remain in place, and 0& is 0
2. | bitwise OR "or"function: The corresponding two binary as long as there is one for 1 o'clock, the result is 1, otherwise 0 example: 9|5 = 1001|0101 = 1101, i.e. 9|5=13
3. ^ Bitwise XOR or "xor,eor"function: The corresponding two binary is not the same as 1, otherwise 0 example: 9^5 = 1001^0101 = 1100, that is, 9^5=12 * law: The same integer is different or 0, for example: 5^5 =0 different integers or results and order Independent, example: 5^6^7 = 5^7^6 any number and 0 XOR or result unchanged, example: x^0 = x, x^y^x = X^x^y = 0^y = y
4. ~ Bitwise REVERSE "nor"Function: For each bit of integer negation, the symbol is also the inverse "negation: 0 to 1, 1 to 0" example: ~ =-10 (because the negative number is the complement of the store) ~9=~[00001001] =[11110110] =[11110101] anti-=[10 001010] Original =-10
5. << left Shift (SHL)Format: integer << Left number example: X << N Essence: X * 2n operation: Move x bits to the left n units, high drop, low 0
6. >> Right Shift (SHR)Format: integer >> Right Shift number example: X >> N Essence: x/2n operation: Move X's bits to the right n units, low drop, sign bit unchanged note: The sign bit also moves, moves right Do not change the positive or negative of the integer, the last sign bit to be adjusted to the original value positive sign bit is 0, the highest bit 0 negative sign bit is 1, the highest bit 1
simple application of the seven-bit arithmetic
1. (& Bitwise AND "and") parity judgmentJudging by the modulus: a%2?printf ("Odd \ n"):p rintf ("even \ n"); And one judge: a&1?printf ("Odd \ n"):p rintf ("even \ n");
2. (^ bitwise XOR or "xor,eor") numeric conversionsWith third-party variables: temp = A;a = B;b = temp; Without additional space, mathematical method: a = B-a;b = B-a;a = B + A; Bit operation without extra space: a = a ^ B;b = a ^ B;a = a ^ b;
3. (<< left shift and >> right shift) optimize multiplication efficiencyA The value of SHL B is equal to a multiplied by 2 B for a shr b such as a binary lookup, a heap insert operation, etc.
C Language Original code anti-code complement and bit operation.