[AS3.0] 数値・演算子アラカルト (5)

そうだ! AS3.0の数値・演算子まわりを学習しよう! :bouzu:

黒魔術じゃないよ! :boy:

:caution: 要 Flash Player 9 以上


排他的論理和演算子

This movie requires Flash Player 9.0.0
var n:uint = 1;

trace(n);
trace(n = n ^ t);
trace(n = n ^ t);
trace(n = n ^ t);
trace(n = n ^ t);
trace(n = n ^ t);


ビット演算でフラグ

This movie requires Flash Player 9.0.0
var data:Array = new Array();
data.push({cat: cat1, type: "1110"});
data.push({cat: cat2, type: "1000"});
data.push({cat: cat3, type: "1111"});
data.push({cat: cat4, type: "1100"});
data.push({cat: cat5, type: "1100"});
data.push({cat: cat6, type: "1110"});
data.push({cat: cat7, type: "1000"});
data.push({cat: cat8, type: "0000"});

btn8.id = 8;
btn8.addEventListener(MouseEvent.CLICK, click, false, 0, true);
btn4.id = 4;
btn4.addEventListener(MouseEvent.CLICK, click, false, 0, true);
btn2.id = 2;
btn2.addEventListener(MouseEvent.CLICK, click, false, 0, true);
btn1.id = 1;
btn1.addEventListener(MouseEvent.CLICK, click, false, 0, true);

function click(evt:MouseEvent):void {
  var btn:Btn = Btn(evt.target);
  btn.selected = !btn.selected;
  manage();
}
function manage():void {
  var bit8:uint = btn8.selected ? btn8.id : 0;
  var bit4:uint = btn4.selected ? btn4.id : 0;
  var bit2:uint = btn2.selected ? btn2.id : 0;
  var bit1:uint = btn1.selected ? btn1.id : 0;
  var bit:uint = bit8 | bit4 | bit2 | bit1;
  txt.text = ("000" + bit.toString(2)).substr(-4);
  search(bit);
}
function search(bit:uint):void {
  var result:Array = new Array();
  for (var n:uint = 0; n < data.length; n++) {
    var type:String = data[n].type;
    if ((parseInt(type, 2) & bit) == bit) {
      result.push(data.cat);
    }
  }
}


参考資料「馬鹿全 – (AS3)ビット演算を倒す
参考資料「[AS3] ビット演算で属性検索! | romatica : Blog : Archive


[修正] (14/10/08 Wed 15:26)
Tween24 2.1 にバージョンアップ。