This Road C language title of "trap", the foundation is not good is easy on the sets!

1、

To understand today's code requires a little knowledge reserves.

2、

Title and analysis

Man of few words said on the code:

What do you make the result?

Most of the estimated results are calculated friends  .

initial analysis

Let us look at, roughly analyze this code, the code can be found in the knowledge examines two points:

First, data type conversion

Second priority issue operators

Above this line, - the highest priority, a first affirmed is performed bitwise, and then + a higher priority, so the implementation of 4 + 1 = 5, and finally performs right shift operations.

Thus the above code is equivalent to:

I.e. firstly a bitwise negation, ~ ~ = 0xA5 (1010 0101b) = 0101 1010b = 0x5A

5 obtained then right 0x2. The results are calculated 2?

Let us directly run the code to see the results:

what happened? The answer is not expected in the 2, b suddenly equal to 250! ,Why is this?

Implicit data type conversion and enhance the value of an integer

Let's take a closer look above this line of code, found there was an operation between different types : a data type is char, the data type is not specified, 4 and 1, c language compiler will default it to an int type.

I believe we all know, in the course of operation in C language, if inconsistent operation on both sides of the character data type , the compiler will be implicit data type conversion automatically .

Overall, this data type conversion is more complex, but generally follow this principle: avoid loss of data accuracy

What are the principles above mean?

If the symbol sides operator inconsistent data type, the compiler always try to convert a wider data type.

If the number of calculation process is not float, then they certainly are integers, the compiler will all generally smaller than the width of an int data type promotion to int, a phenomenon known as " whole value upgrade "

 

further analysis

We know the whole value of the upgrade, let's again look at the top line of code:

is a unsigned char, int is less than the data width, so the compiler to perform a bitwise operation before, will increase to a first data type int.

On different machines, different data widths of int, int on my machine size is 4 bytes.

Therefore the type of data a conversion, a = 0x 00 00 00 a5 , is the bitwise after ~ a = 0x ff ff ff 5a , and then press the right bit 5 (MSB autocomplete 0), is obtained 0x07 ff ff fa, b in the numerical value to the time multiplexed, since the data type is a b char, only one byte, thus occurs data truncation , only the lowest byte preserved, i.e. 0xfa = 250.

 

to sum up

By this question, we can find, if implicit data type conversion c language is not familiar with, it is easy on the sets.

In addition, operator precedence is quite difficult to remember, the proposal is still clearly the order of execution with parentheses .

   For those who love programming, the answer together with a group of small learning partner is very important! I have started to learn a programming zero-based exchange club ( group ), as well as learning video files, are welcome beginners and advanced in little friends!

Published 520 original articles · won praise 132 · views 80000 +

Guess you like

Origin blog.csdn.net/HUYA69/article/details/105292754