Public Group

Weird simple CSharp math issue.

This topic is 2363 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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?

Share on other sites
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

Share on other sites
No bug, it is a bitwise operator (XOR), so it is doing exactly what it is suppose to be doing.

Share on other sites
Oh right thanks. I'm just so used to that being exponential!
Thanks.

Share on other sites
Note that exponential is generally written ** (double asterisk) in programming languages, if it is defined at all (Python supports it, for instance).

Share on other sites
Since nobody's explicitly said: you're looking for Math.Pow.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 14
• 9
• 10
• 12
• 17
• Forum Statistics

• Total Topics
632905
• Total Posts
3009152
• Who's Online (See full list)

There are no registered users currently online

×