~ bitwise NOT operator

~expression

Also known as the one's complement operator or the bitwise complement operator. Converts the expressionto a 32-bit signed integer, and then applies a bitwise one's complement. That is, every bit that is a 0 is set to 1 in the result, and every bit that is a 1 is set to 0 in the result. The result is a signed 32-bit integer.

For example, the hexadecimal value 0x7777 is represented as this binary number: 0111011101110111

The bitwise negation of that hexadecimal value, ~0x7777, is this binary number: 1000100010001000

In hexadecimal, this is 0x8888. Therefore, ~0x7777 is 0x8888.

The most common use of bitwise operators is for representing flag bits (Boolean values packed into 1 bit each).

Floating-point numbers are converted to integers by discarding any digits after the decimal point. Positive integers are converted to an unsigned hexadecimal value with a maximum value of 4294967295 or 0xFFFFFFFF; values larger than the maximum have their most significant digits discarded when they are converted so the value is still 32-bit. Negative numbers are converted to an unsigned hexadecimal value via the two's complement notation, with the minimum being -2147483648 or 0x800000000; numbers less than the minimum are converted to two's complement with greater precision and also have the most significant digits discarded.

The return value is interpreted as a two's complement number with sign, so the return value is an integer in the range -2147483648 to 2147483647.

Availability: ActionScript 1.0; Flash Player 5

Operands

expression : Number - A number.

Returns

Number - The result of the bitwise operation.

Example

The following example demonstrates a use of the bitwise NOT (-) operator with flag bits:

var ReadOnlyFlag:Number = 0x0001; // defines bit 0 as the read-only flag 
var flags:Number = 0; 
trace(flags); 
/* To set the read-only flag in the flags variable, 
 the following code uses the bitwise OR: 
*/
flags |= ReadOnlyFlag; 
trace(flags); 
/* To clear the read-only flag in the flags variable, 
 first construct a mask by using bitwise NOT on ReadOnlyFlag. 
 In the mask, every bit is a 1 except for the read-only flag. 
 Then, use bitwise AND with the mask to clear the read-only flag. 
 The following code constructs the mask and performs the bitwise AND: 
*/ 
flags &= ~ReadOnlyFlag; 
trace(flags); 
// output: 0 1 0 

See also

& bitwise AND operator, &= bitwise AND assignment operator, ^ bitwise XOR operator, ^= bitwise XOR assignment operator, | bitwise OR operator, |= bitwise OR assignment operator


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00001256.html