| I have been trying to talk about this problem for a long time. Many companies often use domain names to answer questions. I want to figure it out and analyze it a little bit. Here, let's take a look at a test question of chinanetcenter: // Assume that the hardware platform is intel X86 (little endian) Char * inet_ntoa (uint32_t in) { Static char B [18]; Register char * P; P = (char *) in; # Define UC (B) (INT) B) & 0xff) (Void) snprintf (B, sizeof (B ), "% D. % d. % d ", UC (P [0]), UC (P [1]), UC (P [2]), UC (P [3]); Return (B ); } Int main () { Printf ("% s, % s", inet_ntoa (0x12345678), inet_ntoa (87654321 )); } A difficult question is actually easy to understand. Bit Domain Analysis Bitwise domains are a concept in both C ++ and C, but there are a lot of things to pay attention to in bitwise domains: Large-end and small-end byte order This is very simple, that is, how to determine the starting point. First look at a program: Union { Struct { Unsigned char A1: 2; Unsigned char A2: 3; Unsigned char A3: 3; } X; Unsigned char B; } D; Int main (INT argc, char * argv []) { D. B = 100; Return 0; } Then, how should we assign values to A1, A2, and A3 of X? The binary value of 100 is 0110 0100, so is A1 to A3 a sequential value? No! Let's take a look at whether the low end of the 100 allocation bit is 0 on the left or 0 on the right? Obviously, it is 0 on the right. Let's look at the distribution from A1 to A3 from low-end to high-end. Then, the corresponding <-- Memory increase A3 A2 A1 011 001 00 The reason why the memory size increases is that 011 is at a high level! Instead of the following: A1 A2 A3 011 001 00 Another common case is that a binary number is converted to a point-to-decimal value. How can this problem be solved? It involves the problem of large-end or small-end. Because the above is a byte, there is no such problem. Multiple bytes have problems with the big end and small end, as shown below: Int main (INT argc, char * argv []) { Int A = 0x12345678; Char * P = (char *) &; Char STR [20]; Sprintf (STR, "% d. % d", P [0], p [1], p [2], p [3]); Printf (STR ); Return 0; } This program assumes that it is a small-end byte sequence. What is the result? Let's see how to place it? Each byte is 8 bits, and 0x12345678 is divided into 4 bytes, that is, from the high byte to the low byte:, 78, So how should we put it here? As follows: ---- >>>>> Memory increase 78 56 34 12 Because this is a small end, the small memory corresponds to a low byte, which is the structure above. The next question is a bit confused, that is, how does P point to the beginning of 0x12345678-12? No! 12 is what we call the beginning, but not the memory At the beginning, let's look at the memory distribution. If we know that the operations from P [0] to P [1] ARE & P [0] + 1, we will know, the P [1] address is larger than the P [0] address, that is, the P address It also increases with memory! 12 ^ P [3] | 34 | P [2] | 56 | P [1] | 78 | P [0] Memory increases with the arrow! At the same time, small-end storage also increases in memory from low to high! In this way, we know how the memory is distributed. So: Sprintf (STR, "% d. % d", P [0], p [1], p [2], p [3]); STR is the result: 120.86.52.18 What is the opposite? Int main (INT argc, char * argv []) { Int A = 0x87654321; Char * P = (char *) &; Char STR [20]; Sprintf (STR, "% d. % d", P [0], p [1], p [2], p [3]); Printf (STR ); Return 0; } It is still a small end, and the 8-bit is a byte, so it is like this: 87 ^ P [3] | 65 | P [2] | 43 | P [1] | 21 | P [0] The result is: 33.67.101.-121 Why is it negative? Because the default char of the system is signed, it is 0x87 or 135, and it is greater than 127. Therefore, minus 256 to get-121. So what should we do? As follows: Int main (INT argc, char * argv []) { Int A = 0x87654321; Unsigned char * P = (unsigned char *) &; Char STR [20]; Sprintf (STR, "% d. % d", P [0], p [1], p [2], p [3]); Printf (STR ); Return 0; } Use unsigned! Result: 33.67.101.135 Symbol of the bit field (plus or minus) After reading the values of the big end and small end, let's look at the value of the bit field. We talked about some of the above. The first is that the bit field is measured by bit and the int value is 32-bit char is 8. Bit is the same, it is very simple, however, note that bit fields also have positive and negative, indicating the signed attribute, that is, the highest bit, also involves the complement code, which is generally considered very Look at the program: # Include <stdio. h> # Include <stdlib. h> # Include <string. h> Int main (INT argc, char ** argv) { Union { Struct { Unsigned char A: 1; Unsigned char B: 2; Unsigned char C: 3; } D; Unsigned char E; } F; F. E = 1; Printf ("% d/N", F. D. ); Return 0; } <Small End> So what is output? Replace: # Include <stdio. h> # Include <stdlib. h> # Include <string. h> Int main (INT argc, char ** argv) { Union { Struct { Char A: 1; Char B: 2; Char C: 3; } D; Char E; } F; F. E = 1; Printf ("% d/N", F. D. ); Return 0; } What is output? If it is a small end, then, d. A gets 1 on it, and this is a non-signed Char. The former output is 1, no problem, and the second output is-1, haha. |