[PLUGIN] Bitwise Operation (update 2013-01-26)

  • Hi guys

    I made a little plugin a while ago to do bitwise operation like

    12 AND 9 = 8
    12 OR  9 = 13
    12 XOR 9 = 5
    NOT 9    = -10
    12 <<  2 = 48
    12 >>  2 = 3[/code:36oad0vt]
    And some condition to check if a number is power of 2 and if some bitfield match a mask. Stuff like that.
    if you have any question about this WTFness please, brace yourself and read that first
    [url=https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators]https://developer.mozilla.org/en/JavaSc ... _Operators[/url]
    The plugin is here:
    #### Conditions ####
        - Check Mask
        - Is bit set to 1
        - Is power of 2
    #### Actions ####
    #### Expressions ####
        - set0
        - set1
        - toggle
      One Operand
        - Not
        - lShift
        - rShift
      Two Operand (ok I forgot the '-s')
        - AND
        - OR
        - XOR
    And that's all <img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt=":D" title="Very Happy">
    feedbacks are taken into accound from 18h30 to 18h35 GMT+1 (gneheheh)
    It was a joke �_�
  • Bit-shifting in the old days was to increase speed dramatically. However, in Gamemaker I used bitshifting and to my horror found out it was pointless as it only stored numbers are real's (floating point), even integers. It ment bit-shifting was no advantage what so every as GM was just multiplying them in the background anyway (as you cant bitshift floats from memory... well, you probably can but the results will not be what you want).

    Do you know if Bitshifting will actually provide any speed increases in COnstruct 2 (ie. does it store integers or is everything floats?)

    Nice plugin by the way

  • My plugin only map the javascript bitshift function so it's as if you did

    function bitShift(a,b)
      return a<<b;

    Indeed it can be hard to find some use to bitshifting, but I use it for the isOne, set0 set1 and toggle bit function

    it does respectively

    isOne(value,pos) return return bitfield & (1 << position);
    set0(value,pos) return value | (1 << pos);
    set1(value,pos) return value & ~(1 << pos);
    toggle(value,pos) return value ^ (1 << pos);

    Now as I'm a mere javascript beginner, I can't really say if javascript does bitshifting the fast way.

    This guy seems to say that they are fast


  • That article is quite interesting. Was talking about high bit and low bit seperation to create a decimal point. Blimey, I rememeber doing that when I made a Doom/Wolfenstein engine MANY years ago using RayCasting. Floats where a big no-no back then and you used pure integers to create a fake float points for speed. :D

    Will certainly give this plugin a try and do some speed checks. <img src="smileys/smiley4.gif" border="0" align="middle" />

  • Yann

    1. expression:AND, OR could support more then 2 value AND/OR , for example: A & B & C


    AddNumberParam("a", "BitField","a");
    AddNumberParam("b", "BitField","b");
    AddExpression(0, ef_return_number | ef_variadic_parameters, "AND", "Two Operand", "AND", "Return a AND b.");


         // the example expression
         exps.AND = function (ret,a,b)     // 'ret' must always be the first parameter - always return the expression's result through it!
           ??var val = a & b;       ??
           ??if (arguments.length > 3)
                 var i, cnt=arguments.length;
                 for(i=3; i<cnt; i+=1)
                     val &= arguments[ i ];
              ret.set_int(val);                    // return our value

    2. Bit.NOT(0) will return -1 , not 1 , since the return value is sign.

    for example: 0 = 32bit 0 , NOT 0 will become 32bit 1 , in sign , it will be (-1).

    So it might be better make a Boolean NOT I guess...

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • rexrainbow

    Hey didn't know you could do that (:

    Ok I'll implement your multiple idea in the coming days =)

    As far as NOT(0) being -1

    I think, since my plugin handle signed integers, it would be weird to make an exception with 0.

    And it might go against some common bitwise operation.

    I would prefer making signed and unsigned functions, like uNOT(0)/sNOT(0).

    In short, I don't like when weird operations are hidden from the user. For the sake of an apparent "better" result.

  • Yann

    uNOT(0)/sNOT(0) is great, although I thought all bit-wise are unsigned.

  • rexrainbow

    Bitwise operations affect the bits themselves

    NOT(0000) = 1111

    but in signed int it's interpreted as -1

    and in unsigned int it's interpreted as 15

  • Alright I updated the code with rex's little snippet and also I changed it a bit so now you can use hexadecimal value (inside strings)


    Bit.AND("0xf","0x6","0xa") //returns 2
    Bit.OR("0x3","0x1","0x1","0x4") //returns 7
    Bit.XOR("0xa","0xc","0x9","0xf","0x9") //returns 9

    And I think left and right shift weren't working before. Now I'm sure they do (:

  • I added two new expressions to the mix

    https://drive.google.com/open?id=0B05J8 ... zFGUTl6cjA


    IntToChar(i) //Casts utf-8 integer code to character

    CharToInt(i) //Casts character to utf-8 integer code

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)