[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

One field maintaining (boolean) state of multiple items

wmwilson01

11/29/2006 6:52:00 PM

I have a friend who posed a question to me about how to maintain the
state of a few boolean values (radio buttons to be specific) in one
(integer) database column. It really is more of a curious question than
anything else, as he could simply add more columns to the table to avoid
even thinking about it, but for the sake of fun he didn't. So, let's
say you have three boolean values that you need to store in one field,
and you must be able to determine what the state is (true/false) of any
of the 3 values at any time. How would you do this? My immediate
thought went to bit shifting or some other bit operations, but I've
never really used anything like that before, so I'm a little stuck on
this. Thoughts?

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

57 Answers

Paul Lutus

11/29/2006 9:53:00 PM

0

El Gato wrote:

> I have a friend who posed a question to me about how to maintain the
> state of a few boolean values (radio buttons to be specific) in one
> (integer) database column. It really is more of a curious question than
> anything else, as he could simply add more columns to the table to avoid
> even thinking about it, but for the sake of fun he didn't. So, let's
> say you have three boolean values that you need to store in one field,
> and you must be able to determine what the state is (true/false) of any
> of the 3 values at any time. How would you do this? My immediate
> thought went to bit shifting or some other bit operations, but I've
> never really used anything like that before, so I'm a little stuck on
> this. Thoughts?

Given an integer that you want to disassemble into binary bits:

v = 15

Let's create an array that will contain the values of the bits:

array = []

Now let's scan the integer's bits:

1.upto(8) do
array << (v & 1 == 1)
v >>= 1
end

Now the array contains 8 boolean (true/false) values corresponding to the
low 8 bits in the original integer.

--
Paul Lutus
http://www.ara...

Joel VanderWerf

11/29/2006 10:53:00 PM

0

Paul Lutus wrote:
> El Gato wrote:
>
>> I have a friend who posed a question to me about how to maintain the
>> state of a few boolean values (radio buttons to be specific) in one
>> (integer) database column. It really is more of a curious question than
>> anything else, as he could simply add more columns to the table to avoid
>> even thinking about it, but for the sake of fun he didn't. So, let's
>> say you have three boolean values that you need to store in one field,
>> and you must be able to determine what the state is (true/false) of any
>> of the 3 values at any time. How would you do this? My immediate
>> thought went to bit shifting or some other bit operations, but I've
>> never really used anything like that before, so I'm a little stuck on
>> this. Thoughts?
>
> Given an integer that you want to disassemble into binary bits:
>
> v = 15
>
> Let's create an array that will contain the values of the bits:
>
> array = []
>
> Now let's scan the integer's bits:
>
> 1.upto(8) do
> array << (v & 1 == 1)
> v >>= 1
> end
>
> Now the array contains 8 boolean (true/false) values corresponding to the
> low 8 bits in the original integer.
>

irb(main):003:0> v = 15; (0..7).map {|i| v[i]}
=> [1, 1, 1, 1, 0, 0, 0, 0]

For more info, ri 'Fixnum#[]'. But there is no #[]= (makes sense if you
think about it).

Another idea, storing the data in a string of length 1:

require 'bit-struct' # http://redshift.sourceforge.net/...

class Bits < BitStruct
unsigned :bit0, 1
unsigned :bit1, 1
unsigned :bit2, 1
unsigned :bit3, 1
unsigned :bit4, 1
unsigned :bit5, 1
unsigned :bit6, 1
unsigned :bit7, 1
end

b = Bits.new

b.bit3 = 1
p b.bit3 # ==> 1
p b.bit4 # ==> 0

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

td

12/28/2011 2:26:00 PM

0

On Dec 28, 9:15 am, "William Sommerwerck" <grizzledgee...@comcast.net>
wrote:
> "Kirk McElhearn" <kirkmc (at) mac (dot) com> wrote in messagenews:4efb1fcb$0$2507$ba4acef3@reader.news.orange.fr...
>
> > On 2011-12-28 13:40:43 +0000, "William Sommerwerck"
> > <grizzledgee...@comcast.net> said:
> >> We are living in a kind of golden age of high-fidelity sound. In terms
> >> of disposable income, today's equipment provides better sound
> >> at a lower cost.
> > Would you agree that today's average audio equipment is equal in
> > quality to top-of-the-line kit of, say, 20 years ago? That's the
> > impression I have, but it's very hard to judge, with age having effects
> > on hearing and all that.
>
> I wouldn't go that far. The average equipment of today, good as it is, is
> not at all up to the highest quality level of 20 years ago. This is
> particularly true of speakers. Current $1000/pair dynamic speakers aren't
> going to match QUADs, Martin-Logans, or Apogees of 20 years ago.

Which cost many thousands more than $1000. even then.

TD

Gerard

12/28/2011 3:13:00 PM

0

td <tomdedeacon@mac.com> typed:
> On Dec 28, 9:15 am, "William Sommerwerck" <grizzledgee...@comcast.net>
> wrote:
> > "Kirk McElhearn" <kirkmc (at) mac (dot) com> wrote in
> > messagenews:4efb1fcb$0$2507$ba4acef3@reader.news.orange.fr...
> >
> > > On 2011-12-28 13:40:43 +0000, "William Sommerwerck"
> > > <grizzledgee...@comcast.net> said:
> > > > We are living in a kind of golden age of high-fidelity sound.
> > > > In terms of disposable income, today's equipment provides
> > > > better sound
> > > > at a lower cost.
> > > Would you agree that today's average audio equipment is equal in
> > > quality to top-of-the-line kit of, say, 20 years ago? That's the
> > > impression I have, but it's very hard to judge, with age having
> > > effects on hearing and all that.
> >
> > I wouldn't go that far. The average equipment of today, good as it
> > is, is
> > not at all up to the highest quality level of 20 years ago. This is
> > particularly true of speakers. Current $1000/pair dynamic speakers
> > aren't
> > going to match QUADs, Martin-Logans, or Apogees of 20 years ago.
>
> Which cost many thousands more than $1000. even then.
>
> TD

Indeed. $1000 speakers are far from SOA.

Kirk McElhearn

12/28/2011 3:13:00 PM

0

On 2011-12-28 14:15:25 +0000, "William Sommerwerck"
<grizzledgeezer@comcast.net> said:

> This is
> particularly true of speakers. Current $1000/pair dynamic speakers aren't
> going to match QUADs, Martin-Logans, or Apogees of 20 years ago.

Has there then not been much of an improvement in speaker technology? I
occasionally see reviews of or ads for a number of odd-shaped speakers,
but are they just gadgets? Has speaker technology hit a wall?

Kirk
--

Kirkville -- http://www.mce...
Writings about more than just Macs
Take Control of iTunes 10: The FAQ: http://www.mce.../itunes

William Sommerwerck

12/28/2011 3:17:00 PM

0

>>> Would you agree that today's average audio equipment is equal in
>>> quality to top-of-the-line kit of, say, 20 years ago? That's the
>>> impression I have, but it's very hard to judge, with age having effects
>>> on hearing and all that.

>> I wouldn't go that far. The average equipment of today, good as it is, is
>> not at all up to the highest quality level of 20 years ago. This is
>> particularly true of speakers. Current $1000/pair dynamic speakers aren't
>> going to match QUADs, Martin-Logans, or Apogees of 20 years ago.

> Which cost many thousands more than $1000, even then.

Not really. The QUAD ESL-63 was $3000/pair, M-L was getting the knack of
making reasonably priced electrostatics, and the Apogee Calipers sold for
$1700/pair. It wasn't /that/ expensive even then, and properly driven -- oh,
brother, were they good.

First of all, I was talking about "disposable income". Ignoring the bad
economic state of this country, people generally have more than they did 20
years, which compensates for inflation.

Broadly speaking, technology has advanced more than the cost to implement
it. The fact that there's horribly expensive equipment out there that
generally does not represent "value for money" has nothing to do with the
fact there is reasonably priced equipment which does.

You can get a really fine system for around $3000. If you think that's
unreasonable -- then you've never heard a good $3000 system.


Romy the Cat

12/28/2011 3:26:00 PM

0

On Dec 28, 8:55 am, Kirk McElhearn <kirkmc (at) mac (dot) com> wrote:
> Would you agree that today's average audio equipment is equal in
> quality to top-of-the-line kit of, say, 20 years ago? That's the
> impression I have, but it's very hard to judge, with age having effects
> on hearing and all that.

Depends, approximately 80% of the today so-called high-end audio (and
I am very much familiar with all of it) do sound inferior to $50 worth
table radio of today or 20 years ago. The progress of truly high
performing audio today in regard to what was available 20, 50 or 80
years back is MUCH more complicated subject and from I have observed
within this community it is way beyond what this forum and it’s
participants will be able to handle. At this forum there are very few
people who are able to express anything worthy about audio and they
post very rarely.

William Sommerwerck

12/28/2011 3:32:00 PM

0

"Kirk McElhearn" <kirkmc (at) mac (dot) com> wrote in message
news:4efb3209$0$5705$ba4acef3@reader.news.orange.fr...

> Has there then not been much of an improvement in speaker technology?
> I occasionally see reviews of or ads for a number of odd-shaped speakers,
> but are they just gadgets? Has speaker technology hit a wall?

There have been improvements, but they're more along the line of cheaper
speakers getting better. You can buy really good speakers for what I
consider a not-unreasonable price.

There is a "wall", and it's the fact that no one make a full-range ionic
speaker. The gap between even good electrostatics and ionic speakers is
wide.


td

12/28/2011 3:49:00 PM

0

On Dec 28, 10:17 am, "William Sommerwerck"
<grizzledgee...@comcast.net> wrote:
> >>> Would you agree that today's average audio equipment is equal in
> >>> quality to top-of-the-line kit of, say, 20 years ago? That's the
> >>> impression I have, but it's very hard to judge, with age having effects
> >>> on hearing and all that.
> >> I wouldn't go that far. The average equipment of today, good as it is, is
> >> not at all up to the highest quality level of 20 years ago. This is
> >> particularly true of speakers. Current $1000/pair dynamic speakers aren't
> >> going to match QUADs, Martin-Logans, or Apogees of 20 years ago.
> > Which cost many thousands more than $1000, even then.
>
> Not really. The QUAD ESL-63 was $3000/pair, M-L was getting the knack of
> making reasonably priced electrostatics, and the Apogee Calipers sold for
> $1700/pair. It wasn't /that/ expensive even then, and properly driven -- oh,
> brother, were they good.

The Calipers were not SOA, Bill. The ESL seemed quite expensive to me.
I paid 47 Pounds Sterling for a brand new pair of the original Quad
electrostatics in the UK in 1970.

THAT was a bargain.


> First of all, I was talking about "disposable income". Ignoring the bad
> economic state of this country, people generally have more than they did 20
> years, which compensates for inflation.

I wonder where you get that notion. I actually think that the vast
majority of people have less today. Much less. The 150,000 dollar
turntables are designed to appeal to the many billionaires with oodles
of disposable income (which is largely not taxed in any significant
way) and very little grey matter.

> Broadly speaking, technology has advanced more than the cost to implement
> it. The fact that there's horribly expensive equipment out there that
> generally does not represent "value for money" has nothing to do with the
> fact there is reasonably priced equipment which does.
>
> You can get a really fine system for around $3000. If you think that's
> unreasonable -- then you've never heard a good $3000 system.

That may, indeed, be reasonable. But it won't buy you a really high
quality audio system.

TD

William Sommerwerck

12/28/2011 4:02:00 PM

0

"td" <tomdedeacon@mac.com> wrote in message
news:d54dbb98-8b10-4fd5-809e-3006a7da3f6f@l24g2000yqm.googlegroups.com...
On Dec 28, 10:17 am, "William Sommerwerck"

>> You can get a really fine system for around $3000. If you think that's
>> unreasonable -- then you've never heard a good $3000 system.

> That may, indeed, be reasonable. But it won't buy you a really high
> quality audio system.

That depends on where you place the "high quality" line. I've heard $3000
systems that are quite good, and significantly better than systems costing
$1000.