Christopher Benson-Manica said:
I have a situation where I have many (more than 32) boolean flags:
var foo=true;
var bar=false;
var baz=false;
// etc.
At various points in the script, these flags may be set or unset.
There is a point where an action is to be taken only if all the flags
are false. I also need to debug this check of all flags - i.e., print
out the value of all 32+ of these flags. I'd like to find something
besides a monstrous conditional - for example, using an integer and
storing these flags as bits in it, except that since there are more
than 32 of these flags an integer will not contain all of them. Any
suggestions would be appreciated.
To store your flags as bits, just use as many integers as you need in an
array to accomodate all the bits.
You can index the relevant integer containing the desired flag thus:
rray[ flagIndex / 32 ] , then just perform a suitable bitwise operation to
read/manipulate the desired bit.
<SCRIPT type='text/javascript'>
function boolManager(boolCount)
{
this.boolCount=boolCount;
this.boolStore=[ 1 + boolCount/32 ];
}
boolManager.prototype.readBool=function(ind)
{
return this.boolStore[ ind/32 ] & 1<<Math.floor(ind % 32);
}
boolManager.prototype.setBool=function(ind, state)
{
state ? ( this.boolStore[ ind/32 ] |= 1 << ind % 32 )
: ( this.boolStore[ ind/32 ] &= ~( 1 << ind % 32 ) );
}
boolManager.prototype.flipBool=function(ind)
{
this.boolStore[ ind/32 ] ^= 1 << ind % 32
}
boolManager.prototype.listBools=function()
{
for(var i=0; i<this.boolCount; i++) // read & display all flags
document.write('<BR>'+ i + " : " + (this.readBool(i)?"True":"False") );
}
boolManager.prototype.allBoolsFalse=function()
{
var rv;
for(var i=0; i<this.boolCount && !(rv=this.readBool(i)) ; i++)
;
return !rv;
}
boolManager.prototype.allBoolsTrue=function()
{
var rv;
for(var i=0; i<this.boolCount && (rv=this.readBool(i)) ; i++)
;
return rv;
}
// ========= Demonstration Code =======
var boolCount=40, // # of booleans in use
setFlags=[ 6, 13, 26, 27, 34, 38 ], // some arbitrary booleans to be
set
myBools=new boolManager(boolCount);
document.write("Set 6, 13, 26, 27, 34 & 38 <BR><BR>");
for(var i=0; i<setFlags.length; i++) // set some booleans to true
myBools.setBool(setFlags
, true);
myBools.listBools(); // list results
document.write("<BR><BR>Flip all flags :<BR>");
for(var i=0; i<boolCount; i++) // invert all flags
myBools.flipBool(i);
myBools.listBools();
document.write("<BR><BR>Reset all flags :<BR>");
for(var i=0; i<boolCount; i++) // reset all flags
myBools.setBool(i, false);
myBools.listBools();
document.write("<BR><BR>Test for all false: " +
(myBools.allBoolsFalse()?"Yes":"No") );
document.write("<BR><BR>Set flag 0 true <BR>");
myBools.setBool(0, true);
document.write("<BR>Test for all false: " +
(myBools.allBoolsFalse()?"Yes":"No") );
document.write("<BR><BR>Set all flags true:<BR>");
for(var i=0; i<boolCount; i++) // Set all flags
myBools.setBool(i, true);
document.write("<BR>Test for all true: " +
(myBools.allBoolsTrue()?"Yes":"No") );
document.write("<BR><BR>Set flag 20 false <BR>");
myBools.setBool(20, false);
document.write("<BR>Test for all true: " +
(myBools.allBoolsTrue()?"Yes":"No") );
document.write("<BR><BR>Sorted.");
</SCRIPT>