p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning Visual C# 2010 (http://p2p.wrox.com/forumdisplay.php?f=563)
-   -   Chapter 3 Exercise 4 Solution (http://p2p.wrox.com/showthread.php?t=79461)

 KurtBergman May 18th, 2010 10:19 PM

Chapter 3 Exercise 4 Solution

in the appendix, the solution to this exercise is:

resultVar += ((var1 * var2) << (var4 / var5));

I could not find an explanation for "<<"

thanks
kb

 KurtBergman May 19th, 2010 12:33 AM

[:confused:]
okay...
found the answer (sort of) here:
http://msdn.microsoft.com/en-us/libr...(v=VS.71).aspx

which leaves me more confused than ever as to why that is the answer to Chapter 3 Exercise 4 Solution

 wwpowell_ga June 22nd, 2010 10:38 PM

Typo

I believe that's just a typo and the correct answer is:

resultVar += (((var1 * var2) + var3) % (var4 / var5));

 bon_chan July 15th, 2010 04:22 AM

Yes it is a typo error.

resultVar += ((var1 * var2) + ((var3 % var4) / var5));

*, % and / have the same priority and in that case it is the leftmost operator that operates first.

 tariq July 15th, 2010 09:20 PM

Operator Priority

According to the book:
* has first priority
/ has second and
% has third
page 51 of Beginning Visual C# 2010

So the corrct answer should be:
resultVar += ((var1 * var2) + (var3 % (var4 / var5)));

Tariq

 bon_chan July 16th, 2010 05:24 AM

No tariq it is not the right answer :

*, / and % have the same priority.

The order displayed in the book or in any website doesn't mean anything.
what I mean is : it is the same whether they write
*, /, %
or
/, *, %

What only matters is the rank (Higher to Lowest)

Try it out on Visual!

 tariq July 16th, 2010 07:57 AM

Thanks

Thanks for the clarification:
What you are saying is that which ever operator comes first out of */and % the calculation will start from there:

Am i right?

For example in this case:
resultVar += (var1 * var2) + (var3 % var4) / var5;

Please correct me if i am on the wrong track.

Tariq

 bon_chan July 16th, 2010 08:27 AM

Yes you got it!

That's exactly it

So in your example, the processing will be performed in this order:
*
%
/
+
+=

That is only if you write it without brackets, I mean like that:
resultVar += var1 * var2 + var3 % var4 / var5;

If you use brackets like for example: resultVar += var1 * (var2 + var3) % var4 / var5;
The order will be:
+
*
%
/
+=

Let me know if you understand my explanation (as English is not my first language).

Bon_chan

 tariq July 16th, 2010 08:13 PM

That is perfect:

Thanks for explaining.

Hope to c u in some other discussion.

Regards
Tariq

 All times are GMT -4. The time now is 07:40 PM.