Suggestion: XOR operator

Started by Ytterbium, Wed 28/01/2004 16:40:02

Previous topic - Next topic

Ytterbium

I've noticed the Or (||) operator will trigger an action in an if statement if one or both are true. I think an XOR operator, which would trigger the if statement if one but not both are true, would be very useful in scripting.

Currently in production: Septigon

Proskrito

but, you can do that easily with the actual && and || operators, cant you?... ::)
like  if ((a==1 && b==0) || (a==0 && b==1))...

SSH

Easier than that: a logical XOR is the != operator...

0 != 0 gives 0
0 != 1 gives 1
1 != 0 gives 1
1 != 1 gives 0

of course, each side must be 0 or 1 for this to work.

It might be nice to have bitwise operators sometimes, though like &, | and ^
12

Scorpiorus

#3
QuoteIt might be nice to have bitwise operators sometimes, though like &, | and ^
There are bitwise AND and OR already, but still missing the NOT and XOR ones. :P

a-v-o

For NOT you can use a simple subtraction and with it you can build a xor function:

Code: ags

// for 16 bit integers
#define MAXINT   2147483647

function not (int a)
{
  return MAXINT - a;
}

function xor (int a, int b)
{
  return (not ((a & b) | (not (a) & not (b))));
}




Pumaman

Adding missing operators MOD, NOT and XOR is on my to-do list. Are there any other common operators that the script is missing, while we're at it?

Scorpiorus

Quote from: a-v-o on Thu 29/01/2004 19:04:24
For NOT you can use a simple subtraction and with it you can build a xor function
thanks for the quick tip :)

Quote from: Pumaman on Sat 31/01/2004 14:23:33Are there any other common operators that the script is missing, while we're at it?
left/right shifting operators could come in handy provided it's not some hassle to implement those of course.

a-v-o

shift 1 bit left: *2
shift 1 bit right: /2

Scorpiorus

Yep, that's how I've done it so far. However, it's an arithmetical shift and I'd also like to see a logical shift (right direction with leading 0s) too. Again it can be workarounded but anyway those I'd like to suggest to be built in AGS. Would be cool to have them as operators with proper type conversion (again if it's not too much work with script engine-related code).

~Cheers

SMF spam blocked by CleanTalk