473,394 Members | 1,887 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,394 software developers and data experts.

BitWise Operations

Jon
Hello all.

I'm currently in the middle of writing a web app and I've come to some
security aspects that I'd appreciate some help with.

I'd like to use some form of BitWise opration on the persons security. For
example, if the following permissions exist in the system:

0: None
1: Add Member
2: Edit Member
4: Delete Member
8: Add Company
16: Edit Company
32: Delete Company

and my user had permissions to Add and Edit a member, they would have the
number 3 (1 + 2) associated with them, another user may have permissions
number 12, to delete a member and add a company.

But how do i effectivky 'decode' this number. So if i pass 12 to some
method, how do I work out that it evaluates to Delete a member and Add a
company?

Any help, or indeed other ideas and example would be much appreciated.

Thanks all,

Jon
Jan 23 '06 #1
3 1171
I use the following (in c#, but can be used in vb)

public boolean HasRight(int RightMask, int RightToTest)
{
if (Convert.ToBoolean(RightMask & RightToTest))
{
return true;
}
else
{
return false;
}
}

I use the & unary operator. So the (RightMask & RightToTest) operation will
be equal to RightToTest if the bit is set to 1 and to 0 if the bit is not
set to 1.

I hope it helps

ThunderMusic

"Jon" <Jo*@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
Hello all.

I'm currently in the middle of writing a web app and I've come to some
security aspects that I'd appreciate some help with.

I'd like to use some form of BitWise opration on the persons security. For
example, if the following permissions exist in the system:

0: None
1: Add Member
2: Edit Member
4: Delete Member
8: Add Company
16: Edit Company
32: Delete Company

and my user had permissions to Add and Edit a member, they would have the
number 3 (1 + 2) associated with them, another user may have permissions
number 12, to delete a member and add a company.

But how do i effectivky 'decode' this number. So if i pass 12 to some
method, how do I work out that it evaluates to Delete a member and Add a
company?

Any help, or indeed other ideas and example would be much appreciated.

Thanks all,

Jon

Jan 23 '06 #2
Here's an example you might find helpful:

[Flags()]
public enum Permissions
{
None = 0,
AddMember = 1,
EditMember = 2,
DeleteMember = 4,
ManageMember = AddMember | EditMember | DeleteMember,
AddCompany = 8,
EditCompany = 16,
DeleteCompany = 32,
ManageCompany = AddCompany | EditCompany | DeleteCompany,
All = ManageMember | ManageCompany
}

public class User
{
private Permissions _permission = Permissions.None;

public User(Permissions permission)
{
this._permission = permission;
}
public bool IsAllowed(Permissions permissionToCheck)
{
return (permissionToCheck & _permission) == permissionToCheck;
}
}
you can then do stuff like:

User user = new User(Permissions.AddMember | Permissions.DeleteCompany);
//create the user and set the his/her permissions

and then check it via:
user.IsAllowed(Permissions.EditMember);
Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Jon" <Jo*@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
Hello all.

I'm currently in the middle of writing a web app and I've come to some
security aspects that I'd appreciate some help with.

I'd like to use some form of BitWise opration on the persons security. For
example, if the following permissions exist in the system:

0: None
1: Add Member
2: Edit Member
4: Delete Member
8: Add Company
16: Edit Company
32: Delete Company

and my user had permissions to Add and Edit a member, they would have the
number 3 (1 + 2) associated with them, another user may have permissions
number 12, to delete a member and add a company.

But how do i effectivky 'decode' this number. So if i pass 12 to some
method, how do I work out that it evaluates to Delete a member and Add a
company?

Any help, or indeed other ideas and example would be much appreciated.

Thanks all,

Jon

Jan 23 '06 #3
Jon
Chaps,

Thanks to both of you for your help, really helpful.

Jon

"Karl Seguin [MVP]" wrote:
Here's an example you might find helpful:

[Flags()]
public enum Permissions
{
None = 0,
AddMember = 1,
EditMember = 2,
DeleteMember = 4,
ManageMember = AddMember | EditMember | DeleteMember,
AddCompany = 8,
EditCompany = 16,
DeleteCompany = 32,
ManageCompany = AddCompany | EditCompany | DeleteCompany,
All = ManageMember | ManageCompany
}

public class User
{
private Permissions _permission = Permissions.None;

public User(Permissions permission)
{
this._permission = permission;
}
public bool IsAllowed(Permissions permissionToCheck)
{
return (permissionToCheck & _permission) == permissionToCheck;
}
}
you can then do stuff like:

User user = new User(Permissions.AddMember | Permissions.DeleteCompany);
//create the user and set the his/her permissions

and then check it via:
user.IsAllowed(Permissions.EditMember);
Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Jon" <Jo*@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
Hello all.

I'm currently in the middle of writing a web app and I've come to some
security aspects that I'd appreciate some help with.

I'd like to use some form of BitWise opration on the persons security. For
example, if the following permissions exist in the system:

0: None
1: Add Member
2: Edit Member
4: Delete Member
8: Add Company
16: Edit Company
32: Delete Company

and my user had permissions to Add and Edit a member, they would have the
number 3 (1 + 2) associated with them, another user may have permissions
number 12, to delete a member and add a company.

But how do i effectivky 'decode' this number. So if i pass 12 to some
method, how do I work out that it evaluates to Delete a member and Add a
company?

Any help, or indeed other ideas and example would be much appreciated.

Thanks all,

Jon


Jan 24 '06 #4

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

Similar topics

11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
8
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
4
by: AMDRIT | last post by:
Gang, I always get confused when it comes to 1's and 0's. I would like to perform a bitwise operation on a value based on checked boxes. Am I doing this right? assuming...
3
by: shdwsclan | last post by:
I am native to various languages but bitwise operators just kill me. I see how much I take object oriented languages for granted. I like all the other c derivitives but ANSI C is making me loose my...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
8
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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...

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.