|
||
|
|
#2 (permalink) |
|
Join Date: Dec 2003
Location: Northwestern Pennsylvania
Posts: 183
|
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 |
|
|
|
|
|
#3 (permalink) |
|
Moderate
Join Date: Nov 2002
Location: USA
Posts: 6,598
|
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. |
|
|
|
|
|
#5 (permalink) | |
|
Forum Expert
|
Quote:
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. |
|
|
|
|
|
|
#6 (permalink) | |
|
Forum Expert
Join Date: Nov 2005
Location: San Diego, CA
Posts: 1,824
|
Quote:
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// |
|
|
|
|
|
|
#7 (permalink) | |
|
Moderate
Join Date: Nov 2002
Location: USA
Posts: 6,598
|
Quote:
/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. |
|
|
|
|
|
|
#8 (permalink) |
|
Forum Expert
|
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|