- In-process
- Bit arithmetic
Introduction to the System
A way of counting, a representation of a number.
Common binaries are: binary, decimal, octal, and hexadecimal.
Binary:
0 and 1, the C language represents the beginning of 0b or 0B.
Octal:
0,1,2,3,4,5,6,7 a number starting with 0 in the C language, for example, 045
Decimal:
Natural number
Hexadecimal:
0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f the number of 0x or 0X in C language
Conversions between the binaries:
The other binary conversions into decimal three elements:
1. Digits: The position of the digital in a number. A sequence from right to left digits is 0,1,2,3 ...
2. Cardinality: A few binary cardinality is a few
3. Bit right: bit right = Digital value * Cardinality ^ digit
The sum of your rights is the representation of this number converted into 10 binary.
Decimal Turn binary:
In addition to 2, the remainder of the sequence is reversed is the binary representation of the number.
Binary goto decimal:
Add all the right bits
Binary turn 16 binary:
Integer part, four-bit binary is combined from left to right, fractional part from right to left;
Hexadecimal:
One demolition four;
Binary turn octal:
Triad
Octal into binary:
One demolition three;
Number of machines:
a binary representation of a number in a computer , called the number of machines. The number of machines is signed , in the computer with a number of the highest bit to store the symbol , positive 0, The negative number is 1.
Truth :
The true value of the number of machines with a signed bit is called the truth of the number of machines.
Because the first bit is the sign bit , the form value of the machine number is not equal to the true value. For example the above signed number 10000011, its highest bit 1 is negative , its true value is -3 instead of the form value 131( 10000011 converted to decimal equals 131). So , for the sake of differentiation , the true value of the number of machines with the sign bit is called the truth of the machine number.
Original code, anti-code, complement:
how the data is stored in the computer: The data is stored inside the computer in the form of a complement.
The data is divided into signed and unsigned numbers:
unsigned numbers are positive , and are converted directly from decimal to binary direct storage ( which is actually the decimal complement ) . The signed number is stored inside the computer in the form of a complement. ( the highest digit of a positive number is the sign bit 0, and the highest digit of the negative number is the sign bit 1.)
for positive numbers : anti-code = = complement = = Original code. for negative numbers : anti-code = = except for the sign bit, you take the reverse. Complement = inverse code +1)
In binary complement storage
Positive number: Original code, anti-code, and complement are the same
Negative: Anti-code = source reverse, complement = Anti-code +1, or complement = Source Reverse + 1, premise is the sign bit unchanged
The first address of a positive number is 0, and its original code is a binary number converted from decimal to
The first address of a negative number is 1, and the bit behind the original code is a binary number that is converted into a decimal , and is represented by a complement of signed numbers.
1) original code
The original code is the absolute value of the symbolic bit plus the truth , that is, the first digit represents the symbol , and the remaining bits represent the values .
2) anti-code
The inverse code is represented by : the inverse code of a positive number is its own; negative number of the inverse code is on the basis of its original code , the sign bit is unchanged , the remaining bits are reversed .
3) complement
the complement is expressed in the following ways: a positive complement is its own; The complement of a negative number is on the basis of its original code , the sign bit is unchanged , the rest of you take the reverse, and finally +1. ( that is, on the basis of the inverse code +1)
Bit arithmetic
, c6 bit manipulation operators. These operators can only be used for integer operands ,char ,short,int with long type.
1) & Bitwise AND formula : The same 1 is 1, 0 is 0
only the corresponding two binaries are 1, the result bit is 1, otherwise 0
2) | Bitwise or formula : There are 1 1
As long as the corresponding two binary has one of 1 , the result bit is 1, otherwise 0
3) ^ bitwise XOR : Same as 0 non 1
When the corresponding binary is different ( not the same ) , the result is 1, otherwise 0
4) ~ Take back
Inverse of each binary (0 Change 1, 1 to 0)
All of the above are in binary operation
1) << left shift
1, each binary all left shift n bit , high drop , low 0
1) left shift may change the positive and negative of a number
2) move left 1 digits equal to *2^n
Example : a fast calculation of n times A number multiplied by 2
(8<<3 equals 8*2^3)
2 ) >> move right
Each binary all right shifts n bits , keeping the sign bit constant
x >> N, All bits of x move n bits to the right, the removed bits are deleted , and the bit complement sign bits are moved in
1, right shift does not change the symbol of a number
2 Right shift n bit is equivalent to /2^n
Purpose : fast calculation of a number divided by 2 N- squared (8>>3 equals 8/2^ 3)
Tips :
1) any number and 1 perform & operation to get the lowest bit of this number
2) want to put a position 0, let it a position with 0 for & operation
Exercises:
1 /*2 write a shift function so that the shift function can both loop left and loop right. When parameter n is greater than 0, the left shift is shifted to the right when parameter n is less than 0.3 4 Explanation: Loop Right Shift: Place the right-hand position on the left5 such as: 1101 0101 1100 Loop left four-bit 0101 1100 11016 7 Loop left Shift: Place the left-hand position on the right8 */9#include <stdio.h>Ten OneUnsigned bittransfer (unsigned,int);//function Declaration A - intMainintargcConst Char*argv[]) { -unsigned a =9; the intn =Ten; - -unsigned B =Bittransfer (A, n); - +printf"the number of unsigned 9-bit shifted 10-bit is:%u\n", b); - + } A /** at * @author Si Yingcheng, 15-06-25 21:06:59 - * - * @param number to shift - * The number of bits @param n shifted - * - * Number after @return shift in */ -unsigned bittransfer (unsigned number,intN) { to if(N >0) + return(number << N) ^ (number >> ( +-n)); - Else{ then =-N; * return(number >> N) ^ (number << ( +-n)); $ }Panax Notoginseng}
Binary, bit operation notes