Jump to content

Trying to simplify some settings.


Recommended Posts

Hello all,

It is my understanding that bit shifting can be faster (and more memory efficient) than having an array of booleans. So, I'm curious how I can duplicate the following C++ code in Au3:

#define IS_SET(flag, bit)   ((flag) & (bit))
#define SET_BIT(var, bit)   ((var) |= (bit))
#define REMOVE_BIT(var, bit)    ((var) &= ~(bit))
#define TOGGLE_BIT(var, bit)    ((var) ^= (bit))

#define SPELL_HASTE     =   1
#define SPELL_SLEEP     =   2
#define SPELL_BLIND     =   4
#define SPELL_STRENGTH      =   8
//Ect...

Usage:

int SpellsAffecting = 0;

if (IS_SET(SpellsAffecting,SPELL_HASTE))
{
    REMOVE_BIT(SpellsAffecting,SPELL_HASTE);
}
else
{
    SET_BIT(SpellsAffecting,SPELL_HASTE);
}

//Or an easier method:
TOGGLE_BIT(SpellsAffecting,SPELL_HASTE);

Since Au3 is designed in C++, do the same operators apply? (&,^,|,&=,|=,^=, and ~)

Just trying to eliminate an array of booleans for the sake of sanity and confusion. One concern I have is what would happen in the C++ code if I set the same bit more than once? Would the same thing happen in Au3 aswell?

Thanx,

CMR

Link to comment
Share on other sites

Ok, I've made some progress, just need some help on one function (or understanding of bitwise operation).

;Works:
Func _IS_SET($bt_Bit,$bt_Value)
    Return BitAND($bt_Bit,$bt_Value)
EndFunc

Func _SET_BIT(ByRef $bt_Bit,$bt_Value)
    $bt_Bit = BitOR($bt_Bit,$bt_Value)
EndFunc

Func _TOGGLE_BIT(ByRef $bt_Bit,$bt_Value)
    $bt_Bit = BitXOR($bt_Bit,$bt_Value)
EndFunc

;Does not work:
Func _REMOVE_BIT(ByRef $bt_Bit,$bt_Value)
    $bt_Bit = BitAND($bt_Bit,$bt_Value)
EndFunc
oÝ÷ Ùh^¬y9Q!2(êv·¬º[ljëh×6
Global $My_Bits = 0

_SET_BIT($My_Bits,1)
_SET_BIT($My_Bits,2)
_SET_BIT($My_Bits,4)
_SET_BIT($My_Bits,8)
_SET_BIT($My_Bits,16)
;All bits are set... so now lets try to remove bit 8...

_REMOVE_BIT($My_Bits,8)
;Mkay... odd, now $My_Bits == 8.... So everything was removed EXCEPT 8...


$My_Bits = 0

_SET_BIT($My_Bits,4)
;Only 4 is set right now... let's try and remove 8 for the heck of it...

_REMOVE_BIT($My_Bits,8)
;Great, now $My_Bits == 0......

Any idea what I'm doing wrong?

-CMR

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...