Need help with Math...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Need help with a variable...

This variable is used to keep track of permissions and can contain
1,2,4,8,16,32,64,128

Or it can contain a sum of one more of the above values. For example, if
the variable has a value of 255, then it has all ove the above values added
together. I need help in figuring out the permissions, when for example the
variable contains 2 or more values added together. If I can break the larger
number down to it's lowest possible values then I can determine the
permissions. I'm sure there must be some mathmatical way to do this, but it
is eluding me so far. Any suggestions?
 
Mark wrote:
This variable is used to keep track of permissions and can contain
1,2,4,8,16,32,64,128

Or it can contain a sum of one more of the above values. For example, if
the variable has a value of 255, then it has all ove the above values added
together. I need help in figuring out the permissions, when for example the
variable contains 2 or more values added together.
<snip>

There are many ways to do it. Mostly, you'll be using the binary And
operator:

<aircode>
Function CheckPermission( _
CurrentValue As Integer, _
FlagValue As Integer _
) As Boolean

Return (CurrentValue And FlagValue) <> 0

End Function

If CheckPermission(Permission, EraseHD) Then
...
End If
</aircode>

Notice that usually such flags are defined as enumerators...

HTH.

Regards,

Branco.
 
Mark,

I'm not very good with VB.Net, but I did recently set up an Enum similar to
what you are doing. You can then use the enum in a Function, i.e., an
argument of the function is declared as the enum type. You can then use
OR's within the function to see which permissions are turned on. Sorry,
that's a rather bad explanation, but without VS in front of me, it's the
best I can do.

Doug
 
Need help with a variable...

This variable is used to keep track of permissions and can contain
1,2,4,8,16,32,64,128

Or it can contain a sum of one more of the above values. For example, if
the variable has a value of 255, then it has all ove the above values added
together. I need help in figuring out the permissions, when for example the
variable contains 2 or more values added together. If I can break the larger
number down to it's lowest possible values then I can determine the
permissions. I'm sure there must be some mathmatical way to do this, but it
is eluding me so far. Any suggestions?

Along with the other suggestions - you might want to look at the
System.Collections.BitArray class, as another possibiltiy.
 
Mark said:
Need help with a variable...

This variable is used to keep track of permissions and can contain
1,2,4,8,16,32,64,128

Or it can contain a sum of one more of the above values. For example, if
the variable has a value of 255, then it has all ove the above values
added
together. I need help in figuring out the permissions, when for example
the
variable contains 2 or more values added together. If I can break the
larger
number down to it's lowest possible values then I can determine the
permissions. I'm sure there must be some mathmatical way to do this, but
it
is eluding me so far. Any suggestions?

To actually get all of the bits that are "on", you can use the following:

Private Function GetFlags( _
ByVal Value As Byte _
) As Integer()
Dim b As Integer = 1
Dim found As ArrayList = New ArrayList()

While b <= Value
If (Value And b) = b
found.Add(b)
End If

b *= 2
End While

Return DirectCast(found.ToArray(GetType(Integer)), Integer())
End Function

HTH,
Mythran
 
Mark said:
Need help with a variable...

This variable is used to keep track of permissions and can contain
1,2,4,8,16,32,64,128

Or it can contain a sum of one more of the above values. For example, if
the variable has a value of 255, then it has all ove the above values added
together. I need help in figuring out the permissions, when for example the
variable contains 2 or more values added together. If I can break the larger
number down to it's lowest possible values then I can determine the
permissions. I'm sure there must be some mathmatical way to do this, but it
is eluding me so far. Any suggestions?

I'd probably look at using Enum as it creates self-documenting code plus
makes it easy to pick from a pop-up selection while coding - (Watch for
wrapping)

eg.
Private Enum byPermissions As Byte
FormatHDD = 1
DeleteFile = 2
MoveFile = 4
CreateFile = 8
CopyFile = 16
ViewFile = 32
End Enum


If (byThisUsersRights And byPermissions.FormatHDD) AndAlso
(byThisUsersRights And byPermissions.DeleteFile) Then
'Do something...
ElseIf (byThisUsersRights And byPermissions.MoveFile) Then
'Do something else...
End If


Just my suggestion.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
Mark said:
Need help with a variable...

This variable is used to keep track of permissions and can contain
1,2,4,8,16,32,64,128

Or it can contain a sum of one more of the above values. For example, if
the variable has a value of 255, then it has all ove the above values
added
together. I need help in figuring out the permissions, when for example
the
variable contains 2 or more values added together. If I can break the
larger
number down to it's lowest possible values then I can determine the
permissions. I'm sure there must be some mathmatical way to do this, but
it
is eluding me so far. Any suggestions?

You can just use the Byte data type.
It can store any value you require up to 255.
And you can use bitwise operators to tell if a certain bit is on or off.
See page 175 of Visual Basic 2005 Programmer's Reference by Rod Stephens
(Wrox publishing)
or search help for bitwise operators.
HTH
 
Hal Rosser said:
You can just use the Byte data type.
It can store any value you require up to 255.
And you can use bitwise operators to tell if a certain bit is on or off.
See page 175 of Visual Basic 2005 Programmer's Reference by Rod Stephens
(Wrox publishing)
or search help for bitwise operators.
HTH
here's a link to an explanation of using bits for flags - which is what
you're doing
http://testing.sadeveloper.net/Articles_View.aspx?articleID=182
 
Hal Rosser said:
You can just use the Byte data type.
It can store any value you require up to 255.
And you can use bitwise operators to tell if a certain bit is on or off.
See page 175 of Visual Basic 2005 Programmer's Reference by Rod Stephens
(Wrox publishing)
or search help for bitwise operators.
HTH

ok - here's an example
Dim myByte as Byte
myByte += 8 'turn on the 8-bit
myByte += 4 'turn on the 4-bit
'*** both bits 4 and 8 are now on and the value is 12***

If myByte and 8 then
msgbox("the 8-bit is on")
else
msgbox("the 8-bit is off")
End If
 
Hal said:
ok - here's an example
Dim myByte as Byte
myByte += 8 'turn on the 8-bit
myByte += 4 'turn on the 4-bit
'*** both bits 4 and 8 are now on and the value is 12***

If myByte and 8 then
msgbox("the 8-bit is on")
else
msgbox("the 8-bit is off")
End If

Hal, one problem I see in using fixed numeric values is that it's not
very self-explanatory. During programming I believe "myByte += 8" would
be too easy to forget and maybe assign the wrong value.

In my example, this would become - "myByte = (myByte Or
byPermissions.CreateFile)", which I truly believe is much easier to
comprehend (and remember) than just "8".

Just my opinion.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
ShaneO said:
Hal, one problem I see in using fixed numeric values is that it's not very
self-explanatory. During programming I believe "myByte += 8" would be too
easy to forget and maybe assign the wrong value.

In my example, this would become - "myByte = (myByte Or
byPermissions.CreateFile)", which I truly believe is much easier to
comprehend (and remember) than just "8".

Just my opinion.

ShaneO

Yes, I like the way you explained it.
I was just trying to show the bare-bones workings under-the-hood of turning
a bit on and how to use the "and" bit-wise operator to check if a certain
bit is turned on or not.
I would assign variables values as powers of 2 and name them as you did. and
check to make sure a bit is not already on before turning it on, and other
necessary chores associated with the task.
Good post.
 
Hal said:
Yes, I like the way you explained it.
I was just trying to show the bare-bones workings under-the-hood of
turning a bit on and how to use the "and" bit-wise operator to check
if a certain bit is turned on or not.
I would assign variables values as powers of 2 and name them as you
did. and check to make sure a bit is not already on before turning it
on, and other necessary chores associated with the task.
Good post.

There is no need to check if a bit is on before turning it on (unless
there's some other significance to that state) because, unlike adding a
number to the flags, the or operator will only affect the intended bit.

Andrew
 
Mark said:
This variable is used to keep track of permissions and can contain
1,2,4,8,16,32,64,128

Or it can contain a sum of one more of the above values.

Using VB.Net, I'd represent this as an Enum with the Flags Attribute on
it, as in:

<Flags()> _
Public Enum SecurityE
Option1 = 1
Option2 = 2
. . .
Option8 = 128
End Enum

Dim eAccess as SecurityE _
= SecurityE.Option1 Or SecurityE.Option3

? eAccess.ToString()
"Option1, Option3"

? ( eAccess And SecurityE.Option8 ) <> 0
False

? CInt( eAccess )
5

HTH,
Phill W.
 
Back
Top