• Welcome, Guest. Please login.
 
March 29, 2024, 04:56:02 AM

News:

Visit our IRC channel, #TrinityMUGEN on irc.sorcery.net!


Bitwise shifting: Demystified

Started by Jesuszilla, April 18, 2013, 02:53:23 PM

Previous topic - Next topic

Jesuszilla

April 18, 2013, 02:53:23 PM Last Edit: April 18, 2013, 02:55:35 PM by Jesuszilla
So I'm sure some of you have seen an old thread by Winane (most likely on Guild) about bitwise shifting. If you were anything like me, you would have looked at it and gone, "what the fuck is this," and walked out. Rightfully so, I might add. This wasn't explained in the thread very well at all, so I decided to take it upon myself to do so.

To understand bitwise shifting, you must understand binary notation.



What the hell is binary notation?
Binary notation is a way of breaking down numbers into 0s and 1s. False or true. Off or on. Everything going on in your computer right now, when you get down to the most basic things, is nothing more than a series of switches that say "on" or "off." 1 for on, 0 for off.

An example of binary notation:

1001

That is 9 in binary notation. Why is this? Let's break this down into simpler terms (spaces are there for the sake of readability)

1 0 0 1
8 4 2 1

8 + 0 + 0 + 1 = 9

So, 16 in binary would be:
10000

So, 32 in binary would be what?

[spoiler]If you answered 100000, you'd be correct![/spoiler]

More at Wikipedia.

Now, if you haven't noticed already: powers of two will never cross over. This is the key to understanding...



What the hell is bitwise shifting?

In MUGEN, we are given two sets of variables to use: ints and floats. However, we are limited to 60 int variables (indices 0-59), and 40 float variables (indices 0-39). Unfortunately, this means we end up wasting a lot of those variables for simple things that we want to either be 0 or 1.... hmmmmm!

Well, we could just relocate a lot of those to just one variable! If it's just a 0 or 1, then it's a boolean. It's on or off. We can use bitwise shifting on ONE variable to handle up to 30 booleans! Wow!

So how do I do it?
There are a number of ways. If you know your powers of two, you should be fine with:
[State -2, Powers of Two]
type = VarAdd
trigger1 = !(Var(4)&1)
var(4) = 1
[State -2, Powers of Two]
type = VarAdd
trigger1 = !(Var(4)&2)
var(4) = 2
[State -2, Powers of Two]
type = VarAdd
trigger1 = !(Var(4)&4)
var(4) = 4

[State -2, Test]
type = PlaySnd
trigger1 = (Var(4)&2)>0
value = S11,0


That is, if you are going to make use of ALL 30 slots, you can store up to var(x)&1073741824.

Alternative notation:
[State -2, Exponentiation]
type = VarAdd
trigger1 = !(Var(4)&2**0)
var(4) = 2**0
[State -2, Exponentiation]
type = VarAdd
trigger1 = !(Var(4)&2**1)
var(4) = 2**1
[State -2, Exponentiation]
type = VarAdd
trigger1 = !(Var(4)&2**2)
var(4) = 2**2

[State -2, Test]
type = PlaySnd
trigger1 = (Var(4)&2**1)>0
value = S11,0


Example of how to use this with the assignment operator:
;---------------------
; N-Groove
[State -2, VarSet]
type = Null
triggerall =  Var(20) = 5 && (RoundState = [1,2])
trigger1 = e|| (var(5) :=   (var(5)|2*1)) ; 0 = Dash, 1 = Run
trigger1 = e|| (var(5) :=   (var(5)|4*0)) ; Just Defend
trigger1 = e|| (var(5) :=   (var(5)|8*0)) ; Parry
trigger1 = e|| (var(5) :=  (var(5)|16*0)) ; Super Combo Cancel
trigger1 = e|| (var(5) :=  (var(5)|32*1)) ; Counter Attack
trigger1 = e|| (var(5) :=  (var(5)|64*1)) ; Counter Movement
trigger1 = e|| (var(5) := (var(5)|128*1)) ; Safe Fall
trigger1 = e|| (var(5) := (var(5)|256*0)) ; Air Guard
trigger1 = e|| (var(6) := 1)              ; Small Jump
trigger1 = e|| (var(9) := 1)              ; 1 = Roll, 2 = Dodge, 3 = Neither
trigger1 = e||(var(10) := 5)              ; 1 = C-Gauge, 2 = A-Gauge, 3 = P-Gauge
                                          ; 4 = S-Gauge, 5 = N-Gauge, 6 = K-Gauge
persistent = 1




How the hell does it work?
Recall once again how binary notation is.

0001 = 1
0010 = 2
0100 = 4
1000 = 8

The way this works is how I said at the end of the first section: powers of two never cross over! Simple as that!


Hope this helps a lot of you out!


Just try to keep things peaceful.