Dark Horse programmer-C language open-door chip memory analysis, dark horse programmer open-door
IOS training, iOS study --------- technical blog, hope to communicate with you! ------------
I. Summary of various hexadecimal formats
1. Binary
(1) in C language, the binary starts with 0 B and the output binary format does not have a fixed format. udfs can be defined as follows: # include <stdio. h>
Int main ()
{
Void printfBinary (int );
PrintfBinary (20 );
Return 0;
}
Void printfBinary (int n)
{
Int bits = sizeof (int) * 8; // calculates the number of bytes occupied by the integer variable.
While (bits --> 0 ){
// The binary form of n moves bits byte to the right, and then bitwise AND operation is performed on the same 1. The original value is retained for printing.
Printf ("% d", n> bits & 1 );
If (bits % 4 = 0 ){
Printf ("");
}
}
}
2. Gossip
The octal value starts with 0 and uses % o to output an integer without a symbol. The hexadecimal value is 3.
The hexadecimal format starts with 0x and uses % x to output an integer.
4. Decimal
In C language, except for the hexadecimal numbers in the preceding three formats, common data is represented in decimal. Use % d to output signed integers, and % u to output unsigned integers
2. How to swap two variable values
1. Use the intermediate variable value method for interchange
Assume that the values of two variables are int a = 5, B = 6, and another variable is used for exchange. The specific code is as follows:
Void swap ()
{
Int a = 5, B = 6;
Int c = a; // Add the value of a to c.
A = B;
B = c;
}
2. Direct Exchange
- A = B-a; // The difference between a and B.
- B = B-a; // The original value of B is changed to.
- A = B + a; // The original value of a is changed to B
3. Exchange Based on bitwise OR operations, and use the bitwise OR calculation law, that is, a ^ B ^ a = B
- A = a ^ B;
- B = a ^ B;
- A = a ^ B;
4. The exchange of two numbers reminds me of an interview question, as shown below:
There are two cups, A = 5 ml and B = 3 ml respectively. Ask how to get 4 ml of water. The water is infinite, but you cannot use another container, there are two methods for this question:
(1) first, pour A filled with water into B, then the B container is poured out, the remaining A = 2 is then poured into B, and then filled with A and then poured into B, at this time, A = 4 ml;
(2) first fill B in A, then fill B in A, B is 1 ml, pour A, B into A, then, A = 4 ml can be obtained after B is filled and then A is poured into;