473,395 Members | 2,467 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,395 software developers and data experts.

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?
Mar 5 '07 #1
13 1305
Mark wrote:
<snip>
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.

Mar 5 '07 #2
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" <Ma**@discussions.microsoft.comwrote in message
news:4F**********************************@microsof t.com...
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?

Mar 5 '07 #3
On Mar 5, 10:21 am, Mark <M...@discussions.microsoft.comwrote:
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

Mar 5 '07 #4
Thanks to all. I'm off and running now!

"Mark" wrote:
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?
Mar 5 '07 #5


"Mark" <Ma**@discussions.microsoft.comwrote in message
news:4F**********************************@microsof t.com...
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
Mar 5 '07 #6
Mark wrote:
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.
Mar 5 '07 #7

"Mark" <Ma**@discussions.microsoft.comwrote in message
news:4F**********************************@microsof t.com...
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
Mar 6 '07 #8

"Hal Rosser" <hm******@bellsouth.netwrote in message
news:UF****************@bignews2.bellsouth.net...
>
"Mark" <Ma**@discussions.microsoft.comwrote in message
news:4F**********************************@microsof t.com...
>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

Mar 6 '07 #9

"Hal Rosser" <hm******@bellsouth.netwrote in message
news:UF****************@bignews2.bellsouth.net...
>
"Mark" <Ma**@discussions.microsoft.comwrote in message
news:4F**********************************@microsof t.com...
>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

Mar 6 '07 #10
Hal Rosser wrote:
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.
Mar 6 '07 #11

"ShaneO" <sp****@optusnet.com.auwrote in message
news:45**********************@news.optusnet.com.au ...
Hal Rosser wrote:
>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.

Mar 6 '07 #12
Hal Rosser wrote:
"ShaneO" <sp****@optusnet.com.auwrote in message
news:45**********************@news.optusnet.com.au ...
>Hal Rosser wrote:
>>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
Mar 6 '07 #13
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.
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.
Mar 6 '07 #14

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

Similar topics

2
by: TSK | last post by:
I cannot get my focus() fn to work in my validateInput(userInput) fn. It will not allow the user to go back and correct invalid input like it should. Instead it goes right on through with the...
5
by: Mad Scientist Jr | last post by:
Has anyone worked on code that that can parse evaluation expressions (could be numbers or strings) like ( ( "dog" = "dog" ) or "foo" = "bar" ) and ("cow" = "bat" and "bye" = "hi") or ("math" =...
6
by: lucy | last post by:
Hello comp.lang.js.I need a script to rotate a background image on each newpage load, given the id of the element and a list (array?)of 2 or more image paths ("img1.jpg", "img2.jpg", ...).Actually,...
16
by: didier.doussaud | last post by:
I have a stange side effect in my project : in my project I need to write "gobal" to use global symbol : .... import math .... def f() : global math # necessary ?????? else next line...
10
by: pcbutts1 | last post by:
Yes this is a homework assignment. My instructor has broken english and I just could not follow along last night. How do I do this Write the program in Java (without a graphical user interface)...
1
by: ArcInversion | last post by:
Hi, I've been using a javascript script to create a dragon that flies across the page. Anyways, I'd like to make it so when you click the dragon it takes you to a new page. Was wondering if anyone...
3
by: knightsmastergeneral | last post by:
Hi, I'm currently working on getting junit and junireport to take some java files, convert it to xml and then display error messages / success rates etc in html which is one of the features of...
11
by: frankie_85 | last post by:
Hi everyone, I just made a simple code which is part of my assignment but I can't figure it out what's wrong with it. (always give me error messages) What the code basically does is: a...
1
by: feathers75 | last post by:
-------------------------------------------------------------------------------- First, Hello eveyone and I am new to Java. I am trying to create a Java program that will calculate a person...
4
by: Yonih | last post by:
So I am trying to get this Calculator to work. It needs to take in a vaule, and select a shipping Everythin works great except the shipping part. I need it to take the shipping value and add it to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.