View Full Version : operators

Chompy

11-05-2006, 05:41 PM

How do this operators work? I've seen them around, and I've wondered how they work

Bitwise-Shift left a << b

Bitwise-Shift right a >> b

Bitwise-Invert ~a

Bitwise-Xor a xor b (operator ^ already used for power)

Power Assignment a ^= b

Shift Left Assignment a <<= b

Shift Right Assignment a >>= b

(Listed on the Wiki)

That are some of the operators I don't seem to understand,

anyone can help me with those?

AND, what is the fully different beetween "=" and "=="?

Skyld

11-05-2006, 05:59 PM

what is the fully different beetween "=" and "=="?

= is for assignment (in other words, for setting a variable). == is for comparison (so it doesn't do any setting, just checking for equality).

Admins

11-05-2006, 06:07 PM

Shift, Invert and Xor are bit-operators. Graal handles all numbers internally as floating point values, but those operators are still interesting for some purposes. Integer numbers can be represented as fields of 32 bits, the first bit stands for 1, the second for 2, the third for 4, the fourth for 8 etc. Example: 10 is 8 + 2, so the bit representation is 1010 (we start reading from the right, the fields stand for 8421). Invert is inverting the bits, shift is moving the bits to the right or to the left (which is basicly multiplying with 2 or dividing by 2), and xor is combining two bit fields and the bits in the result are set when the bits of the two operands are unequal.

Chompy

11-05-2006, 06:38 PM

= is for assignment (in other words, for setting a variable). == is for comparison (so it doesn't do any setting, just checking for equality).

hmm, so

if (foo = bar) {

would assign bar too foo?

if (foo == bar) {

would be to check if foo is equal to bar?

think I got it, if that was that I just understood now :)

Shift, Invert and Xor are bit-operators. Graal handles all numbers internally as floating point values, but those operators are still interesting for some purposes. Integer numbers can be represented as fields of 32 bits, the first bit stands for 1, the second for 2, the third for 4, the fourth for 8 etc. Example: 10 is 8 + 2, so the bit representation is 1010 (we start reading from the right, the fields stand for 8421). Invert is inverting the bits, shift is moving the bits to the right or to the left (which is basicly multiplying with 2 or dividing by 2), and xor is combining two bit fields and the bits in the result are set when the bits of the two operands are unequal.

Hmm, I didn't understand fully all of that, but do you think

you can show some examples on how they work?

I would love to know those bit-operators, and how to use them

properly in a script ^^

Gambet

11-05-2006, 06:45 PM

hmm, so

if (foo = bar) {

would assign bar too foo?

if (foo == bar) {

would be to check if foo is equal to bar?

think I got it, if that was that I just understood now :)

No.

foo = bar; would assign the value of bar to foo.

You can't assign a value to something in a condition statement.

Gambet

11-05-2006, 06:48 PM

hmm, so

[PHP]

if (foo = bar) {

would assign bar too foo?

think I got it, if that was that I just understood now :)

No.

foo = bar; would assign the value of bar to foo.

You can't assign a value to something in a condition statement.

Hmm, I didn't understand fully all of that, but do you think

you can show some examples on how they work?

I would love to know those bit-operators, and how to use them

properly in a script ^^

You don't understand it because you havn't learned about it in school yet. Operators are mathematical functions, learned through math courses. Stefan didn't think these up himself, he integrated mathematical functions into GScript.

Chompy

11-05-2006, 06:51 PM

darn, I forgot to mention,

I wanted to know the different beetween doing

that in an statement, like

if (foo = bar) {

instead of

if (foo == bar) {

I have been wondering about that in a while, since I couldn't find it out,

so I decided to post a thread about it, but oh well..

darn, I forgot to mention,

I wanted to know the different beetween doing

that in an statement, like

if (foo = bar) {

instead of

if (foo == bar) {

I have been wondering about that in a while, since I couldn't find it out,

so I decided to post a thread about it, but oh well..

Technically speaking, there are NO difference.

See:

http://forums.graalonline.com/forums/showthread.php?t=66825

Basically, (foo = bar) is allowed for people who don't know better at the expense of others. :(

Chompy

11-05-2006, 07:09 PM

Thanks Novo :D

Now I just wonder how xor etc.. (first post)

works in a script,

like I used ages on figure out the a? b : c stuff untill

someone said: condition ? true : false, then I understood it

I would like to know a explanation on xor and those (again, first post)

and again, Thanks Novo :D

xor is EXCLUSIVELY OR...

if ( var1 != var2 && (var1 || var2 ) )

is the same as

if ( var1 xor var2 )

If only one of both vars is true, it returns true.

Chompy

11-05-2006, 07:30 PM

I tested the xor stuff, and I did this:

function onCreated()

{

var1 = 1;

var2 = 2;

echo((var1 xor var2 ? "true" : "false"));

}

// would return true

and

function onCreated()

{

var1 = 1;

var2 = 1;

echo((var1 xor var2 ? "true" : "false"));

}

// would return false

is this one example of how to use it?

by this it looks like it returns false if they are not the same,

and false if they are equal, so..?

if this is not right, got an example?

var1 != var2

1 != 2... So... That would be true. The other part... (var1 || var2) means true || true => true.

It works wonders on TRUE and FALSE... But... I think you found a bug?

Because, if you check it out... Everything that is not false (0) is true... So it should have both been false ( Theoretically )

But... The problem with my script ( and that of graal ) is it doesn't evaluate var1 and var2 as such.

(!var1) != (!var2) should be the RIGHT version... Hrm.

As for the echo part... I'd just do:

echo( var1 xor var2 ); // returns 0 or 1

Chompy

11-05-2006, 08:11 PM

weird..

But... I think you found a bug?

I did? :confused:

Tolnaftate2004

11-05-2006, 10:49 PM

Uhhhh xor does not act like you think it does.

var_a = 1; // or in binary, 000......0001

var_b = 2; // or in binary, 000......0010

var_c = var_a xor var_b; // only where one of the bits from either variable's binary values is 1, so we get 000.......0011, or 3.

And for the others:

<<

var_a = 3; // or in binary, 000......0011

var_b = 2;

var_c = var_a << var_b; // shifting the binary left twice to yield 000.......1100, or 12.

>>

var_a = 6; // or in binary, 000......0110

var_b = 2;

var_c = var_a >> var_b; // shifting the binary right twice to yield 000.......0001, or 1.

~

var_a = 0; // or in binary, 000.....0000

var_b = ~var_a; // yields binary 111.....1111, or -1.

This is not necessarily true for unsigned long integers, but I don't think Graal deals with them, so.... yeah. But if we did, ~0 would be 4294967295.

Uhhhh xor does not act like you think it does.

var_a = 1; // or in binary, 000......0001

var_b = 2; // or in binary, 000......0010

var_c = var_a xor var_b; // only where one of the bits from either variable's binary values is 1, so we get 000.......0011, or 3.

Ah. :) On the binary level.

Tolnaftate2004

11-05-2006, 11:01 PM

In addition, ...

| (or)

var_a = 7; // or in binary, 000....0111

var_b = 14; // or in binary, 000....1110

// in the bit in each position becomes 1 if either bits from either variable are 1.

var_c = var_a | var_b; // which yields 000....1111, or 15.

& (and)

var_a = 7; // or in binary, 000....0111

var_b = 14; // or in binary, 000....1110

// in the bit in each position becomes 1 if both bits from either variable are 1.

var_c = var_a & var_b; // which yields 000....0110, or 6.

Chompy

11-06-2006, 03:55 PM

Nice, I think I will experiment with those,

I did understand it, but I don't know how you can know what a binary is,

like you said

000.......0001 = 1

000.......0010 = 2

000.......0110 = 6

etc.. how do you know that? :O

is there an rule or something?

Well, thanks Tolnaftate :D

contiga

11-06-2006, 04:17 PM

Yes there's a rule..

It checks like true / false:

32,16,8,4,2,1

Lets say you have the line; 000110

That's like;

0,0,0,1,1,0

The 4 is true, and the 2 is true, you count all the "true" things.. which makes 6.

Chompy

11-06-2006, 04:22 PM

hmm,

so like

25, 20, 15, 10, 5

into

0, 0, 1, 1, 1

that would be 30?

but how is that if you only got one number?

btw, Thanks Contiga :D

Tolnaftate2004

11-06-2006, 08:05 PM

There are 32 bits.

I usually read binary backwards. The bit on the end is 1 (2^0). The second to last is 2 (2^1). The one before that is 4 (2^2). Then 8 (2^3), 16 (2^4), 32 (2^5), ... etc. You multiply the bit (0 or 1) by each of these numbers then add them up. The ones where the bit is 0 will just be 0, so they don't change the number.

000........1010

.............^.^

.............3..1

2^3 + 2^1 = 10, thus it represents 10.

vBulletin® v3.8.3, Copyright ©2000-2019, Jelsoft Enterprises Ltd.