Feature - Implemented bitwise operators

Bale

Minion
While bitwise operators are not important to me, if they existed I expect that I'd use them.

They're both fun and nice for simultaneously making things compact and legible.
 
Last edited:

Winterbay

Active member
While bitwise operators are not important to me, if they existed I expect that I'd use them.

They're both fun and nice for simultaneously making things compact and legible.

The legibility would depend on the person being familiar with bitwise operators though :)
Maps are readable quite easily after some time of looking at code, while I feel that bitwise operators are completely confusing the heck out of me when I look at them...
 

Bale

Minion
Well, the code is pretty cool if it is written like:

PHP:
int all_prefs = get_property("ThisScriptPrefs").to_int();
int drink_milk = 2;
int use_ode = 4;
int eat_cookie = 8;

boolean permission(int check_this) {
   return to_boolean(all_prefs & check_this)
}

if(permission(eat_cookie)) {
   if(permission(drink_milk))
      use(1, $item[milk of magnesium]);
   eat(1, $item[fortune cookie]);
}

That's kinda fun. That's what coaster3000 was awkwardly trying to explain a few posts back. It's a way to put a LOT of information into a single property in mafia's preference file. Compact, while still being good code when done right.
 
Last edited:

Veracity

Developer
Staff member
Code:
int all_prefs = get_property("ThisScriptsPrefs").to_int(); 
int drink_milk = 2; 
int use_ode = 4; 
int eat_cookie = 8; 

boolean permission(int check_this)
{
   // returns true if ALL the bits in the bit mask are set
   return (all_prefs & check_this) == check_this;
} 

if ( permission(eat_cookie | drink_milk) ) 
   use( 1, $item[milk of magnesium] );

...which is no longer quite the same as what you edited your example into. :p
 

Bale

Minion
Ah yes. Thanks for fixing my permissions() function. You can see how long it has been since I used bitwise operators. :D
 

Paragon

Member
So... we do have bitwise ops now or we don't have bitwise ops? I tried using them, couldn't get them to work, looked it up on the wiki to see if they were implemented, found this thread on the forum and... the permissions func doesn't work, parsing error... so we don't have these? Personally I would appreciate it if we did.

Here is a crude workaround that will at least enable bitfields. (bnot doesn't work exactly like a binary not because it doesn't fill all the bits, it only reverses the bits from the highest used power of two and below.)

int band(int a, int b)
{
int ret=0;
int c=0;
int Da=0;
int Db=0;
a=2*a;
b=2*b;
while(a>0 && b>0)
{
a=a/2;
b=b/2;
Da=a % 2;
Db=b % 2;
if (Da+Db==2) ret=ret+2^c;
c=c+1;
a=a-Da;
b=b-Db;
}

return ret;
}
int bnot(int a)
{
int ret=0;
int c=0;
int Da=0;
a=2*a;
while (a>0)
{
a=a/2;
Da=a%2;
if (Da==0) ret=ret+2^c;
a=a-Da;
}
return ret;
}
int bor(int a, int b)
{
int ret=0;
int c=0;
int Da=0;
int Db=0;
a=2*a;
b=2*b;
while (a>0 && b>0)
{
a=a/2;
b=b/2;
Da=a%2;
Db=b%2;
if (Da+Db>0) ret=ret+2^c;
c=c+1;
a=a-Da;
b=b-Db;
}
if (b>a) a=b;
while (a>0)
{
a=a/2;
Da=a%2;
if (Da==1) ret=ret+2^c;
c=c+1;
a=a-Da;
}
return ret;
}
 

Theraze

Active member
Nope. Nobody who does the code stuff cared enough to do it, and the sales pitch was insufficient to generate interest.
 

StDoodle

Minion
By the way, I still blame you for the situation I was in a week or so ago; trying to use "&=" in a script, banging my head against the desk trying to figure out why it didn't work... :p
 

Theraze

Active member
Sorry... I should have said:
Not in the base code. Nobody who does the code stuff cared enough to do it, and the sales pitch was insufficient to generate interest.

However, there's an ash script you can use that will add it in.
 

Veracity

Developer
Staff member
This caught my eye and this time I thought I'd see what I could do. Revision 8898 says this:

Add some arithmetic bitwise operators:

a & b --> and
a | b --> or
~a -> not
a << b -> left shift
a >> b -> right shift
a &= b --> a = a & b
a |= b --> a = a | b

In all of the above, the operands must be integers; no coercion is allowed.
I am annoyed that the "^" operator already exists in ash to mean power. Does anybody really use it? I'd like to make power be "**", or, perhaps a function and use "^" for bitwise exclusive or. I'd also add "^=" in that case.

I could add "<<=" and ">>=" right now. I suppose I should...

Anyway, try it out and tell me what you think.
 

StDoodle

Minion
Yeah, there are a couple of people, including me, who have used it. Stupid libram formula. If you could add a new function that raises to exponents I'd be happy to replace the rare use of ^ as an operator with said function.
 

Theraze

Active member
Currently, in my script folder, the following scripts use ^ for exponents, not matchers...
autoBasement
familiartonext
get_skill
zlib
 

Theraze

Active member
The rnum function uses it twice in the same line. It's also described as being the exponentation variable modifier in there.
 

Fluxxdog

Active member
Considering KoLmafia is Java-based, I would suggest going with what Java uses the caret for. Of course that'd mean XOR. Honestly, I don't like that way ** looks. It's more a typo. Is that used anywhere else? Is there any other common way to denote raising to a power? Everywhere I've looked uses either x^y or x(superscript)y.

Perhaps a function would be the best route in that case. power(3,5) = 3 to the fifth power
 
Top