1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C# Programming Operator precedence

  1. May 8, 2013 #1
    1. The problem statement, all variables and given/known data
    What is the value of x = 5 + (9 * 5) * (3 ^ 3/2 - 20).

    2. Relevant equations
    Operator precedence.

    3. The attempt at a solution
    x = 5 + (45) * (3 ^ 3/2 - 20).
    Is the "^" here the exclusive or operator? In that case wouldn't x have 2 possible values?
    Last edited: May 8, 2013
  2. jcsd
  3. May 8, 2013 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    Looks like a plus to me. Check your post for typos. I see at least two.

    Anywho, aren't XOR operators and such bitwise operators rather than arithmetic operators?
  4. May 8, 2013 #3
    Edited it there. No, I'm not referring to the plus sign above. I'm literally asking about this "^" sign. The XOR operator. I thought they were only used in boolean comparisons and stuff.
  5. May 8, 2013 #4


    Staff: Mentor

    The ^ operator in C# is defined for pairs of integral arguments (int, uint, long, ulong), pairs of args of some enumerated type, and pairs of type bool. For an expression consisting of int constants, like yours, ^ computes the bitwise logical exclusive OR of the two operands.

    Keep in mind the relative precedence of the operators in your expression: * and / are higher than + and -, which are higher than any of the logical operators.
  6. May 8, 2013 #5


    User Avatar
    Gold Member

    Also keep in mind that most C/C++ compilers do automatic typecasting, so you may THINK you have one kind of variable after an operation, but you really have another kind. For example, depending on what operation is performed on it, the term


    might reduce to an integer (1) or to a single (1.5). I think the term by itself reduces to 1 because both parts are integer so the compiler will assume you want an integer answer (unless you set a = 3/2 and a is a single. Anyway that kind of thing is important to keep in mind, and to understand how your compiler handles it.
  7. May 8, 2013 #6
    Thanks for the help lads. So, what exactly should the output be? An I right in assuming there'll be 2 possible values for x? It was actually an exam question and not a homework question so I'm just wondering out of pure curiosity like.
  8. May 8, 2013 #7


    Staff: Mentor

    x ends up as -805. The tricky part is evaluating 3^(-19), which entails writing -19 as a binary number so you can calculate the XOR with 3.
  9. May 8, 2013 #8
    Ohh, ok. I see. Thanks for that man.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted