algorithm - binary opposite number -


I want to get the opposite number of the binary number (x -> - (x) and - (x) - & Gt; x).

What will be the algorithms? I think change all the bits ("1" to "0" and "0" to "1") and add "1" for it is it okay?

thnx

Your algorithm is correct for two complementary numbers:

  -x = ~ X   

and for sign-magnitude numbers:

  -x = x ^ (1  < / Pre> 

where the sign bit is offset in the word n I am using C operator notation - ^ is a bitwast exclusive or operator, and & Lt; & Lt; is a left shift.

Representing the second number, you have to use some other operation.

Comments