Page MenuHomeFeedback Tracker

Bitwise Operations
Acknowledged, WishlistPublic

Description

There is a lack of native bitwise operations within the scripting language, making these calculations can often become difficult and full of many unnecessary operations without native support from the engine. These operations are very simple and primitive to many programming languages and should be included in Arma.

Possible syntax:

  OR:  _z = _x | _y;
       if( surfaceIsWater position player ) then
           { _z = _z | 16 };              // Sets a flag

  AND: _z = _x & _y;
       if( _x & 32 > 0 ) then
           { /* blah */ };                // Tests a flag

  XOR: _msg = [];
       { _msg pushBack (_x xor _key) }    // ^ symbol is taken
           forEach toArray _str;          // Decrypt message
       hint toString _msg;

  NOT: if( player distance _object > 10 ) then
           { _x = _x & (~ 64) };          // Clears a flag

Thank you for reading

Details

Legacy ID
1166433879
Severity
None
Resolution
Open
Reproducibility
N/A
Category
Feature Request

Event Timeline

ProfessorK set Category to Feature Request.May 29 2015, 8:02 AM
ProfessorK set Reproducibility to N/A.
ProfessorK set Severity to None.
ProfessorK set Resolution to Open.
ProfessorK set Legacy ID to 1166433879.May 8 2016, 12:08 PM

We'd obviously need a true Integer type for that, which might not be so much trivial to implement in their engine now that they've been relying on Floats in all their SQF codebase/api for so many years.

Not at all. It would have the same simplicity as the "mod" function or the "+" operator. It would first check to make sure the type of parameters passed are scalar, most likely flooring the floats before performing the appropriate bit-wise operation and spitting the result back as a float.

Perhaps at its simplest, it would look something like this:

float ArmaScriptingEngine::XOR( float firstParameter, float secondParameter )
{

return (float)( (int)firstParameter ^ (int)secondParameter );

}
I have no clue what Arma's data structures look like so this is just an example of what it may look like:

ArmaObject* ArmaScriptingEngine::XOR( const ArmaObject& firstParameter, const ArmaObject& secondParameter )
{

    if( firstParameter.getType()  != ARMATYPE_SCALAR ||
        secondParameter.getType() != ARMATYPE_SCALAR )
    {
        // Do error stuffs
        return new ArmaScalar( 0.0f );
    }

    return new ArmaScalar( float(int( ArmaCast<ArmaScalar>(firstParameter).getValue() ) ^
        int( ArmaCast<ArmaScalar>(secondParameter).getValue() )) );

}

Of course you can work it that way. That for sure would still be way ahead than continuous floating point modulo 2 then divide by 2 :)

And +1 for that, that's better than nothing.

But the command should clearly state that you are doing your bitwise stuff on the integer conversion of the passed float argument (bitXorAsInt ? intXor ?). Cause who knows, someone might want to use bitwise operation directly on the float bits to retrieve the mantissa or whatever.

So in the end, I personally think that this language missing simple 32bit signed integer is awful, and can only lead to such weird situations + lot of useless performance and precision problem. But the sad thing is, I understand their initial choice for a single SCALAR type, and how it would be hard to introduce integers now given their desire for retro compatibility.

My conclusion : still +1 for your idea via cast, but with explicit indication of this casting via a wise operator name.