# Storing boolean flags in numbers

**URL:** <https://www.ruby-forum.com/t/storing-boolean-flags-in-numbers/104871>\
**Category:** Ruby\
**Created:** [May 23, 2007, 12:43pm UTC](https://www.ruby-forum.com/t/storing-boolean-flags-in-numbers/104871 "2007-05-23T12:43:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 23, 2007, 12:43pm UTC](https://www.ruby-forum.com/t/storing-boolean-flags-in-numbers/104871/1 "2007-05-23T12:43:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 23, 2007, 12:46pm UTC](https://www.ruby-forum.com/t/storing-boolean-flags-in-numbers/104871/2 "2007-05-23T12:46:41Z")

</div>

In message [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid), 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 23, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/storing-boolean-flags-in-numbers/104871/3 "2007-05-23T12:51:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 23, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/storing-boolean-flags-in-numbers/104871/4 "2007-05-23T13:16:06Z")

</div>

> 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…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 23, 2007, 1:10pm UTC](https://www.ruby-forum.com/t/storing-boolean-flags-in-numbers/104871/5 "2007-05-23T13:10:48Z")

</div>

Hi,

At Wed, 23 May 2007 19:46:01 +0900,  
Peter S. wrote in [ruby-talk:252673]:

> Experiment with “5 & 4” or “1 | 4”.

p 5[2]

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 23, 2007, 1:36pm UTC](https://www.ruby-forum.com/t/storing-boolean-flags-in-numbers/104871/6 "2007-05-23T13:36:50Z")

</div>

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/](http://raa.ruby-lang.org/project/bitset/)

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 23, 2007, 2:03pm UTC](https://www.ruby-forum.com/t/storing-boolean-flags-in-numbers/104871/7 "2007-05-23T14:03:04Z")

</div>

Great!  
Thanks Robert
