How to convert the signed DWORD to Unsigned DWORD ?
I have one method of doing which is Two’s complement.
But, when i did, compiler is giving an error that bitwise operation isnt allowed for signed numbers (as per the MISRA rule 2004 12th rule) .
so then how can i convert to unsigned Dword?
long int num1,num2,
unsigned long int num3;
num1 = 293;
num2 = 296;
num3 = num1 - num2;
if i run the code then num3 is loaded with some 0xFFFFFFE somevalue like this.
but actually the desired value 3 should get store?
so i made the unsigned long int num3 to long int num3.
Then num3 signed value (-3 in this case) should be converted to 3 without doing twos complement?
The definition of the conversion from signed to unsigned is to take the
modulus of the value, so that the results always correspond to what you
would get by taking the raw bits on a two’s complement machine. But you
don’t want to convert
-3; you want to convert the distance, which isthe absolute value of the difference:
abs( num1 - num2 ).