BIT banging help

  • Thread starter Thread starter DotNetNewbie
  • Start date Start date
D

DotNetNewbie

I want to store the maximum amount of permissions in a bit mask.

From what I know, I should use the C# long data type and use the
bigint in sqlserver.

How would I initialize a variable of type long to represent a 64bit
mask?

I think it is something like:

0x0..............0

But I am unsure as to the exact number of 0's, can someone clear this
up for me (with as much detail as possible).
 
[...]
How would I initialize a variable of type long to represent a 64bit
mask?

I think it is something like:

0x0..............0

But I am unsure as to the exact number of 0's, can someone clear this
up for me (with as much detail as possible).

long grbitMask = 0;

You can write as many zeros in your literal as you like. The number is
still zero.
 
DotNetNewbie said:
I want to store the maximum amount of permissions in a bit mask.

From what I know, I should use the C# long data type and use the
bigint in sqlserver.

How would I initialize a variable of type long to represent a 64bit
mask?

I think it is something like:

0x0..............0


When entering the digits in hexadecimal (prefix 0x), every digit
represents four bits. Therefore, you would need 16 hex digits to represent a
64-bit value. However, if it is all zeroes that you want, you don't have to
write all of them. Just writing value=0 will set to zero all the bits in
value.
 
    When entering the digits in hexadecimal (prefix 0x), every digit
represents four bits. Therefore, you would need 16 hex digits to representa
64-bit value. However, if it is all zeroes that you want, you don't have to
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).
 
[...]
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
 
[...]
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

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?
 
DotNetNewbie said:
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).

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
 
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 formulated the question :
You don't need to worry about setting the values - just use:

[Flags]
enum Permissions : ulong
{
CanLogIn,
CanXXXX,
CanAAA,
CanBBB
}

Alun Harford

No, that would still use underlying values of 0, 1, 2, 3 instead of 1,
2, 4, 8

Hans Kesting
 
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 :

You don't need to worry about setting the values - just use:

[Flags]
enum Permissions : ulong
{
CanLogIn,
CanXXXX,
CanAAA,
CanBBB
}

Alun Harford


No, that would still use underlying values of 0, 1, 2, 3 instead of 1,
2, 4, 8

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top