What are the simple and interesting programming codes? Programming code of creative advertisements

The happiness of programming lies in that it not only satisfies our inner desire to create, but also wakes up everyone's inner feelings. Write a line of code, and you will know its right and wrong immediately; After you write a program, you can know whether it is successful or not as soon as you run it. It is so exciting to create one interesting picture after another. What are the simple and interesting programming codes?

简单好玩的编程代码有哪些?创意告白的编程代码大全

What are the simple and interesting programming codes?

Change the value of two variables

For example, exchange the values of a and b variables. (By default, they are all int types). The classic and safe solution is as follows:

简单好玩的编程代码有哪些?创意告白的编程代码大全

With the help of the third variable, the problem is perfectly solved, safe and easy to understand. However, I see that some people do not need to use the third variable. Their solution is as follows:

简单好玩的编程代码有哪些?创意告白的编程代码大全

Although this method also successfully exchanged the values of two variables, the error situation should not be underestimated. Taking the int type as an example, the int type variable takes up four bytes, one byte is 8 bits, and the total is 32 bits. The maximum binary number that an int variable can represent is

The decimal number corresponding to 11111111-11111111-11111111-11111111 is 4294967295.

If the sum of the two variables we exchange exceeds this number, an error will occur.

The error occurs in this step: a+=b. Theoretically, about half of them may be wrong, but in fact, they may not be because we will not exchange such a large number of values. In fact, I have done so many program cases that I have never encountered the need to exchange two variables.

The second method can save memory of one variable, but I haven't seen any other advantages. If you think this method is too high, please look at the third method below:

简单好玩的编程代码有哪些?创意告白的编程代码大全

This method uses the XOR float: ^. XOR operation means whether the corresponding bit values of binary numbers corresponding to two variables are different. The difference is 1, and the same is 0, that is, 0 ^ 0=0; 1^1=0; 0^1=1; 1^0=1; For example, 2 ^ 6=4;

Because the binary corresponding to decimal 2 is 010; The decimal 6 corresponds to the binary 110, and the corresponding bits are different or the result is 100, that is, 4. Children who have learned logic circuits should know XOR better.

This method is more lattice intensive, but also has more defects, because XOR operation belongs to bit operation, and bit operation can only be applied to integer type, that is to say, this method cannot exchange two variables with decimals.

Magic endless cycle

Sometimes the following code will have an endless loop:

简单好玩的编程代码有哪些?创意告白的编程代码大全

Originally, I just wanted to make every element of the array zero through the for loop. Unexpectedly, there was an endless loop. Many people think that the problem lies in i<=10, which is the direct cause, but the root cause of the endless loop is in the first line.

It is possible for the compiler to place the storage address of i immediately after the array. When the array goes out of bounds and accesses to a [10], it is exactly the position of i that sets i to 0, which will continue to loop forever. This is why every book says that array bounds can produce unexpected results.

Of course, it is also possible that the compiler does not put the address of i behind the array. You have to see its mood. The way to avoid these mistakes is to develop good programming habits. If you can use local variables, you don't need global variables. If you can use small scope variables, you don't need large ones. For example, the above i can be clearly defined in the for loop.

 shenxiaocen
  • This article is written by Published on January 13, 2023 11:27:18
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/13571.html

Comment