Need help with Math... | | |
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? | | | | re: Need help with Math...
Mark wrote:
<snip> Quote:
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. | | | | re: Need help with Math...
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
"Mark" <Mark@discussions.microsoft.comwrote in message
news:4F413620-0099-45F3-BC2C-1EEF8F98B364@microsoft.com... Quote:
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?
| | | | re: Need help with Math...
On Mar 5, 10:21 am, Mark <M...@discussions.microsoft.comwrote: Quote:
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.
--
Tom Shelton | | | | re: Need help with Math...
Thanks to all. I'm off and running now!
"Mark" wrote: Quote:
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?
| | | | re: Need help with Math...
"Mark" <Mark@discussions.microsoft.comwrote in message
news:4F413620-0099-45F3-BC2C-1EEF8F98B364@microsoft.com... Quote:
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 | | | | re: Need help with Math...
Mark wrote: Quote:
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. | | | | re: Need help with Math...
"Mark" <Mark@discussions.microsoft.comwrote in message
news:4F413620-0099-45F3-BC2C-1EEF8F98B364@microsoft.com... Quote:
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 | | | | re: Need help with Math...
"Hal Rosser" <hmrosser@bellsouth.netwrote in message
news:UF3Hh.5519$sC.4935@bignews2.bellsouth.net... Quote:
>
"Mark" <Mark@discussions.microsoft.comwrote in message
news:4F413620-0099-45F3-BC2C-1EEF8F98B364@microsoft.com... Quote:
>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
>
>
here's a link to an explanation of using bits for flags - which is what
you're doing http://testing.sadeveloper.net/Artic...?articleID=182 | | | | re: Need help with Math...
"Hal Rosser" <hmrosser@bellsouth.netwrote in message
news:UF3Hh.5519$sC.4935@bignews2.bellsouth.net... Quote:
>
"Mark" <Mark@discussions.microsoft.comwrote in message
news:4F413620-0099-45F3-BC2C-1EEF8F98B364@microsoft.com... Quote:
>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
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 | | | | re: Need help with Math...
Hal Rosser wrote: Quote:
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. | | | | re: Need help with Math...
"ShaneO" <spcacc@optusnet.com.auwrote in message
news:45ece8d9$0$5746$afc38c87@news.optusnet.com.au ... Quote:
Hal Rosser wrote: Quote:
>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
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. | | | | re: Need help with Math...
Hal Rosser wrote: Quote:
"ShaneO" <spcacc@optusnet.com.auwrote in message
news:45ece8d9$0$5746$afc38c87@news.optusnet.com.au ... Quote:
>Hal Rosser wrote: Quote:
>>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
>
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 | | | | re: Need help with Math...
Mark wrote: Quote:
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. |  | Similar Visual Basic .NET bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,537 network members.
|