YesNoOk
avatar

Bitwise shifting & Powers of two (Read 3033 times)

Started by Winane, April 19, 2005, 01:23:42 pm
Share this topic:
Bitwise shifting & Powers of two
#1  April 19, 2005, 01:23:42 pm
  • *****
  • Tends to lose track of things a lot. :/
    • www.mugenguild.com/~winane/
This is all probably painfully obvious to anyone who'd actually be wanting to use it, but I decided to post it anyway.  =P


I just noticed today that Elecbyte neglected to include shift operators for Mugen.  But instead, you can use the following code to implement bitwise logical shifting in Mugen, with zero-filling of vacated positions:


To implement var(x) >> 1:

var(x) := (var(x)&2147483647)/2 + 1073741824*(var(x)<0)
or
var(x) := (var(x)&2147483647)/2 + IfElse(var(x)<0,1073741824,0)



To implement var(x) << 1:

var(x) := (var(x)&2147483647)*2




To implement var(x) >> var(y), for var(y) = [9,22]  (or for var(y) = [0,22] && var(x) >= 0) :

var(x) := (var(x)&2147483647)/2**var(y) | 2**(31-var(y))*(var(x)<0)
or
var(x) := (var(x)&2147483647)/2**var(y) | IfElse(var(x)<0,2**(31-var(y)),0)



To implement var(x) << var(y), for var(y) = [0,22]:

var(x) := (var(x)&2147483647)*2**var(y)


Or, to remove the restrictions on the values of var(y), you can use the code in the next post below to obtain "2**var(y)" and "2**(31-var(y))".  But as in C, the results will still be undefined for var(y)<0.  (Note that the var names in the next post are completely independent of the var names in this post.)
Still quite busy.

(Yes, I intend to deal with that stuff eventually, but kinda can't just yet, sorry. :/ )
Last Edit: April 19, 2005, 01:48:12 pm by Horseradish Booty
Re: Bitwise shifting & Powers of two
New #2  April 19, 2005, 01:24:17 pm
  • *****
  • Tends to lose track of things a lot. :/
    • www.mugenguild.com/~winane/
The ** operator in Mugen is broken, and, for example, won't return the correct results of 2**x for any x > 22.  So, if you want to obtain the correct value in such cases, you need to use a different method.  So here's a simple method that will give the correct value of 2^x for any x=[0,30], -2^31 for x=31 (which isn't arithmetically accurate, but is useful for bitwise operations), and 0 for x>31.  It uses var(z) as the exponent, uses var(y) as a temporary counter variable, and stores the final result in var(x).  Probably not the most compact or efficient method, but it'll do.

Code:
[State #, var(x):=2**var(z)]
type = Null
triggerall = whatever
triggerall = var(z) >= 0
triggerall = var(y):=var(z) || 1
trigger1 = var(x):=1 && !var(y)
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1
trigger2 = var(x):=2*var(x) && var(y):=var(y)-1; will always set var(x) to 0 if reached.



So, did I make any errors in either post, as I usually do?
Still quite busy.

(Yes, I intend to deal with that stuff eventually, but kinda can't just yet, sorry. :/ )
Last Edit: April 24, 2005, 03:50:12 am by Horseradish Booty

Bea

Re: Bitwise shifting & Powers of two
#3  April 19, 2005, 02:10:44 pm
  • *****
  • MUGEN Grandma
    • Brazil
    • www.smeenet.org
Not really. Everything seems fine except for assuming that 2**22 will work on every mugen system. :)
Mine gets broken at 2**21, for one. :)

And lovely way you did to get power(2,x).
Princess Adora: "My friend saw She-Ra take her dress off in the shower. She said she has an 8 pack. She said She-Ra is shredded."

SF2NES is dead. Long live SF2NES.

Simple Machines

Re: Bitwise shifting & Powers of two
#4  April 19, 2005, 04:11:01 pm
I distinctly remember 2**x not returning the correct results for numbers before x > 22 (because I asked for SMEE's help on this one).  It is correct for certain values (in the latest DOS Mugen, I don't know what you used as your test case).  I think x =3 or 4 is the first incorrect value.