Tag Archives: bitwise

XOR Conditional

Actionscript:
  1. addEventListener(Event.ENTER_FRAME, onLoop);
  2.  
  3. function onLoop(evt:Event):void {
  4.     var boolA:Boolean = false;
  5.     var boolB:Boolean = false;
  6.    
  7.     if (mouseX> 200){
  8.         boolA = true;
  9.     }else{
  10.         boolA = false;
  11.     }
  12.    
  13.     if (mouseY <200){
  14.         boolB = true
  15.     }else{
  16.         boolB = false;
  17.     }
  18.    
  19.     if (boolA || boolB){
  20.         trace("regular or");
  21.     }
  22.    
  23. // this is the XOR conditional
  24.     if (int(boolA) ^ int(boolB)){
  25.         trace("exclusive or");
  26.     }
  27.    
  28. }

There's an obvious more logical ways to do this. But I thought it was somewhat interesting to see....

So in binary XOR will give us:
0^0 = 0;
0^1 = 1;
1^0 = 1;
1^1 = 0;

What other operator might you use to get the same results?... that's right !=

With regular OR we get:
0|0 = 0;
0|1 = 1;
1|0 = 1;
1|1 = 1;

Posted in misc | Also tagged , , , | Leave a comment

XOR Color Invert

Actionscript:
  1. var yellow:uint = 0xFFFF00;
  2.  
  3. // draws yellow circle
  4. with(graphics) beginFill(yellow), drawCircle(100,100,50);
  5.  
  6. // invert the color using XOR assignment
  7. // yellow becomes 0x0000FF
  8. yellow ^= 0xFFFFFF;
  9.  
  10. // draws blue  circle
  11. with(graphics) beginFill(yellow), drawCircle(200,100,50);

Just a fun use for XOR. You could also do it without XOR assignment:

Actionscript:
  1. with(graphics) beginFill(yellow ^ 0xFFFFFF), drawCircle(200,100,50);

Playing a little with bitwise operators is a good way to make sure you understand them. Try tweaking this trace statement:

Actionscript:
  1. trace((0x543210 ^ 0xFFFFFF).toString(16));

If you'd like to read about bitwise operators, I recommend wikipedia's article.

Posted in Operators, one-liners | Also tagged , | Leave a comment