Click to See Complete Forum and Search --> : bit indexing


boojum
07-01-2003, 02:35 PM
i have a passed number that was an array index, i want to use it to switch its corresponding bit in a flag. is there some way to directly index the bits of a byte, or do i have to use an array filled with powers of two?

jeffmott
07-01-2003, 05:05 PM
function switch_bit(num, pos)
{
return num ^ Math.pow(2, pos - 1);
}Note that this will only work as expected with primitive data types.