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

Post your completed addons to share with the community

Post » Mon Jan 02, 2012 4:44 pm

Hi guys

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

Code: Select all
12 AND 9 = 8
12 OR  9 = 13
12 XOR 9 = 5
NOT 9    = -10
12 <<  2 = 48
12 >>  2 = 3


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
https://developer.mozilla.org/en/JavaSc ... _Operators

The plugin is here:
Bit.zip


#### Conditions ####
Check
    - Check Mask
    - Is bit set to 1
    - Is power of 2

#### Actions ####
NONE

#### Expressions ####
Manipulation
    - set0
    - set1
    - toggle
One Operand
    - Not
Shifting
    - lShift
    - rShift
Two Operand (ok I forgot the '-s')
    - AND
    - OR
    - XOR

And that's all :D

feedbacks are taken into accound from 18h30 to 18h35 GMT+1 (gneheheh)

It was a joke �_�
Last edited by Yann on Sun Sep 14, 2014 10:21 pm, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Wed Jan 11, 2012 8:29 am

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 waysinofnature2012-01-11 08:32:35
B
2
G
1
Posts: 6
Reputation: 912

Post » Wed Jan 11, 2012 8:55 am

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

[code]function bitShift(a,b)
{
return a<<b;
}

bitShift(8,3);[/code]

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
[code]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);[/code]

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
http://dreaminginjavascript.wordpress.com/2009/02/09/bitwise-byte-foolish/
Yann2012-01-11 08:57:17
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Wed Jan 11, 2012 2:44 pm

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.

sinofnature2012-01-11 14:49:11
B
2
G
1
Posts: 6
Reputation: 912

Post » Wed Jan 09, 2013 4:14 am

@Yann

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

edittime.js
[code]AddNumberParam("a", "BitField","a");
AddNumberParam("b", "BitField","b");
AddExpression(0, ef_return_number | ef_variadic_parameters, "AND", "Two Operand", "AND", "Return a AND b.");[/code]

runtime.js
[code]     // 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
     };[/code]


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...rexrainbow2013-01-09 04:21:08
B
97
S
22
G
178
Posts: 4,121
Reputation: 104,049

Post » Thu Jan 10, 2013 1:56 pm

@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.Yann2013-01-10 13:56:58
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Thu Jan 10, 2013 3:31 pm

@Yann

uNOT(0)/sNOT(0) is great, although I thought all bit-wise are unsigned.
B
97
S
22
G
178
Posts: 4,121
Reputation: 104,049

Post » Thu Jan 10, 2013 3:39 pm

@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


B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Sat Jan 26, 2013 2:35 pm

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)

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


And I think left and right shift weren't working before. Now I'm sure they do (:Yann2013-01-26 14:37:15
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Sat Oct 31, 2015 5:12 pm

I added two new expressions to the mix
https://drive.google.com/open?id=0B05J8 ... zFGUTl6cjA
Casting:
IntToChar(i) //Casts utf-8 integer code to character
CharToInt(i) //Casts character to utf-8 integer code
B
35
S
16
G
16
Posts: 2,222
Reputation: 16,589

Next

Return to Completed Addons

Who is online

Users browsing this forum: No registered users and 2 guests