This article mainly discusses the calculation problem of unsigned integers exceeding the range in C/C++.
Question raised
There is no time stamp acquisition function in the SDK of nrf52832. In order to calculate the performance, and to provide time stamps (millisecond level) to some libraries, you need to use a timer to achieve the interface to obtain milliseconds.
nrf52832 is 32 bits. Calculated in milliseconds, the maximum value will be reached in about 49 days. I don’t understand how to deal with the situation after the millisecond value overflows. I read some posts and said that I was dealing with overflow inversion alone, but I always felt that this was not good, so I focused a little time to learn about overflow (or carry) of unsigned numbers, and wrote some code to test it.
Design ideas
For easy debugging, this article uses 32-bit virtual machine Linux for testing. useget_time
Get the second value of the system, and its value isg_ms
Indicates that a thread is openedtime_handler
Accumulate time values once per second, and open another threadmyfunc_sleep
Statistics take time.
It should be noted that the above is just a simulation demonstration, aiming to illustrate the essential issues, and is not actually used.
Engineering code
First, simply test the addition of unsigned numbers, the function is as follows:
// Test the difference after unsigned overflow deltavoid delta_test(int delta) { mytime_t start = 0xfffffffe; /* Take ms as 10 as an example, the result obtained by ent is 8 8 - start = 10 Therefore, even after overflow, the difference remains unchanged In the delay function, even if the timestamp is overflowing, there is no problem. */ mytime_t end = start + delta; mytime_t mydelta = end - start; printf("end: %u start: %u delta: %u mydelta: %u\n", end, start, delta, mydelta); for (int i = 0; i < delta; i++) { printf("%u %d\n", start, start); start++; } }
Test code:
delta_test(10);
The printing results are as follows:
end: 8 start: 4294967294 delta: 10 mydelta: 10
4294967294 -2
4294967295 -1
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
The starting value is0xfffffffe
,Right now4294967294
, print with signed value-2
. When more than0xffffffff
Then from0
Start counting. If you look at the results only, you can get:8 - 4294967294 = 10
, consistent with the passed parameters.
The following is a multi-threaded test code:
The timestamp variable defined in the code ismytime_t
, is actually an unsigned typeunsigned
. In addition, the test was also done. Using the uint8_t type on a 32-bit machine can also get the correct value, but the negative number cannot be printed out.
test
wheng_start
The value of0xfffffffe
When the test results are as follows:
test of unsigned overflow.. sizeof: 4 after sleep 3 s 1 - 4294967294 = 3 after sleep 3 s 4 - 1 = 3 after sleep 3 s 7 - 4 = 3 after sleep 3 s 10 - 7 = 3 after sleep 3 s 13 - 10 = 3
wheng_start
The value of0xfffffff0
When the test results are as follows:
First test: test of unsigned overflow.. sizeof: 4 after sleep 2 s 4294967282 - 4294967281 = 1 after sleep 2 s 4294967284 - 4294967282 = 2 after sleep 2 s 4294967286 - 4294967284 = 2 after sleep 2 s 4294967288 - 4294967286 = 2 after sleep 2 s 4294967290 - 4294967288 = 2 after sleep 2 s 4294967292 - 4294967290 = 2 after sleep 2 s 4294967294 - 4294967292 = 2 after sleep 2 s 0 - 4294967294 = 2 after sleep 2 s 2 - 0 = 2 after sleep 2 s 4 - 2 = 2 after sleep 2 s 6 - 4 = 2 after sleep 2 s 8 - 6 = 2 Second test: test of unsigned overflow.. sizeof: 4 after sleep 2 s 4294967282 - 4294967280 = 2 after sleep 2 s 4294967284 - 4294967282 = 2 after sleep 2 s 4294967286 - 4294967284 = 2 after sleep 2 s 4294967288 - 4294967286 = 2 after sleep 2 s 4294967290 - 4294967288 = 2 after sleep 2 s 4294967292 - 4294967290 = 2 after sleep 2 s 4294967294 - 4294967292 = 2 after sleep 2 s 0 - 4294967294 = 2 after sleep 2 s 2 - 0 = 2 after sleep 2 s 4 - 2 = 2 after sleep 2 s 6 - 4 = 2 after sleep 2 s 8 - 6 = 2
Judging from the results, it basically meets the requirements, that is, the delay is 2 seconds, and the statistical time is 2. ——Whether there is an overflow or not.
Extended knowledge
The numerical value in the computer stores 2's complement (2's complement). The complement of positive numbers is itself, and the complement of negative numbers is to take the inverse code based on the original code, and add 1 to the last position.
The gyroscope and acceleration values of the mpu6050 chip are 16-bit signed values, which are stored in the form of 2's complement.
summary
For functions of timed and delayed classes, the variable that records the timestamp is an unsigned number.The type is unsinged, and the range limit cannot be added. This refers to the largest platform, such as a 32-bit system, which uses a 32-bit unsigned number, while a 64-bit system is a 64-bit unsigned number.. When the numeric value of the variable overflows, its value is 0, but the timing function is normal and there is no need to deal with the overflow situation.
This is the end of this article about the overflow of C++ unsigned integers. For more related content on unsigned integers, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!