I would like to know how to implicit casting works in case of expressions between unsigned int of various bits ie uint8_t,uint16_t etc and ways to avoid it explicitly. For this reason i sumarrized the following cases:
- How implicit casting in case of uint8_t addition,substraction,multiplication,division would work?
uint8_t A;
uint8_t B;
uint16_t C;
C= A+B; (uint8_t + uint8_t )
C= A-B; (uint8_t + uint8_t )
C= A*B; (uint8_t + uint8_t )
C= A/B; (uint8_t + uint8_t )
Explicit declaration would be C= static_cast<uint16_t>A+B; or C= static_cast<uint16_t>(A+B);.Is this correct? Is there any difference between C= static_cast<uint16_t>A+B; or C= static_cast<uint16_t>(A+B)?
- How implicit casting in case of expressions of unsigned int(with U literal)and uint8_t would work? Is there also a difference between the order that it matters ie 1UB;(unsined int * uint8_t ) or B1U;(uint8_t * uint8_t )
C= A+1U;(uint8_t + uint8_t )
C= A-1U;(uint8_t - uint8_t )
C= 1U*B;(uint8_t * uint8_t )
C= 1U/B;(uint8_t / uint8_t )
Explicit casting would be C= static_cast<uint16_t>A+1U; or C= static_cast<uint16_t>(A+1U); C= static_cast<uint16_t>1UB; or C= static_cast<uint16_t>(1UB);.Is that correct Is there any difference between those lines?
- How the implicit casting in case of expressions would work.Is the normal order take into account? What would it be the final type of the expression?
C= 1U/(A-1U); (unsigned int / (uint8_t -(unsigned int))
C= (C-(A/B))/B; (uint8_t -(uint8_t /(unsigned int))/(uint8_t)
How should static_cast look in this case? Only the first variable (1U or C) would define for the rest C= static_cast<uint8_t >(1U)/(A-1U);
- How the implicit casting in case of standard functions would work
sizeof(A) returns size_t
C=abs(-1*A) returns int in case of int parmaters
Explicit casting would be C= static_cast<uint16_t>sizeof(A) and C= static_cast<uint16_t>abs(-1*A). Is that correct? What about C= static_cast<uint16_t>abs(-1)*A)?
- How the implicit casting in case of function parameters would work.
uint16_t sum(uint16_t C1,uint16_t C2);
C=sum(A,B-1U/2U);
C=sum(A,1U/2U-B);
Explicit casting would be C= sum(static_cast<uint16_t>(A),static_cast<uint16_t>(B-1U/2U)). Is that correct?
I saw in Opencv a similar to static_cast function called saturate_cast. Would it be a beter solution in any of the above cases?