storing boolean flags in numbers

Discussion in 'Ruby' started by Sebastian probst Eide, May 23, 2007.

  1. I might sound completely dumb asking this question, but well, I'll risk
    it anyway!

    Some years ago I read about using a single number as a mechanism for
    storing flags (on/off values) in a Java magazine.

    By setting the bits in a number to either 0 or 1 you could store
    enormous amounts of boolean values in a single number.

    An example.

    I have three flags:
    Flag1
    Flag2
    Flag3

    (The following byte sequences are written from the right)

    Flag1 = true
    Flag2 = false
    Flag3 = false
    gives:
    001 => 1

    Flag1 = false
    Flag2 = true
    Flag3 = true
    gives:
    110 => 6

    Flag1 = true
    Flag2 = false
    Flag3 = true
    gives:
    101 => 5

    In the magazine they used some sort of combination of AND and OR and XOR
    and stuff like that to get the different values.
    Would there be an easy way to do this in Ruby? How would one implement a
    thing like this?
    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!

    Best regards
    Sebastian

    --
    Posted via http://www.ruby-forum.com/.
    Sebastian probst Eide, May 23, 2007
    #1
    1. Advertising

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

    Great,
    thanks


    --
    Posted via http://www.ruby-forum.com/.
    Sebastian probst Eide, May 23, 2007
    #2
    1. Advertising

  3. 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
    Nobuyoshi Nakada, May 23, 2007
    #3
  4. > 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-forum.com/.
    Sebastian probst Eide, May 23, 2007
    #4
  5. 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/project/bitset/

    Kind regards

    robert
    Robert Klemme, May 23, 2007
    #5
  6. Sebastian probst Eide, May 23, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    11
    Views:
    766
    Dale King
    Jul 16, 2005
  2. zilleeizad

    problem with boolean flags

    zilleeizad, Nov 1, 2004, in forum: C++
    Replies:
    1
    Views:
    399
    Jack Klein
    Nov 1, 2004
  3. J Leonard
    Replies:
    4
    Views:
    12,604
    Mark Space
    Jan 19, 2008
  4. Steve Holden
    Replies:
    0
    Views:
    752
    Steve Holden
    Feb 8, 2009
  5. Metre Meter
    Replies:
    7
    Views:
    350
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page