Supposing that the implementation uses 32-bit int
, and wDeltaTime1
is an int
, then the operations in DeltaTime1 = (uint16_t) - (int16_t)wCaptureTime;
are:
- The
(int16_t)
cast converts the uint16_t
wCaptureTime
to int16_t
. The value remains 35.
- This
int16_t
is automatically promoted to int
. The value remains 35.
- The unary
-
operator negates the value, producing −35.
- The
(uint16_t)
cast converts it to uint16_t
. Conversion to uint16_t
is performed modulo 65536, so the result is −35 + 65536 = 65501.
- This
uint16_t
converted to the type of wDeltaTime1
, int
. The value is 65501.
- This value is assigned to
wDeltaTime1
, producing 65501 in wDeltaTime1
.
The operations in wDeltaTime2 = 0xFFFF - (int16_t)wCaptureTime;
are:
0xFFFF
is an int
constant with value 65535.
- The
(int16_t)
cast converts the uint16_t
wCaptureTime
to int16_t
. The value remains 35.
- This
int16_t
is automatically promoted to int
. The value remains 35.
- The binary
-
operator subtracts 35 from 65535, producing 65535 − 35 = 65500.
- This value is assigned to
wDeltaTime2
, producing 65500 in wDeltaTime2
.
One source of confusion you may have is that negation in uint16_t
is equivalent to subtracting from 65536, not 65535 (or 0xFFFF
). 65535 is the “all ones” pattern in 16 bits; it does not act like zero. 65536 is where uint16_t
wraps around, so it acts more like zero.
The bitwise complement operator, ~
, would change (considering just the low 16 bits) 35 to 65500.