1
votes

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?

2
cast it to unsigned first, then apply the bitwise operation.Alex Brown

2 Answers

3
votes

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 is the absolute value of the difference: abs( num1 - num2 ).

0
votes
long num3;
...
if (num3<0) num3=-num3;

Or what exactly is the problem?