Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 12-04-2005, 08:31 PM   #1 (permalink)
 
Join Date: Jun 2003
Location: Poland, Zgierz
Age: 23
Posts: 173
Default arithmetics?

"&&" means AND, "||" means OR, bu I have no idea what does this code:

int var1 = var2 | var2;

and

int id = tiles[i].ID & 0x3FFF;

I hope it's not very dumb question :P
ultek is offline   Reply With Quote
Old 12-04-2005, 09:07 PM   #2 (permalink)
 
Join Date: Dec 2003
Location: Northwestern Pennsylvania
Posts: 183
Default

They are bitwise, boolean operators. | is OR, & is AND, ^ is XOR, and ~ is NOT. The examples given are therefore boolean (0s and 1s).

If A is 1001 and B is 1010:

A | B = 1011 (bits in either A or B are set)
A & B = 1000 (bits in both A and B are set)
A ^ B = 0011 (bits in either A or B, but not both, are set)

NOT works a bit differently in that it only uses one value and inverts the bits.

~A = 0110

The examples you gave:

int var1 = var2 | var2;
int id = tiles[i].ID & 0x3FFF;

The first one is a bit silly as it appears to be the same as int var1 = var2;

The second is masking. 0x3FFF is every bit set below 0x4000. Because values above 0x3FFF are used as flags, to get the actual ID from the tile the masking is applied. It removes all unwanted bits.

There is an article about these operators here:
http://en.wikipedia.org/wiki/Bitwise

And specific to C#, there is one here:
http://www.thecodeproject.com/csharp/MasksAndFlags.asp
a_dahaka is offline   Reply With Quote
Old 12-05-2005, 01:09 AM   #3 (permalink)
Moderate
 
David's Avatar
 
Join Date: Nov 2002
Location: USA
Posts: 6,598
Default

Additional information--

AND, OR, etc are either bit-wise or logical operators. Expressions that evaluate to true or false are evaluated on that basis. It is this use that you will see most often in scripting for RunUO for example.

"&&" is actually a "conditional AND" which only evaluates the second or subsequent operand if it is necessary.

In an AND operation both operands must be true in order for the result to be true. If the first operand evaluates to false the result must be false. A regular AND will continue to evaluate the second operand while a conditional AND will skip it since the result is already known.

The "conditional OR" or "||" is similiar except that it only evaluates the second operand if the first is false.

I belive (though I could be wrong here) that the conditional operators only perform the logical comparison. It seems to me that there would be no use for them in a bit-wise comparison since you would always want the second operand evaluated in that usage.
__________________
David Forum Moderator
The RunUO.com Forum Moderator Team

Forum Rules and Guidelines
RunUO Forum Search Engine
Download RunUO 2.0 RC2

Last edited by David; 12-05-2005 at 01:24 AM.
David is offline   Reply With Quote
Old 12-05-2005, 03:23 AM   #4 (permalink)
 
Join Date: Jun 2003
Location: Poland, Zgierz
Age: 23
Posts: 173
Default

Quote:
Originally Posted by ultek
int var1 = var2 | var2;
Ych.
I meann:

int var1 = var2 | var3;

but now I know how it is working. Thx for help
ultek is offline   Reply With Quote
Old 12-10-2005, 04:36 AM   #5 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Quote:
Originally Posted by David
Additional information--

AND, OR, etc are either bit-wise or logical operators. Expressions that evaluate to true or false are evaluated on that basis. It is this use that you will see most often in scripting for RunUO for example.

"&&" is actually a "conditional AND" which only evaluates the second or subsequent operand if it is necessary.

In an AND operation both operands must be true in order for the result to be true. If the first operand evaluates to false the result must be false. A regular AND will continue to evaluate the second operand while a conditional AND will skip it since the result is already known.

The "conditional OR" or "||" is similiar except that it only evaluates the second operand if the first is false.

I belive (though I could be wrong here) that the conditional operators only perform the logical comparison. It seems to me that there would be no use for them in a bit-wise comparison since you would always want the second operand evaluated in that usage.
Well, you wouldn't normally call && conditional "and" and || conditional "or", they're normally referred to as logical "and" and logical "or" respectively. The only operator that would be referred to using the word conditional, would be the conditional operator, ?:

The logical operators perform only a logical test on the operands after they are evaluated and do no type of bitwise operations, that is, they do only do a logical test of the operand to see if it's true or false, rather than bitwise ANDing or ORing a value with another. However they could of course be used to logically test the results of two bitwise operations.

Oh, and in case anyone is wondering, the concept of evaluating only the first operand if the first is false in a logical "and" or if the first is true in a logical "or" is called short-circuit, minimal or lazy evaluation.

Last edited by Sep102; 12-10-2005 at 04:40 AM.
Sep102 is offline   Reply With Quote
Old 12-10-2005, 02:02 PM   #6 (permalink)
Forum Expert
 
Courageous's Avatar
 
Join Date: Nov 2005
Location: San Diego, CA
Posts: 1,824
Default

Quote:
int var1 = var2 | var3;
Do you know what this is supposed to do, ultek?

What it does, in effect, is go one by one down each bit of both variables. If one bit is set in either variable, the bit will be set in the assignment. You have to comprehend binary here to know what is going on. Here is an example:

011100101000
001010100011
-------------
011110101011

If you don't get the binary, you will never get this; so if you don't get the binary, you need to learn binary.

C//
Courageous is offline   Reply With Quote
Old 12-12-2005, 09:46 PM   #7 (permalink)
Moderate
 
David's Avatar
 
Join Date: Nov 2002
Location: USA
Posts: 6,598
Default

Quote:
Originally Posted by Sep102
Well, you wouldn't normally call && conditional "and" and || conditional "or", they're normally referred to as logical "and" and logical "or" respectively. The only operator that would be referred to using the word conditional, would be the conditional operator, ?:

The logical operators perform only a logical test on the operands after they are evaluated and do no type of bitwise operations, that is, they do only do a logical test of the operand to see if it's true or false, rather than bitwise ANDing or ORing a value with another. However they could of course be used to logically test the results of two bitwise operations.

Oh, and in case anyone is wondering, the concept of evaluating only the first operand if the first is false in a logical "and" or if the first is true in a logical "or" is called short-circuit, minimal or lazy evaluation.
Actually if you look on msdn's C# language reference, you will see that a conditional-AND is precisely what && is called. (The ?: operator is called the conditional operator.) A regular & is discussed here and does a logical comparison if the operands are boolean, and a bitwise comparison if the operands are integral. The primary differance in the two is the short-circuit aspect of the conditional version which, in turn, makes it unusable for binary operations.

/edit: added clarification
__________________
David Forum Moderator
The RunUO.com Forum Moderator Team

Forum Rules and Guidelines
RunUO Forum Search Engine
Download RunUO 2.0 RC2

Last edited by David; 12-13-2005 at 12:49 AM.
David is offline   Reply With Quote
Old 12-13-2005, 02:22 AM   #8 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Hmm, guess that's why I'm a C++ developer and not a C# developer (well at least normally by choice, however actually I have aspirations at D development), MSDN's C++ Language Reference.

I think this adds a bit of weight to my assertion, however. Wikipedia search on conditional and and Wikipedia search on logical and. Note that I do know the logical and article is on conjunctions in logic, I just wished to point out the fact that logical and is the normal way to term a conjunction (essentially &&'s purpose) rather than conditional and. Also, I do not think I have ever heard another language than C# term its equivalent of && conditional and.

I don't particularly like the way C# handles the & (binary) operator, to tell you the truth. It (technically) silently changes its meaning when applied to objects of another type which unnerves me a bit.

However, good show, that did teach me something I did not know about the way C# handles bitwise and logical conjunctions and disjunctions which is always a good thing.

Last edited by Sep102; 12-13-2005 at 02:26 AM.
Sep102 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5