Flash / Flex / ActionScript/Language/Bitwise Operators — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:15, 26 мая 2010

Bitwise Operators in Actionscript

 
Operator      Name 
&             Bitwise AND 
|             Bitwise OR 
^             Bitwise XOR (exclusive or) 
<<            Bitwise Left Shift 
>>            Bitwise Right Shift 
>>>           Bitwise Unsigned Right Shift 
~             Bitwise Not



Use Bitwise Unsigned Right Shift

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
            var x:Number = -12;
            var y:Number = x >>> 2; 
            trace(y);                // y = 1073741821
            
            //x      1111 1111 1111 1111 1111 1111 1111 1011 
            //y      0011 1111 1111 1111 1111 1111 1111 1101 
    }
  }
}



Using Bitwise AND

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var x:Number = 1234;
        var y:Number = 4567;
        var z:Number = x & y;
        //x      0000 0000 0000 0000 0000 0100 1101 0010 
        //y      0000 0000 0000 0000 0001 0001 1101 0111 
        //z      0000 0000 0000 0000 0000 0000 1101 0010 
        
        trace(z); //210
    }
  }
}



Using Bitwise Left Shift

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var x:Number = 3;
        var y:Number = x << 2;
        
        trace(y); //12
        
        //x      0000 0000 0000 0000 0000 0000 0000 0011 
        //y      0000 0000 0000 0000 0000 0000 0000 1100 
    }
  }
}



Using Bitwise NOT

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var x:Number = ~6;
        
        trace(x);  //-7
        
        x = -(6+1);
        
        trace(x);  //-7
    }
  }
}



Using Bitwise Operations with Color

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var nRed:Number = 123;  
        var nGreen:Number = 123; 
        var nBlue:Number = 123;  
        
        var nRGB:Number = nRed<<16 | nGreen<<8 | nBlue;
        var nRedPart:Number = nRGB >> 16 & 0xFF;
        var nGreenPart:Number = nRGB >> 8 & 0xFF;
        var nBluePart:Number = nRGB & 0xFF;
    }
  }
}



Using Bitwise OR

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var x:Number = 1234;
        var y:Number = 4567;
        var z:Number = x | y;
        
        trace(z);//5591
        
        //x      0000 0000 0000 0000 0000 0100 1101 0010 
        //y      0000 0000 0000 0000 0001 0001 1101 0111 
        //z      0000 0000 0000 0000 0001 0001 1101 0111 
    }
  }
}



Using Bitwise Right Shift

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var x:Number = 12;
        var y:Number = x >> 2;
        
        trace(y); //3
        
        //x      0000 0000 0000 0000 0000 0000 0000 1100 
        //y      0000 0000 0000 0000 0000 0000 0000 0011 
    }
  }
}



Using Bitwise XOR

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var x:Number = 1234;
        var y:Number = 4567;
        var z:Number = x ^ y;
        trace(z); //5381
        
        //x      0000 0000 0000 0000 0000 0100 1101 0010 
        //y      0000 0000 0000 0000 0001 0001 1101 0111 
        //z      0000 0000 0000 0000 0001 0001 0000 0101 
    }
  }
}



Using Flag Variables

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var UP:Number = 1;
        var DRAG:Number = 2;
        var PLAYING:Number = 4;
        var VISIBLE:Number = 8;
        var nFlag:Number |= VISIBLE; 
        trace(nFlag);   
        
        nFlag &= ~PLAYING; // Make it stop playing
        trace(nFlag);   
        nFlag ^= DRAG; // Toggle draggability on and off
        trace(nFlag); 
    }
  }
}