Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
storing boolean flags in numbers
Sebastian probst Eide
5/23/2007 10:43:00 AM
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-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
storing boolean flags in numbers
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password