Two methods of representing discrete values: one is to use bits to represent discrete values, and one is to take discrete values as elements of a finite set.
Easy to forget knowledge points:
1, complement
The complement representation of the nonnegative integer n is the bits string of N. If we do a bitwise negation of the bit string and add the result to 1, we get the-n complement,
2, left shift and right shift
The left and right two operands of the shift operator must be an integer expression, and each operand will perform a type elevation. The type of the entire expression is the type of the promoted left operator.
For example: char c = ' Z ';
When the C << 1 shift is performed, it is promoted to int, and when the shift expression is evaluated, the two operands perform an integer elevation, and the type of the expression is the type of the promoted operator. Thus, the value of an expression like c<<1 is stored in 4 bytes.
3. Priority and combination of shift operators
a<< b >> 1 is equivalent to (a << b) >> 1
A << 1 + 2 << 3 equivalent to (a<< (1+2)) << 3
A + b << a >> B is equivalent to ((a+b) << (12*a)) >>b
4. Mask Code
A mask is a constant or a variable that extracts the desired bits from another variable or expression, because the binary expression of int constant 1 is entered: 00000000 00000000 00000000 00000001
It can be used to determine the low end bit of an int expression, and the following code uses this mask to print out a sequence of alternating 0 and 1. If you want to find a number in a position of a number, then the mask of that position is 1 and the operation can be obtained.
Example:
void Bit_print (int a)
{
int i;
int n = sizeof (int) *char_bit; int Limit.h
int mask = 1<< (n-1);
for (i = 1;i <= n; ++i)
{
Putchar (((A & mask) = = 0)? ' 0 ': ' 1 ');
a<<=1;
if (I%char_bit = = 0 && i < n)
Putchar (");
}
}
Example explanation: The above example function is to output the binary number of an integer: one of the int mask = 1 << (n-1); This statement shifts 1 to the highest bit of the bits number. Putchar (((a&mask) = = 0)? ' 0 ': ' 1 '); This statement is to perform the arithmetic of each input number and mask, the highest bit of each number and the binary of the highest bit after the operation. Then the a<<1 shifts the A to the left, changing the highest bit to the next loop output.
5, Packaging and reconciliation package
Use a bit expression to compress boundary data based on byte. This not only helps save space, but more importantly helps save time. On a 4-byte machine, each instruction cycle handles 32 bits in parallel. The following function can wrap 4 characters into an int and use the shift operation for a byte based wrapper.
#include <limits.h>
int pack (char A,char b,char C,char D)
{
int p = A;
p = (P << char_bit) | b
p = (P << char_bit) | C
p = (P << char_bit) | D
return p;
}
Program interpretation: The above program packaging A,b,c,d become p and use the print function, print the results.
Unpack: #include <limits.h>
Char unpack (int p,int k)
{
int n = k * Char_bit;
unsigned mask = 255;
Mask << N;
Return ((P & mask) >> N);
}