I have a tree control with checkboxes next to each node that allows for checked, unchecked and middle checked states on the nodes. When clicking a node, the parent and children are updated. The code I found that does the trick uses bit shifting and I’m trying to understand what exactly is happening.
Can someone explain the following code? Or even better, rewrite this code so it is easier to understand?
// click event handler private function eventMessageTree_itemCheckHandler(event:TreeEvent):void { var node:ITreeNode = ITreeNode(event.item); var checkState:uint = TreecheckboxItemRenderer(event.itemRenderer).checkBox.checkState; updateParents(node, checkState); updateChilds(node, checkState); } private function updateChilds(item:ITreeNode, value:uint):void { var middle:Boolean = (value & 2 << 1) == (2 << 1); var selected:Boolean = (value & 1 << 1) == (1 << 1); if (item.children.length > 0 && !middle) { for each (var childNode:ITreeNode in item.children) { childNode.checked = value == (1 << 1 | 2 << 1) ? '2' : value == (1 << 1) ? '1' : '0'; updateChilds(childNode, value); } } } private function updateParents(item:ITreeNode, value:uint): void { var checkValue:String = (value == (1 << 1 | 2 << 1) ? '2' : value == (1 << 1) ? '1' : '0'); var parentNode:ITreeNode = item.parent; if (parentNode) { for each (var childNode:ITreeNode in parentNode.children) { if (childNode.checked != checkValue) { checkValue = '2'; } } parentNode.checked = checkValue; updateParents(parentNode, value); } }
Basically, an expression like this:
Is counter-intuitive. You usually test bits by shifting the constant 1 to the left, since that lets the number of bits shifted be the same as the index of the bit, counting the LSB (rightmost) bit as bit number 0.
Also, there’s no point in testing the result with a == comparison, since it’s always going to be either 0 or non-zero, so you can at least test for something simpler if your language requires that.
In C and C++, which by default interpret a non-zero integer as ‘true’, the comparison is totally unnecessary and just serves to introduce clutter, repetition, and increase the risk of bugs.
I’d write this like so:
The extra parenthesis should help make it clearer how things are grouped. Note how ‘2 << 1’ was rewritten as ‘1 << 2’. This is not just a ‘switch’, you need to compute the proper shift to get the same bit value, 4 in this case.
Of course you could put the bit-testing into subroutines and call them, to make the code more readable.