Wrox Programmer Forums
|
BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4
This is the forum to discuss the Wrox book Beginning Microsoft Visual C# 2008 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780470191354
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 23rd, 2009, 03:07 AM
Registered User
 
Join Date: Sep 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Please help me understand

On page 65 the following is shown

Code:
int myColor = 0;
bool containsRed;
myColor = myColor | 2;
myColor = myColor | 4;
containsRed = (myColor & 4) == 4;
OK, Line 3 and 4 show the use of "|". This is a boolean operator so the way I read it it is asking if either myColor or 2 is true. Will neither are, myColor is 0 and 2 is 2. Same thing happens in the next line but with 4.

Line 5 then uses "&" which assigned a true value if both items are true. So the outcome would be false. False does not == 4.

Debugging the program shows that Line 3 and 4 add the values to the integer. But on page 47 where we go over integer operators "|" is not listed.

Also on line 5 where "&" is used myColor = 6 and not 4.

I can kinda quess what is happening, I just wish the book would explain it. I hate guessing on how something is working just to find out that my guess was wrong.

Thanks in advance,
Sean

Last edited by SeanDunn; September 23rd, 2009 at 03:50 AM.. Reason: Added code block
 
Old September 23rd, 2009, 03:30 AM
Registered User
 
Join Date: Sep 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb Been playing around with this.

Ok, tell me if I am correct on this. If you use a boolean operator on a integer then the integer is viewed as if it were and array of bit and each bit place is evaluated separately.

So I tried

myColor = 11 | 7 // or 1011 | 0111 and I get 17 or 1111

then I tried

myColor = 11 & 7 // or 1011 & 0111 and I get 3 or 0011
 
Old September 23rd, 2009, 04:25 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

I'm still getting used to all of this as well so I could of misunderstood but here goes (I onyl just completed chapter 4 myself):

The operators "&" and "|" don't actually target the value placed in the variable but rather the "series of bits" placed in the variable.

The "&" operator is a Logical AND as such it will only assign a value of true if both are true otherwise the outcome will be false.

The "|" is a Logical OR and will assign a value of true if either value is true.

Quote:
OK, Line 3 and 4 show the use of "|". This is a boolean operator so the way I read it it is asking if either myColor or 2 is true. Will neither are, myColor is 0 and 2 is 2.
It is saying assign the value 2 to myColor if either myColor or 2 are true. As the binary representation of 2 is 010, this assigns 010 to myColor.

In binary 1 is a value of true and 0 is a value of false.

Quote:
Same thing happens in the next line but with 4.
Similarly this line is saying that if myColor or 4 contain a "true" value then assign that to myColor so myColor now stores 110.

Quote:
Also on line 5 where "&" is used myColor = 6 and not 4.
The last line is saying that containsRed should be true if the bit representation of myColor contains the bit value for red(100) which is also the representation for 4.

As we added the bit values for green(010) and red(100) to myColor it currently stores a bit value of (110) as we are using the "|" (logical OR) and not "&" (Logical AND).

So to break down the last line:

Code:
containsRed = (myColor & 4) == 4;
Code:
(myColor & 4)
Check that myColor and 4 are true and the same value, as it uses the "&" (logical AND) it reads the value as 100 and 110 because when the two numbers are added together the middle digits are different it is read as 0 (false).

Code:
(myColor & 4) == 4
This is basically asking "Is 4 equal to 4?"

Code:
containsRed = (myColor & 4) == 4;
This places a value of true into the boolean variable containsRed as the statement is correct.

Anyway that was my understanding of it but it would be great if someone with a bit more experience could clarify it for me as I have probably got things wrong, hope it helps you out a bit though!
 
Old November 14th, 2009, 02:38 PM
Authorized User
 
Join Date: Nov 2009
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
Exclamation No LOGICAL oparators here!!!!

I wonder why you go to logic discussion????

& and | are considered to be logical operators when we have bool values,I mean 0 and 1 (false and true) or when we deal with conditions and comparisons that return true or false.

But here the first and third lines say that myColor is of type int and if | or & is applied to myColor,it is known as a number (like 0,1,5,100,...) to these operators.look at this part:

Code:
myColor = myColor | 2;
| doesn't seem to be a logical operator.Does it?
Because the value 2 is out of the range of a bool value so | is a bitwise operator here.

Till here,because we didn't have bool value, | was treated as bitwise operator.
but in action (during the computation of 0000 | 0010) it is a logical operator,because it's operands are 0 and 1.Look at this:

Code:
myColor = myColor | 2;// (===> 0000 | 0010 = 0010 = 2)
As you see | is logical operator here BUT it is none of our business.It is related to ALU part of CPU.Just get out of logic here.

Line 4 is analyzed as line 3 regardless of any logical operator but bitwise operator.After the execution of this line,myColor is assigned the value 6 (0010 | 0100 = 0110 = 6)

About the last line:

Code:
containsRed = (myColor & 4) == 4;
Again inside the parenthesis,& is a bitwise operator and after evaluation we have the value 4 inside parenthesis (0110 & 0100 = 0100 = 4).
The rest of the code is the comparison 4==4,which assigns the value true (or 1) to containsRed which is a variable of type bool.

Hope you got the point...

Erfan





Similar Threads
Thread Thread Starter Forum Replies Last Post
I don't understand for some contents... jay0715 BOOK: Ivor Horton's Beginning Visual C++ 2005 0 November 4th, 2007 07:39 AM
I don't understand... jmsherry ASP.NET 2.0 Basics 17 July 23rd, 2007 12:28 PM
I still do not understand aude_poullain ASP.NET 1.0 and 1.1 Basics 2 February 7th, 2007 09:11 PM
Who can understand this book? HandSomeLi All Other Wrox Books 2 October 20th, 2004 04:12 PM
i can't understand this error! ion Classic ASP Databases 2 September 23rd, 2003 03:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.