[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Bitwise question

Andrew Barringer

1/28/2009 9:21:00 PM

I'm working on a project that has a bitmap of permissions and I need to
find out if a user has access.=20

=20

Given permissions bit mask of 0001C0200F02000000000 where each bit
represents a specific permission

=20

And a request for permissions check on bits [37, 12, 48]

=20

What's the best way to find out if user has access to all requested
permissions?


13 Answers

pjb

1/28/2009 9:34:00 PM

0

Andrew Barringer <abarringer@bsecure.com> writes:

> I'm working on a project that has a bitmap of permissions and I need to
> find out if a user has access.
>
>
>
> Given permissions bit mask of 0001C0200F02000000000 where each bit
> represents a specific permission
>
>
>
> And a request for permissions check on bits [37, 12, 48]
>
>
>
> What's the best way to find out if user has access to all requested
> permissions?


(def check(permissions,bits)
(bits . inject(true) { | r , b |
(r and (0 != (permissions & (1 << b))))
})
end)
nil
(check 0xf0,[1,4,5,6]) -> false
(check 0xf0,[4,5,6]) -> true

--
__Pascal Bourguignon__

Rob Biedenharn

1/28/2009 10:07:00 PM

0


On Jan 28, 2009, at 4:32 PM, Pascal J. Bourguignon wrote:

> Andrew Barringer <abarringer@bsecure.com> writes:
>
>> I'm working on a project that has a bitmap of permissions and I
>> need to
>> find out if a user has access.
>>
>>
>>
>> Given permissions bit mask of 0001C0200F02000000000 where each bit
>> represents a specific permission
>>
>>
>>
>> And a request for permissions check on bits [37, 12, 48]
>>
>>
>>
>> What's the best way to find out if user has access to all requested
>> permissions?
>
>
> (def check(permissions,bits)
> (bits . inject(true) { | r , b |
> (r and (0 != (permissions & (1 << b))))
> })
> end)
> nil
> (check 0xf0,[1,4,5,6]) -> false
> (check 0xf0,[4,5,6]) -> true
>
> --
> __Pascal Bourguignon__

Or a simpler version:

class Integer
def as_bits
ary = []
(self.size*8).times {|i| ary.unshift(i) if self[i].nonzero?}
ary
end

def check_permissions(*bits)
bits.all? {|bit| self[bit].nonzero? }
end
end

irb> 0x0001C0200F02000000000.as_bits
=> [68, 67, 66, 57, 47, 46, 45, 44, 37]
irb> 0x0001C0200F02000000000.check_permissions 37, 12, 48
=> false
irb> 0x0001C0200F02000000000.check_permissions 37, 47, 57
=> true

And written in normal Ruby rather than LispRuby ;-)

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com

David A. Black

1/28/2009 10:10:00 PM

0

Hi --

On Thu, 29 Jan 2009, Pascal J. Bourguignon wrote:

> Andrew Barringer <abarringer@bsecure.com> writes:
>
>> I'm working on a project that has a bitmap of permissions and I need to
>> find out if a user has access.
>>
>>
>>
>> Given permissions bit mask of 0001C0200F02000000000 where each bit
>> represents a specific permission
>>
>>
>>
>> And a request for permissions check on bits [37, 12, 48]
>>
>>
>>
>> What's the best way to find out if user has access to all requested
>> permissions?
>
>
> (def check(permissions,bits)
> (bits . inject(true) { | r , b |
> (r and (0 != (permissions & (1 << b))))
> })
> end)
> nil
> (check 0xf0,[1,4,5,6]) -> false
> (check 0xf0,[4,5,6]) -> true

Possibly more efficient since it short-circuits on the first failed
bit:

def check(permissions,bits)
bits.all? {|bit| (permissions & 1 << bit).nonzero? }
end


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

Albert Schlef

1/28/2009 11:35:00 PM

0

Pascal J. Bourguignon wrote:
> (def check(permissions,bits)
> (bits . inject(true) { | r , b |
> (r and (0 != (permissions & (1 << b))))
> })
> end)


That what happens when one spends his time typing parenthesis instead of
programming, my dear Pascal. I managed to count at least two bugs there.
One, you need to do (1 << (b-1)). Two, if you pass [] as the bits, the
result will be 'true'.
--
Posted via http://www.ruby-....

Stefan Rusterholz

1/29/2009 12:03:00 AM

0

Andrew Barringer wrote:
> I'm working on a project that has a bitmap of permissions and I need to
> find out if a user has access.
>
>
>
> Given permissions bit mask of 0001C0200F02000000000 where each bit
> represents a specific permission
>
>
>
> And a request for permissions check on bits [37, 12, 48]
>
>
>
> What's the best way to find out if user has access to all requested
> permissions?

Whatever 'best' is supposed to mean. Here is one way:

requested_bits.all? { |bit| permissions[bit] == 1 }

Regards
Stefan
--
Posted via http://www.ruby-....

pjb

1/29/2009 12:34:00 AM

0

Rob Biedenharn <Rob@AgileConsultingLLC.com> writes:

> On Jan 28, 2009, at 4:32 PM, Pascal J. Bourguignon wrote:
>
>> Andrew Barringer <abarringer@bsecure.com> writes:
>>
>>> What's the best way to find out if user has access to all requested
>>> permissions?
>>
>> (def check(permissions,bits)
>> (bits . inject(true) { | r , b |
>> (r and (0 != (permissions & (1 << b))))
>> })
>> end)
>> nil
> Or a simpler version:
>
> class Integer
> def as_bits
> ary = []
> (self.size*8).times {|i| ary.unshift(i) if self[i].nonzero?}
> ary
> end
>
> def check_permissions(*bits)
> bits.all? {|bit| self[bit].nonzero? }
> end
> end

Simplier? A class, two methods, a lot of operations, a lot of memory!

--
__Pascal Bourguignon__

pjb

1/29/2009 12:42:00 AM

0

Albert Schlef <albertschlef@gmail.com> writes:

> Pascal J. Bourguignon wrote:
>> (def check(permissions,bits)
>> (bits . inject(true) { | r , b |
>> (r and (0 != (permissions & (1 << b))))
>> })
>> end)
>
>
> That what happens when one spends his time typing parenthesis instead of
> programming, my dear Pascal. I managed to count at least two bugs there.
> One, you need to do (1 << (b-1)).

Bit 0 is the first bit. 2^0 = 1

> Two, if you pass [] as the bits, the
> result will be 'true'.

Which is of course the correct answer.
All the bits in the empty set are in any bitfield.
There is no bit in the empty set hat are not in a given bitfield.

--
__Pascal Bourguignon__

Albert Schlef

1/29/2009 2:12:00 AM

0

Pascal J. Bourguignon wrote:
> Albert Schlef <albertschlef@gmail.com> writes:
> > One, you need to do (1 << (b-1)).
> Bit 0 is the first bit. 2^0 = 1

I stand corrected.

> > Two, if you pass [] as the bits, the
> > result will be 'true'.
>
> Which is of course the correct answer.
> All the bits in the empty set are in any bitfield.
> There is no bit in the empty set hat are not in a given bitfield.

All right, all right, you win ;-) I now see that [].all? too returns
'true'.

(So my attempt at dissuading you from using parenthesis failed
miserably. Say, won't you consider dropping them for humanitarian
reasons?)
--
Posted via http://www.ruby-....

David A. Black

1/29/2009 2:16:00 AM

0

Hi --

On Thu, 29 Jan 2009, Rob Biedenharn wrote:

>
> On Jan 28, 2009, at 4:32 PM, Pascal J. Bourguignon wrote:
>
>> Andrew Barringer <abarringer@bsecure.com> writes:
>>
>>> I'm working on a project that has a bitmap of permissions and I need to
>>> find out if a user has access.
>>>
>>>
>>>
>>> Given permissions bit mask of 0001C0200F02000000000 where each bit
>>> represents a specific permission
>>>
>>>
>>>
>>> And a request for permissions check on bits [37, 12, 48]
>>>
>>>
>>>
>>> What's the best way to find out if user has access to all requested
>>> permissions?
>>
>>
>> (def check(permissions,bits)
>> (bits . inject(true) { | r , b |
>> (r and (0 != (permissions & (1 << b))))
>> })
>> end)
>> nil
>> (check 0xf0,[1,4,5,6]) -> false
>> (check 0xf0,[4,5,6]) -> true
>>
>> --
>> __Pascal Bourguignon__
>
> Or a simpler version:
>
> class Integer
> def as_bits
> ary = []
> (self.size*8).times {|i| ary.unshift(i) if self[i].nonzero?}
> ary
> end
>
> def check_permissions(*bits)
> bits.all? {|bit| self[bit].nonzero? }
> end
> end

Thanks for the reminder about Integer#[], one of those cool Ruby
things that come back and re-surprise me with their coolness
occasionally :-)


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

David A. Black

1/29/2009 2:22:00 AM

0

On Thu, 29 Jan 2009, Pascal J. Bourguignon wrote:

> Rob Biedenharn <Rob@AgileConsultingLLC.com> writes:
>
>> On Jan 28, 2009, at 4:32 PM, Pascal J. Bourguignon wrote:
>>
>>> Andrew Barringer <abarringer@bsecure.com> writes:
>>>
>>>> What's the best way to find out if user has access to all requested
>>>> permissions?
>>>
>>> (def check(permissions,bits)
>>> (bits . inject(true) { | r , b |
>>> (r and (0 != (permissions & (1 << b))))
>>> })
>>> end)
>>> nil
>> Or a simpler version:
>>
>> class Integer
>> def as_bits
>> ary = []
>> (self.size*8).times {|i| ary.unshift(i) if self[i].nonzero?}
>> ary
>> end
>>
>> def check_permissions(*bits)
>> bits.all? {|bit| self[bit].nonzero? }
>> end
>> end
>
> Simplier? A class, two methods, a lot of operations, a lot of memory!

as_bits is just there to provide information for the sake of
understanding the examples. check_permissions is Rob's answer to the
original question. It looks about as simple as it could be: very
idiomatic, clear, and concise.

If you don't like adding to Integer you can make it functional-style
by adding a second argument.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!