Bit Values compared to Property Flags

M

Mythran

In C/C++ for MUDs that I used to work on, I'd use the following defines:

#define IS_SET(flag,bit) ((flag) & (bit))
#define PLR_ISALIVE (1 << 0)
#define PLR_THIEF (1 << 1)
#define PLR_LOADFIRST (1 << 2)

To see if a player "is alive", we'd check the PLR_ISALIVE bit:

IS_SET(flag, PLR_ISALIVE)

where flag is the number of the combined flags (IE: flag |= PLR_ISALIVE; flag |=
PLR_THIEF;).

Now in VB.Net, a Player Class could be made that simulates the above using
bitwise operaters....I'm wondering what the cost would be to use properties
instead:

Class Player
' Private Members
...
' Properties
Public Property IsAlive() As Boolean
Get
Return mIsAlive
End Get
Set
mIsAlive = Value
End Set
End Property

' et cetera
End Class

Is there a significant difference? Lets say there "could" be roughly 100-200 of
these properties...this exceeds the limitations of the bitwise comparisons using
the old method in C/C++ (could only use up to 32 bits). So, that advantage for
using Class properties instead is already visible. Others would be
maintainability, easier reading and understanding.

So, cost performance. What type of gains or otherwise would I get when using
properties vs bitwise storage (IE: Memory consumption, processor usage, et
cetera)?

Please don't go out of yer way to test, just need something off top of yer heads
:)

THanks!~

Mythran
 
L

Larry Serflaten

Mythran said:
Is there a significant difference? Lets say there "could" be roughly 100-200 of
these properties...this exceeds the limitations of the bitwise comparisons using
the old method in C/C++ (could only use up to 32 bits). So, that advantage for
using Class properties instead is already visible. Others would be
maintainability, easier reading and understanding.

But can you imagine the size of that class when you get done? Also, how are
you soing to save the state of that player, that too would be some lengthy code.

So, cost performance. What type of gains or otherwise would I get when using
properties vs bitwise storage (IE: Memory consumption, processor usage, et
cetera)?

Please don't go out of yer way to test, just need something off top of yer heads

I'd suggest you go back to the bit twiddling method, you'd end up with more
compact code. You can get around the 32-bit limitation by overriding your
methods, something like:

[ Player Class ]

Public Enum IsSwitch
IsAlive = 1
IsMobile = 2
IsProtected = 4
''''
IsAngry = &H4000000
End Enum

Public Enum HasSwitch
HasFood = 1
HasWeapon = 2
''''
HasScroll = &H40000000
End Enum

Private mIsSwitch As Integer
Private mHasSwitch As Integer



Public Function Switches(ByVal Item As IsSwitch) As Boolean
Return CBool(Item And mIsSwitch)
End Function

Public Function Switches(ByVal item As HasSwitch) As Boolean
Return CBool(item And mHasSwitch)
End Function

Public Sub SetSwitch(ByVal Item As IsSwitch, ByVal Value As Boolean)
If Value Then
mIsSwitch = mIsSwitch Or Item
Else
mIsSwitch = mIsSwitch And Not Item
End If
End Sub

Public Sub SetSwitch(ByVal Item As HasSwitch, ByVal Value As Boolean)
If Value Then
mHasSwitch = mHasSwitch Or Item
Else
mHasSwitch = mHasSwitch And Not Item
End If
End Sub

You could of course expand that idea to more than 2 Emuns, and more than
2 overloads to get as many switches as you need....

Then your code could call on the appropreate function by passing in the
right item, which Intellisense will supply:

Dim plr As New Player

... game code

If plr.Switches(HasSwitch.HasFood) And plr.Switches(IsSwitch.IsMobile) Then
plr.Move
End If


HTH
LFS
 
L

Larry Serflaten

Larry Serflaten said:
Public Function Switches(ByVal Item As IsSwitch) As Boolean
Return CBool(Item And mIsSwitch)
End Function
You could of course expand that idea to more than 2 Emuns, and more than
2 overloads to get as many switches as you need....

If plr.Switches(HasSwitch.HasFood) And plr.Switches(IsSwitch.IsMobile) Then
plr.Move
End If


I typed that up pretty fast, so the naming convention could use a little work, but
I wanted to also mention that the two types cannot be mingled, because they both
use the same range of numbers. With a little adjustment similar types can be mingled:

Public Function Switches(ByVal Item As IsSwitch) As Boolean
Return (Item And mIsSwitch) = Item
End Function


Then:


If plr.Switches(IsSwitch.IsMobile + IsSwitch.IsAlive) Then ...

I used + instead of Or here because it reads better. Using Or may lead
some to think either may be present when the result actually depends on
both being present. For this case, + adds a bit more clarity....

Have fun!
LFS
 
L

Larry Serflaten

If plr.Switches(IsSwitch.IsMobile + IsSwitch.IsAlive) Then ...

I used + instead of Or here because it reads better.


Too bad you can't use that in your code, however!

One more thing... You can add convenience values to your
Enums:

Public Enum IsSwitch
IsAlive = 1
IsMobile = 2
IsProtected = 4
IsStrong = 8
''''

IsSuperCharged = IsAlive Or IsProtected Or IsStrong
End Enum

;-)
LFS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top