[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: storing boolean flags in numbers

seebs

5/23/2007 10:46:00 AM

In message <f15b00519ae6326d2171a8a162995dbd@ruby-forum.com>, Sebastian probst Eide writes:
>I basically just need a way to read the different values. I'll try to
>extend it myself so I can read any given number of flags myself. Just
>need to understand how to read and set the flags!

You're looking for "bitwise operators".

Experiment with "5 & 4" or "1 | 4".

-s

5 Answers

Sebastian probst Eide

5/23/2007 10:51:00 AM

0

> You're looking for "bitwise operators".
>
> Experiment with "5 & 4" or "1 | 4".
>
> -s
Great,
thanks


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

Nobuyoshi Nakada

5/23/2007 11:10:00 AM

0

Hi,

At Wed, 23 May 2007 19:46:01 +0900,
Peter Seebach wrote in [ruby-talk:252673]:
> Experiment with "5 & 4" or "1 | 4".

p 5[2]

--
Nobu Nakada

Sebastian probst Eide

5/23/2007 11:16:00 AM

0

> p 5[2]
Wow! This is great! But is there a way to set flags this way to?

Say I have:
flags = 0b010110010

checking flag 6 is easy enough
flags[6] => 0
But how could I easily set flag 6 to 1?
flags[6] = 1 doesn't work obviously...



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

Robert Klemme

5/23/2007 11:31:00 AM

0

On 23.05.2007 13:16, Sebastian probst Eide wrote:
>> p 5[2]
> Wow! This is great! But is there a way to set flags this way to?
>
> Say I have:
> flags = 0b010110010
>
> checking flag 6 is easy enough
> flags[6] => 0
> But how could I easily set flag 6 to 1?
> flags[6] = 1 doesn't work obviously...

Set and reset:

irb(main):001:0> flags = 0b010110010
=> 178
irb(main):002:0> flags[6]
=> 0
irb(main):003:0> flags |= 1 << 6
=> 242
irb(main):004:0> flags[6]
=> 1
irb(main):005:0> flags ^= flags[6] << 6
=> 178
irb(main):006:0> flags[6]
=> 0

See also http://raa.ruby-lang.org/proje...

Kind regards

robert

Sebastian probst Eide

5/23/2007 12:03:00 PM

0

Great!
Thanks Robert

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