"Bit manipulation can be tricky," writes Nathan, "especially if you have no familiarity with bitwise operators or logic."

"At least, that's what my smarter-than-built-in-language-features colleague must have thought when he authored `setBit`. Fortunately, his code was the only one that utilized this function, as it doesn't quite work as advertised."

```/*
*    setBit(\$val, \$bit, \$switch)
*
*    This will switch all the bits specified by \$bit to on or
*    off in \$val depending on \$switch. So, if \$val = 8, and
*    \$bit = 4, \$switch is true, it will turn on the 4 bit,
*    and \$val will end up being 12; Now, if \$val = 12, and \$bit
*    is 5, if \$switch is true, \$val will be 13, if false \$val
*    will be 8. So, all the bits of \$bit is turned on or off
*
*/
function setBit(\$val, \$bit, \$switch = true) {
\$val = (int) \$val;
\$bit = (int) \$bit;

// set some strings that humans would consider false
// but would be converted to true if converted by PHP
if (is_string(\$switch)) {
switch (\$switch) {
case 'false' :
case 'down' :
case 'off' :
case 'not' :
case '0' :
case '' :
\$switch = false;
break;

default :
\$switch = true;
break;
}
}

if (\$switch) { // we are turning the bits on
\$newval = \$val | \$bit; // val OR bit
}
else { // we are turning the bits off
\$newval = \$val & ( ~ \$bit); // val AND ( NOT bit)
}

return \$newval;
}``` [Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!