# Using Integers to Store Bits of Information

**URL:** <https://www.ruby-forum.com/t/using-integers-to-store-bits-of-information/97396>\
**Category:** Rails\
**Created:** [July 6, 2006, 8:32pm UTC](https://www.ruby-forum.com/t/using-integers-to-store-bits-of-information/97396 "2006-07-06T20:32:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![P.V.Anthony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/59ef9b/32.png) [@P.V.Anthony](https://www.ruby-forum.com/u/P.V.Anthony)\
**Post date:** [July 6, 2006, 8:32pm UTC](https://www.ruby-forum.com/t/using-integers-to-store-bits-of-information/97396/1 "2006-07-06T20:32:47Z")

</div>

Hi,

I am a very new to ruby on rails so please forgive me if someone has  
asked this before.

I need to make a form with many checkboxes. Sometime ago I was using  
Coldfusion and I chanced upon this great article on how to store flags  
using binary numbers.  
[http://cfdj.sys-con.com/read/41674.htm](http://cfdj.sys-con.com/read/41674.htm)

Just wondering if there is something similar in RubyOnRails that I can  
learn from and use. That rails have done it easier.

Please note that I am very new to RubyOnRails.

P.V.Anthony

---

<div class="post-metadata">

**Author:** ![P.V.Anthony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/59ef9b/32.png) [@P.V.Anthony](https://www.ruby-forum.com/u/P.V.Anthony)\
**Post date:** [July 6, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/using-integers-to-store-bits-of-information/97396/2 "2006-07-06T21:08:10Z")

</div>

> Please note that I am very new to RubyOnRails.

I don’t know of anything that will do this automatically, but you can  
certainly do it. I do it right now manually just by shifting bits  
around  
with \<\<. Works fine.

-philip

---

<div class="post-metadata">

**Author:** ![P.V.Anthony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/59ef9b/32.png) [@P.V.Anthony](https://www.ruby-forum.com/u/P.V.Anthony)\
**Post date:** [July 6, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/using-integers-to-store-bits-of-information/97396/3 "2006-07-06T21:49:49Z")

</div>

Philip H. wrote:

> > Please note that I am very new to RubyOnRails.
> 
> I don’t know of anything that will do this automatically, but you can  
> certainly do it. I do it right now manually just by shifting bits  
> around  
> with \<\<. Works fine.
> 
> -philip

Fixnums support some bit-wise opperations  
You can also store binary values explicitly:  
a = 0b01

a[n] returns the nth bit, where a[0] is the least significant bit

See [http://ruby-doc.org/core/classes/Fixnum.html](http://ruby-doc.org/core/classes/Fixnum.html) for details

Cheers

---

<div class="post-metadata">

**Author:** ![P.V.Anthony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/59ef9b/32.png) [@P.V.Anthony](https://www.ruby-forum.com/u/P.V.Anthony)\
**Post date:** [July 7, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/using-integers-to-store-bits-of-information/97396/4 "2006-07-07T20:25:52Z")

</div>

Use composition.

Make a class that represents your flags as boolean attributes,  
then have it map those attributes to integers in any convenient  
way.

Do yourself a favor and DON’T expose your flagging implementation.

---

<div class="post-metadata">

**Author:** ![P.V.Anthony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/59ef9b/32.png) [@P.V.Anthony](https://www.ruby-forum.com/u/P.V.Anthony)\
**Post date:** [July 7, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/using-integers-to-store-bits-of-information/97396/5 "2006-07-07T20:45:20Z")

</div>

On Friday, July 07, 2006, at 11:23 AM, Tom M. wrote:

> > Hi,
> > 
> > > can learn from and use. That rails have done it easier.  
> > > Rails mailing list  
> > > removed\_email\_address@domain.invalid  
> > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

I think there’s a ‘acts\_as\_bitfield’ plugin somewhere.

\_Kevin
