>I want to store the maximum amount of permissions in a bit mask.
>
[quoted text clipped - 7 lines]
>
> 0x0..............0
On Jan 17, 3:17 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.org> wrote:
> >I want to store the maximum amount of permissions in a bit mask.
>
[quoted text clipped - 13 lines]
> write all of them. Just writing value=0 will set to zero all the bits in
> value.
I want to make an enumeration that will represent all my permission
values for each role, then I will get all the users roles and then OR
them together to get the users final permission value.
So something like:
enum Permisions : long
{
CanLogIn = 0,
CanXXXX = 0x0000000000000001,
CanAAA = 0x0000000000000002,
CanBBB = 0x0000000000000004,
etc.
}
1. how many enum values can I store with a long? (which I will then
store as a bigint in sqlserver).
Peter Duniho - 17 Jan 2008 21:44 GMT
> [...]
> 1. how many enum values can I store with a long? (which I will then
> store as a bigint in sqlserver).
Do you know how many bits are in a long?
Do you know how many bits it takes to store one permission flag?
You _should_ already have the answers to both of those questions. And
from those answers, the answer to "how many enum values can I store with a
long" is trivial to answer.
Pete
DotNetNewbie - 17 Jan 2008 21:53 GMT
On Jan 17, 4:44 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > [...]
> > 1. how many enum values can I store with a long? (which I will then
[quoted text clipped - 9 lines]
>
> Pete
long is 64 bits, or 8 bytes.
I want to be able to AND and OR each permission enum value, so there
can't be any overlap.
From what I understand that I will use 1 bit per permission right?
Peter Duniho - 17 Jan 2008 22:57 GMT
> long is 64 bits, or 8 bytes.
Yes.
> I want to be able to AND and OR each permission enum value, so there
> can't be any overlap.
Yes. Obviously you cannot use the same bit for two different values.
> From what I understand that I will use 1 bit per permission right?
Assuming each permission is "on" or "off", yes.
Alun Harford - 17 Jan 2008 22:21 GMT
> On Jan 17, 3:17 pm, "Alberto Poblacion" <earthling-
> quitaestoparacontes...@poblacion.org> wrote:
[quoted text clipped - 30 lines]
> 1. how many enum values can I store with a long? (which I will then
> store as a bigint in sqlserver).
You can store 64 bits if you use a ulong and bigint unsigned.
You can store 63 bits if you use a long and bigint.
You don't need to worry about setting the values - just use:
[Flags]
enum Permissions : ulong
{
CanLogIn,
CanXXXX,
CanAAA,
CanBBB
}
Alun Harford
Hans Kesting - 18 Jan 2008 08:48 GMT
Alun Harford formulated the question :
> You don't need to worry about setting the values - just use:
>
[quoted text clipped - 8 lines]
>
> Alun Harford
No, that would still use underlying values of 0, 1, 2, 3 instead of 1,
2, 4, 8
Hans Kesting
Ian Semmel - 18 Jan 2008 19:00 GMT
You can do something like this
public class Permissions
{
ulong val = 0;
public enum Attr
{
CanLogIn,
CanXXXX,
CanAAA,
CanBBB
}
public Permissions (params Attr[] attr)
{
val = 0;
foreach (Attr a in attr)
val |= ( 1U << (int) a );
}
public bool IsSet(Attr a)
{
return ((val & ( 1U << (int) a )) != 0);
}
// etc
}
and then
Permissions p = new Permissions(Permissions.Attr.CanAAA,
Permissions.Attr.CanBBB);
bool b = p.IsSet(Permissions.Attr.CanBBB);
b = p.IsSet(Permissions.Attr.CanLogIn);
Could be done better but I know C++ better than C#. Checkout [Flags] for
Attr.
> Alun Harford formulated the question :
>
[quoted text clipped - 15 lines]
>
> Hans Kesting