Exponentiation

  • Thread starter Thread starter James McGivney
  • Start date Start date
J

James McGivney

What is happening here ?

long longg = 5;
longg = longg + (2 ^ 8);

the answer should be 5 + 256 or 261

but at the end of the above operation C# returns
longg = 5 + 10 or 15

What's going on here ?

What is the correct syntax for Exponentiation ?

Thanks,
Jim
 
James McGivney said:
What is happening here ?

long longg = 5;
longg = longg + (2 ^ 8);

the answer should be 5 + 256 or 261

but at the end of the above operation C# returns
longg = 5 + 10 or 15

What's going on here ?

It's using the bitwise XOR operator, ^. 2^8 is 10, hence your result.
What is the correct syntax for Exponentiation ?

There's no exponentiation operator in C# - you need to use the
framework Math.Pow method.
 
Hi James,

Just as Jon said, ^ is the bit operator in C# language.
I think you should refer to the Math class in .Net Framework library. Do
like this:

private void button1_Click(object sender, System.EventArgs e)
{
double result=Math.Pow(2, 8);
result+=5;
MessageBox.Show(result.ToString());
}

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top