473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Bit Values compared to Property Flags

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
Nov 21 '05 #1
3 2485

"Mythran" <ki********@hotmail.com> wrote
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
Nov 21 '05 #2

"Larry Serflaten" <se*******@usinternet.com> wrote
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
Nov 21 '05 #3
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
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Perttu Pulkkinen | last post by:
Hi all php freaks! Do you think this kind of "property class" is useful or not? I have bee bored to the way I've been coding earlier. because: - often i have database-oriented classes like...
2
by: Bill Cohagan | last post by:
In my app I'm validating an XML file against an XSD which contains several attribute default value specifications. I'm performing the validation via an xml document load() using a...
6
by: cipher | last post by:
I have some constant values in my web service that my client application will require. Having to keep server side and client side definitions insync is tedious. I am trying to do something like...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
4
by: Hugh O'Donnell | last post by:
I am trying to access the values of child properties by a string value. For instance, have a user type "Form1.txtFirstName.Text" in a text box, click on a button, and return the value of that...
9
by: userblue | last post by:
Hi Does anyone know if there is a way to define what is effectively a single globally visible, enumerated list whilst actually defining the entries across several different modules? or somehow do...
2
by: cantankerousoldgit | last post by:
I am trying to call a DLL with a function like this: """DESCRIPTION: Get a list of objects attributes matching attribute values ARGUMENTS: session : the current session classId :...
8
by: karthikbalaguru | last post by:
Hi, Is it possible to define default values for arguments that are not passed when the function call is made ? I tried it , but it showed linking errors. Any other ideas/help ? Thx in...
11
by: Jon Slaughter | last post by:
This is more of a C++ question but I guess it applies equally well to C#. Is there a way to declare an enum where the values assigned to the fields is incremented by something rather than 1. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.