Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > New Scripting Engine (GS2)
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11-05-2006, 05:41 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
operators

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

HTML Code:
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 "=="?
__________________
Reply With Quote
  #2  
Old 11-05-2006, 05:59 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by Chompy View Post
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).
Reply With Quote
  #3  
Old 11-05-2006, 06:07 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
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.
Reply With Quote
  #4  
Old 11-05-2006, 06:38 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by Skyld View Post
= 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
PHP Code:
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

Quote:
Originally Posted by Stefan View Post
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
__________________
Reply With Quote
  #5  
Old 11-05-2006, 06:45 PM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Quote:
Originally Posted by Chompy View Post
hmm, so
PHP Code:
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.
Reply With Quote
  #6  
Old 11-05-2006, 06:48 PM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Quote:
Originally Posted by Chompy View Post
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.

Quote:
Originally Posted by Chompy
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.
Reply With Quote
  #7  
Old 11-05-2006, 06:51 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
darn, I forgot to mention,
I wanted to know the different beetween doing
that in an statement, like
PHP Code:
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..
__________________
Reply With Quote
  #8  
Old 11-05-2006, 07:02 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Quote:
Originally Posted by Chompy View Post
darn, I forgot to mention,
I wanted to know the different beetween doing
that in an statement, like
PHP Code:
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...ad.php?t=66825

Basically, (foo = bar) is allowed for people who don't know better at the expense of others.
Reply With Quote
  #9  
Old 11-05-2006, 07:09 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Thanks Novo

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
__________________
Reply With Quote
  #10  
Old 11-05-2006, 07:13 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
xor is EXCLUSIVELY OR...

PHP Code:
if ( var1 != var2 && (var1 || var2 ) ) 
is the same as

PHP Code:
if ( var1 xor var2 
If only one of both vars is true, it returns true.
Reply With Quote
  #11  
Old 11-05-2006, 07:30 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
I tested the xor stuff, and I did this:

PHP Code:
function onCreated()
{
  
var1 1;
  
var2 2;
  echo((
var1 xor var2 "true" "false"));
}
// would return true 
and
PHP Code:
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?
__________________
Reply With Quote
  #12  
Old 11-05-2006, 07:36 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
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:
PHP Code:
echo( var1 xor var2 ); // returns 0 or 1 
Reply With Quote
  #13  
Old 11-05-2006, 08:11 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
weird..

Quote:
Originally Posted by Novo View Post
But... I think you found a bug?
I did?
__________________
Reply With Quote
  #14  
Old 11-05-2006, 10:49 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Uhhhh xor does not act like you think it does.

PHP Code:
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:
<<
PHP Code:
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. 
>>
PHP Code:
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. 
~
PHP Code:
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.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/

Last edited by Tolnaftate2004; 11-05-2006 at 11:10 PM..
Reply With Quote
  #15  
Old 11-05-2006, 10:53 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Quote:
Originally Posted by Tolnaftate2004 View Post
Uhhhh xor does not act like you think it does.

PHP Code:
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.
Reply With Quote
  #16  
Old 11-05-2006, 11:01 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
In addition, ...
| (or)
PHP Code:
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)
PHP Code:
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. 
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/

Last edited by Tolnaftate2004; 11-06-2006 at 08:09 PM..
Reply With Quote
  #17  
Old 11-06-2006, 03:55 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
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
__________________
Reply With Quote
  #18  
Old 11-06-2006, 04:17 PM
contiga contiga is offline
Graal2001 Administration
contiga's Avatar
Join Date: Jul 2004
Location: Netherlands
Posts: 419
contiga is an unknown quantity at this point
Send a message via ICQ to contiga Send a message via AIM to contiga Send a message via MSN to contiga Send a message via Yahoo to contiga
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.
__________________
AIM: Contiga122
MSN: [email protected]
Status:
Quote:
Originally Posted by unixmad View Post
I am also awake 3AM to help correct problems.
Quote:
Originally Posted by Bomy Island RC people
Daniel: HoudiniMan is a bad guy =p
*Bell: rofl. I first read that as houdini is a bad man. like the little kid that wants his mommy to keep her away from that boogie man
Daniel: xD
*Rufus: I wouldn't want my kids around him.
Reply With Quote
  #19  
Old 11-06-2006, 04:22 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
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
__________________
Reply With Quote
  #20  
Old 11-06-2006, 08:05 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
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.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
Reply


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

Forum Jump


All times are GMT +2. The time now is 01:20 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.