Home | History | Annotate | Download | only in Expressions
      1 /* The contents of this file are subject to the Netscape Public
      2  * License Version 1.1 (the "License"); you may not use this file
      3  * except in compliance with the License. You may obtain a copy of
      4  * the License at http://www.mozilla.org/NPL/
      5  *
      6  * Software distributed under the License is distributed on an "AS
      7  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      8  * implied. See the License for the specific language governing
      9  * rights and limitations under the License.
     10  *
     11  * The Original Code is Mozilla Communicator client code, released March
     12  * 31, 1998.
     13  *
     14  * The Initial Developer of the Original Code is Netscape Communications
     15  * Corporation. Portions created by Netscape are
     16  * Copyright (C) 1998 Netscape Communications Corporation. All
     17  * Rights Reserved.
     18  *
     19  * Contributor(s):
     20  *
     21  */
     22 /**
     23     File Name:          11.10-3.js
     24     ECMA Section:       11.10-3 Binary Bitwise Operators:  ^
     25     Description:
     26     Semantics
     27 
     28     The production A : A @ B, where @ is one of the bitwise operators in the
     29     productions &, ^, | , is evaluated as follows:
     30 
     31     1.  Evaluate A.
     32     2.  Call GetValue(Result(1)).
     33     3.  Evaluate B.
     34     4.  Call GetValue(Result(3)).
     35     5.  Call ToInt32(Result(2)).
     36     6.  Call ToInt32(Result(4)).
     37     7.  Apply the bitwise operator @ to Result(5) and Result(6). The result is
     38         a signed 32 bit integer.
     39     8.  Return Result(7).
     40 
     41     Author:             christine (at) netscape.com
     42     Date:               12 november 1997
     43 */
     44     var SECTION = "11.10-3";
     45     var VERSION = "ECMA_1";
     46     startTest();
     47 
     48     var testcases = getTestCases();
     49 
     50     writeHeaderToLog( SECTION + " Binary Bitwise Operators:  ^");
     51     test();
     52 
     53 function test() {
     54     for ( tc=0; tc < testcases.length; tc++ ) {
     55         testcases[tc].passed = writeTestCaseResult(
     56                             testcases[tc].expect,
     57                             testcases[tc].actual,
     58                             testcases[tc].description +" = "+
     59                             testcases[tc].actual );
     60 
     61         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
     62     }
     63     stopTest();
     64     return ( testcases );
     65 }
     66 function getTestCases() {
     67     var array = new Array();
     68     var item = 0;
     69     var shiftexp = 0;
     70     var addexp = 0;
     71 
     72     for ( shiftpow = 0; shiftpow < 33; shiftpow++ ) {
     73         shiftexp += Math.pow( 2, shiftpow );
     74 
     75         for ( addpow = 0; addpow < 33; addpow++ ) {
     76             addexp += Math.pow(2, addpow);
     77 
     78             array[item++] = new TestCase( SECTION,
     79                                     shiftexp + " ^ " + addexp,
     80                                     Xor( shiftexp, addexp ),
     81                                     shiftexp ^ addexp );
     82         }
     83     }
     84 
     85     return ( array );
     86 }
     87 function ToInteger( n ) {
     88     n = Number( n );
     89     var sign = ( n < 0 ) ? -1 : 1;
     90 
     91     if ( n != n ) {
     92         return 0;
     93     }
     94     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
     95         return n;
     96     }
     97     return ( sign * Math.floor(Math.abs(n)) );
     98 }
     99 function ToInt32( n ) {
    100     n = Number( n );
    101     var sign = ( n < 0 ) ? -1 : 1;
    102 
    103     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
    104         return 0;
    105     }
    106 
    107     n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
    108     n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
    109 
    110     return ( n );
    111 }
    112 function ToUint32( n ) {
    113     n = Number( n );
    114     var sign = ( n < 0 ) ? -1 : 1;
    115 
    116     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
    117         return 0;
    118     }
    119     n = sign * Math.floor( Math.abs(n) )
    120 
    121     n = n % Math.pow(2,32);
    122 
    123     if ( n < 0 ){
    124         n += Math.pow(2,32);
    125     }
    126 
    127     return ( n );
    128 }
    129 function ToUint16( n ) {
    130     var sign = ( n < 0 ) ? -1 : 1;
    131 
    132     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
    133         return 0;
    134     }
    135 
    136     n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
    137 
    138     if (n <0) {
    139         n += Math.pow(2,16);
    140     }
    141 
    142     return ( n );
    143 }
    144 function Mask( b, n ) {
    145     b = ToUint32BitString( b );
    146     b = b.substring( b.length - n );
    147     b = ToUint32Decimal( b );
    148     return ( b );
    149 }
    150 function ToUint32BitString( n ) {
    151     var b = "";
    152     for ( p = 31; p >=0; p-- ) {
    153         if ( n >= Math.pow(2,p) ) {
    154             b += "1";
    155             n -= Math.pow(2,p);
    156         } else {
    157             b += "0";
    158         }
    159     }
    160     return b;
    161 }
    162 function ToInt32BitString( n ) {
    163     var b = "";
    164     var sign = ( n < 0 ) ? -1 : 1;
    165 
    166     b += ( sign == 1 ) ? "0" : "1";
    167 
    168     for ( p = 30; p >=0; p-- ) {
    169         if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
    170             b += ( sign == 1 ) ? "1" : "0";
    171             n -= sign * Math.pow( 2, p );
    172         } else {
    173             b += ( sign == 1 ) ? "0" : "1";
    174         }
    175     }
    176 
    177     return b;
    178 }
    179 function ToInt32Decimal( bin ) {
    180     var r = 0;
    181     var sign;
    182 
    183     if ( Number(bin.charAt(0)) == 0 ) {
    184         sign = 1;
    185         r = 0;
    186     } else {
    187         sign = -1;
    188         r = -(Math.pow(2,31));
    189     }
    190 
    191     for ( var j = 0; j < 31; j++ ) {
    192         r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
    193     }
    194 
    195     return r;
    196 }
    197 function ToUint32Decimal( bin ) {
    198     var r = 0;
    199 
    200     for ( l = bin.length; l < 32; l++ ) {
    201         bin = "0" + bin;
    202     }
    203 
    204     for ( j = 0; j < 31; j++ ) {
    205         r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
    206 
    207     }
    208 
    209     return r;
    210 }
    211 function And( s, a ) {
    212     s = ToInt32( s );
    213     a = ToInt32( a );
    214 
    215     var bs = ToInt32BitString( s );
    216     var ba = ToInt32BitString( a );
    217 
    218     var result = "";
    219 
    220     for ( var bit = 0; bit < bs.length; bit++ ) {
    221         if ( bs.charAt(bit) == "1" && ba.charAt(bit) == "1" ) {
    222             result += "1";
    223         } else {
    224             result += "0";
    225         }
    226     }
    227     return ToInt32Decimal(result);
    228 }
    229 function Xor( s, a ) {
    230     s = ToInt32( s );
    231     a = ToInt32( a );
    232 
    233     var bs = ToInt32BitString( s );
    234     var ba = ToInt32BitString( a );
    235 
    236     var result = "";
    237 
    238     for ( var bit = 0; bit < bs.length; bit++ ) {
    239         if ( (bs.charAt(bit) == "1" && ba.charAt(bit) == "0") ||
    240              (bs.charAt(bit) == "0" && ba.charAt(bit) == "1")
    241            ) {
    242             result += "1";
    243         } else {
    244             result += "0";
    245         }
    246     }
    247 
    248     return ToInt32Decimal(result);
    249 }
    250 function Or( s, a ) {
    251     s = ToInt32( s );
    252     a = ToInt32( a );
    253 
    254     var bs = ToInt32BitString( s );
    255     var ba = ToInt32BitString( a );
    256 
    257     var result = "";
    258 
    259     for ( var bit = 0; bit < bs.length; bit++ ) {
    260         if ( bs.charAt(bit) == "1" || ba.charAt(bit) == "1" ) {
    261             result += "1";
    262         } else {
    263             result += "0";
    264         }
    265     }
    266 
    267     return ToInt32Decimal(result);
    268 }
    269