Weird simple CSharp math issue.

Started by
4 comments, last by TheUnbeliever 11 years, 11 months ago
I have a weird bug!
The line

Console.WriteLine(2^32);
Console.WriteLine((uint)2^32);

both show the result is 34...
And that's basically my entire program.

I ran a loop for each possible number for i from 32 down and did 2^i.
34
29
28
31
30
25
24
27
26
21
20
23
22
17
16
19
18
13
12
15
14
9
8
11
10

Thoughts?
Advertisement
What are you expecting it to return? Are you thinking that ^ is a power operator (it's not)?

See here for c# operators: http://msdn.microsof...y/6a71f45d.aspx
No bug, it is a bitwise operator (XOR), so it is doing exactly what it is suppose to be doing.
Oh right thanks. I'm just so used to that being exponential!
Thanks.
Note that exponential is generally written ** (double asterisk) in programming languages, if it is defined at all (Python supports it, for instance).

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Since nobody's explicitly said: you're looking for Math.Pow.
[TheUnbeliever]

This topic is closed to new replies.

Advertisement