Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

BIT banging help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DotNetNewbie - 17 Jan 2008 19:59 GMT
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).
Peter Duniho - 17 Jan 2008 20:04 GMT
> [...]
> How would I initialize a variable of type long to represent a 64bit
[quoted text clipped - 6 lines]
> 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.
Alberto Poblacion - 17 Jan 2008 20:17 GMT
>I want to store the maximum amount of permissions in a bit mask.
>
[quoted text clipped - 7 lines]
>
> 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.
DotNetNewbie - 17 Jan 2008 21:23 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.