p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Beginning Visual C# 2010 (http://p2p.wrox.com/book-beginning-visual-c-2010-563/)
-   -   Chapter 3 Exercise 4 Solution (http://p2p.wrox.com/book-beginning-visual-c-2010/79461-chapter-3-exercise-4-solution.html)

KurtBergman May 18th, 2010 11: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 "<<"

Can someone please explain?


KurtBergman May 19th, 2010 01:33 AM

found the answer (sort of) here:

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 11:38 PM

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

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

bon_chan July 15th, 2010 05:22 AM

Yes it is a typo error.
And the correct answer is:

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 10: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)));


bon_chan July 16th, 2010 06: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
*, /, %
/, *, %

What only matters is the rank (Higher to Lowest)

Try it out on Visual!

tariq July 16th, 2010 08:57 AM

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.


bon_chan July 16th, 2010 09: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).


tariq July 16th, 2010 09:13 PM

That is perfect:
Thanks for explaining.

Hope to c u in some other discussion.


All times are GMT -4. The time now is 05:15 PM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.