RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

arithmetics?

ultek

Wanderer
arithmetics?

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

int var1 = var2 | var2;

and

int id = tiles.ID & 0x3FFF;

I hope it's not very dumb question :p
 

a_dahaka

Wanderer
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.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
 

David

Moderate
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.
 

Sep102

Page
David said:
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.
 

Courageous

Wanderer
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//
 

David

Moderate
Sep102 said:
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
 

Sep102

Page
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.
 
Top