开发者

Solve logical and mathematical Equation

开发者 https://www.devze.com 2023-04-06 06:11 出处:网络
I would li开发者_JS百科ke to solve following equation: x^2-bitxor(2,x)=0 Is it possible ?bitxor(2,x) wil either add 2 or subtract 2 to/from x, so you have to solve both:

I would li开发者_JS百科ke to solve following equation:

x^2-bitxor(2,x)=0

Is it possible ?


bitxor(2,x) wil either add 2 or subtract 2 to/from x, so you have to solve both:

x^2 - x + 2 = 0

and:

x^2 - x - 2 = 0

and then test to see if the any of the solutions work in the original expression.

In the first case the roots are complex, in the second case the roots are -1 and +2.

Substituting back into the original equation:

x = -1 => (-1^2) - bitxor(2, -1) = 1 - (-3) = 4

x = 2 => (2^2) - bitxor(2, 2) = 4 - 0 = 4

So apparently there is no real integer solution.


There are solutions in modular arithmetic, which is precisely the arithmetic that most computers use for unsigned integers. For example, consider the case of modulo 256 arithmetic. In C and C++, this is the arithmetic of unsigned char. Here we have two solutions, 91 and 166. (See http://www.wolframalpha.com/input/?i=x%5E2+mod+256+%3D+x+-+2%2C+x+%3E+0%2C+x%3C+256 ).

In fact, for any modulo 2^N system, there will be two solutions that sum to 1 (modulo 2^N).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号