Contents
- Hexadecimal
- Memory ANALYSIS OF VARIABLES
- Type specifier
- Bitwise operation
- Char type
Go back to top 1. hexadecimal 1. What is hexadecimal
1> it is a counting method, which represents a value.
Count the number of squares
Chinese character: 11 decimal: 11 binary: 1011 octal: 13
2> multiple hexadecimal: decimal, binary, octal, and hexadecimal. That is to say, we have at least four Representation Methods for the same integer.
3> you must understand this in software development.
2. Binary
1> features: only 0 and 1, every 2 to 1
2> writing format: starting with 0b or 0b
3> usage: binary commands \ binary files. variables are stored in binary memory.
4> mutual conversion between binary and decimal
5> N is the data range that can be expressed by binary bits (negative number is not considered): 0 ~ The Npower of 2-1
3. Gossip
1> features: 0 ~ 7. Every 8 hours
2> writing format: starting with 0
3> mutual conversion between octal and binary
4. hexadecimal
1> features: 0 ~ F
2> writing format: starting with 0x or 0x
3> hexadecimal and Binary Conversion
5. Summary
1> Use of calculators in Mac
2> printf outputs in different hexadecimal formats
6. Exercise
1> determine whether the following numbers are reasonable
00011 0x0011 0x7h4 10.98 0986. 089-109
+ 178 0b325 0b0010 0 xffdc 96f 96.0f 96.0f
-. 003 15.4e6 10e8. 7 7.6e-6
2> write their decimal, octal, and hexadecimal respectively.
0b0011 1101 0b0111 1011
3> write their binary
67 056 0x004f
Back to Top 2. Variable memory Analysis
Study the storage of variables in memory
1. bytes and address
To better understand the storage details of variables in the memory, first let's take a look at the "Byte" and "Address" in the memory ".
1> memory in bytes"
2> the bytes occupied by different types are different.
2. Storage of Variables
1> the number of bytes occupied is related to the type and the compiler environment.
2> variable instances
Int B = 10;
Int A = 134;
1) memory addressing from large to small
2) Only binary format is stored
3) each variable has an address. The address of the first byte is the address of the variable.
3> two ways to view the memory address: % x and % P
4> View the binary form of an integer
// Output the binary form of an integer
Void putbinary (int n)
{
Int bits = sizeof (n) * 8;
While (bits --> 0 ){
Printf ("% d", N> bits & 1 );
If (BITS % 4 = 0) printf ("");
}
Printf ("\ n ");
}
3. Storage of negative numbers in memory
1> value range of one byte
2> negative expression
3> source code, reverse code, and complement code
4. Value Range
5. Exercise
Write the storage of the following variables in the memory
Int A = 134;
Int B = 0;
Int c =-10;
Back to Top 3. type specifiers 1. Short and long
1> Differences Between 100l and 100ll and 100
2> output of long and long
3> storage space occupied by different types
1> short and long can provide integer numbers of different lengths, that is, they can change the value range of integer numbers. In the 64-bit compiler environment, int occupies 4 bytes (32 bit) and the value range is-231 ~ 231-1; short occupies 2 bytes (16 bits) and the value range is-215 ~ 215-1; long occupies 8 bytes (64 bit), value range:-263 ~ 263-1
2> to sum up, in a 64-bit compiler environment, short occupies 2 bytes (16 bits), and INT occupies 4 bytes (32 bits ), long occupies 8 bytes (64-bit ). Therefore, if the integer used is not very large, you can use short instead of int, which saves memory overhead.
3> there are many compilers in the world. In different compiler environments, the value ranges of int, short, and long are different from the occupied length. For example, in a 16-bit compiler environment, long occupies only four bytes. Fortunately, ANSI \ ISO has the following rules:
1) short and INT must be at least 16 bits (2 bytes)
2) Long must be at least 32 bits (4 bytes)
3) the short length cannot be greater than int, And the int length cannot be greater than long.
4) Char must be 8 bits (1 byte). After all, char is the minimum data type that we can use for programming.
4> two long, that is, long, can be used consecutively. In general, the range of long is not smaller than long. For example, in the 32bit compiler environment, long occupies 8 bytes and long occupies 4 bytes. However, in the 64-bit compiler environment, long is the same as long and occupies 8 bytes.
5> it should also be clear that: Short int is equivalent to short, long int is equivalent to long, long int is equivalent to long
2. Signed and unsigned
1> first, we must make it clear that signed int is equivalent to signed, and unsigned int is equivalent to unsigned.
2> the difference between signed and unsigned is whether the highest bit of signed is used as the symbol bit. It does not change the data length as short and long, that is, the number of bytes occupied.
1) Signed: indicates a signed symbol. That is to say, the highest bit must be regarded as a signed bit. Therefore, it includes positive numbers, negative numbers, and 0. In fact, the highest bit of an int is a signed bit, which already includes positive and negative numbers and 0. Therefore, signed and INT are the same. Signed is equivalent to signed int and Int. The value range of signed is-231 ~ 231-1
2) unsigned: Indicates unsigned. That is to say, the highest bit is not used as the symbol bit and does not include negative numbers. In the 64-bit compiler environment, int occupies 4 bytes (32bit), so the value range of unsigned is: 0000 0000 0000 0000 0000 0000 0000 0000 ~ 1111 1111 1111 1111 1111 1111 1111 1111, that is, 0 ~ 232-1
Back to Top 4. bitwise operation 1. & bitwise AND
1> Functions
The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0.
2> for example, 9 & 5 is actually 1001 & 101 = 1, so 9 & 5 = 1
3> rule
In binary, the Phase 1 is in the original position, and the phase 0 is 0.
2. | by bit or
1> Functions
If one of the two binary numbers is 1, The result bit is 1. Otherwise, the result bit is 0.
2> example: 9 | 5 is actually 1001 | 101 = 1101, So 9 | 5 = 13
3. ^ bitwise OR
1> Functions
When the binary numbers are different (different), the result is 1. Otherwise, the result is 0.
2> for example, 9 ^ 5 is actually 1001 ^ 101 = 1100, So 9 ^ 5 = 12
3> rule
L the result of the same integer ^ is 0. For example, 5 ^ 5 = 0
L the result of multiple integers ^ is irrelevant to the order. For example, 5 ^ 6 ^ 7 = 5 ^ 7 ^ 6
L so I come to the conclusion that a ^ B ^ A = B
4 .~ Invert
Returns the decimal digits of the binary digits of integer A, and the sign bit is also reversed (0 to 1, 1 to 0)
5. <move left
1> remove all binary digits of integer a from the Left to N places, discard the digits at the top, and add 0 at the bottom. Shifting n places to the left is actually multiplied by the N power of 2
2> because the left shift is to discard the highest bit, and the 0 complement the second bit, the symbol bit is also discarded, and the result value removed from the Left shift may change the positive and negative values.
6.> right shift
1> shifts all the binary places of integer a to the right n bits to keep the symbol bit unchanged. Shifting n places to the right is actually dividing by the N power of 2
2> when the number is positive, the sign bit is 0, and the maximum bit is 0.
3> when it is a negative number, the sign bit is 1, and the maximum bit is 0 or 1, depending on the compilation system.
7. Exercise
1> Use bitwise OR ^ operator to swap values of two variables without introducing other variables
2> parity of bitwise AND & operator Variables
3> compile a function to output the binary form of integers in the memory.
Back to Top 5. Char Type 1. Storage details
ASCII single-byte table (dual-byte GBK \ gb2312 \ gb18030 \ Unicode)
2. Common Errors
Char c =;
Char c = "";
Char c = 'abcd ';
Char c = 'male ';
3. Use it as an integer
In-128 ~ Within the range of 127, it can be used as an integer.
4. Use of % C and % d \ % I
Printf ("% d", 'A ');
Printf ("% C", 68 );
5. escape characters
Escape characters |
Meaning |
ASCII Value |
\ N |
Move the current position to the beginning of the next line (Press enter to wrap the line) |
10 |
\ T |
Jump to the next tab |
9 |
\\ |
A backslash character. |
92 |
\' |
Represents a single quote character |
39 |
\" |
Represents a double quotation mark character |
34 |
\ 0 |
NULL Character |
0 |
6. Exercise
- Write a function to convert lowercase letters into uppercase letters
- Output result of the program
Int main ()
{
Int I = 67 + '4 ';
Char c = 'C'-10;
Printf ("% d-% C \ n", I, I );
Printf ("% d-% C \ n", C, C );
Return 0;
}