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.4.8.js
     24     ECMA Section:       11.4.8 Bitwise NOT Operator
     25     Description:        flip bits up to 32 bits
     26                         no special cases
     27     Author:             christine (at) netscape.com
     28     Date:               7 july 1997
     29 
     30     Data File Fields:
     31         VALUE           value passed as an argument to the ~ operator
     32         E_RESULT        expected return value of ~ VALUE;
     33 
     34     Static variables:
     35         none
     36 
     37 */
     38 
     39     var SECTION = "11.4.8";
     40     var VERSION = "ECMA_1";
     41     startTest();
     42     var testcases = getTestCases();
     43 
     44     writeHeaderToLog( SECTION + " Bitwise Not operator");
     45     test();
     46 
     47 function test() {
     48     for ( tc=0; tc < testcases.length; tc++ ) {
     49         testcases[tc].passed = writeTestCaseResult(
     50                             testcases[tc].expect,
     51                             testcases[tc].actual,
     52                             testcases[tc].description +" = "+
     53                             testcases[tc].actual );
     54 
     55         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
     56     }
     57     stopTest();
     58     return ( testcases );
     59 }
     60 function getTestCases() {
     61     var array = new Array();
     62     var item = 0;
     63 
     64     for ( var i = 0; i < 35; i++ ) {
     65         var p = Math.pow(2,i);
     66 
     67         array[item++] = new TestCase( SECTION, "~"+p,   Not(p),     ~p );
     68 
     69     }
     70     for ( i = 0; i < 35; i++ ) {
     71         var p = -Math.pow(2,i);
     72 
     73         array[item++] = new TestCase( SECTION, "~"+p,   Not(p),     ~p );
     74 
     75     }
     76 
     77     return ( array );
     78 }
     79 function ToInteger( n ) {
     80     n = Number( n );
     81     var sign = ( n < 0 ) ? -1 : 1;
     82 
     83     if ( n != n ) {
     84         return 0;
     85     }
     86     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
     87         return n;
     88     }
     89     return ( sign * Math.floor(Math.abs(n)) );
     90 }
     91 function ToInt32( n ) {
     92     n = Number( n );
     93     var sign = ( n < 0 ) ? -1 : 1;
     94 
     95     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
     96         return 0;
     97     }
     98 
     99     n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
    100     n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
    101 
    102     return ( n );
    103 }
    104 function ToUint32( n ) {
    105     n = Number( n );
    106     var sign = ( n < 0 ) ? -1 : 1;
    107 
    108     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
    109         return 0;
    110     }
    111     n = sign * Math.floor( Math.abs(n) )
    112 
    113     n = n % Math.pow(2,32);
    114 
    115     if ( n < 0 ){
    116         n += Math.pow(2,32);
    117     }
    118 
    119     return ( n );
    120 }
    121 function ToUint16( n ) {
    122     var sign = ( n < 0 ) ? -1 : 1;
    123 
    124     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
    125         return 0;
    126     }
    127 
    128     n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
    129 
    130     if (n <0) {
    131         n += Math.pow(2,16);
    132     }
    133 
    134     return ( n );
    135 }
    136 function Mask( b, n ) {
    137     b = ToUint32BitString( b );
    138     b = b.substring( b.length - n );
    139     b = ToUint32Decimal( b );
    140     return ( b );
    141 }
    142 function ToUint32BitString( n ) {
    143     var b = "";
    144     for ( p = 31; p >=0; p-- ) {
    145         if ( n >= Math.pow(2,p) ) {
    146             b += "1";
    147             n -= Math.pow(2,p);
    148         } else {
    149             b += "0";
    150         }
    151     }
    152     return b;
    153 }
    154 function ToInt32BitString( n ) {
    155     var b = "";
    156     var sign = ( n < 0 ) ? -1 : 1;
    157 
    158     b += ( sign == 1 ) ? "0" : "1";
    159 
    160     for ( p = 30; p >=0; p-- ) {
    161         if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
    162             b += ( sign == 1 ) ? "1" : "0";
    163             n -= sign * Math.pow( 2, p );
    164         } else {
    165             b += ( sign == 1 ) ? "0" : "1";
    166         }
    167     }
    168 
    169     return b;
    170 }
    171 function ToInt32Decimal( bin ) {
    172     var r = 0;
    173     var sign;
    174 
    175     if ( Number(bin.charAt(0)) == 0 ) {
    176         sign = 1;
    177         r = 0;
    178     } else {
    179         sign = -1;
    180         r = -(Math.pow(2,31));
    181     }
    182 
    183     for ( var j = 0; j < 31; j++ ) {
    184         r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
    185     }
    186 
    187     return r;
    188 }
    189 function ToUint32Decimal( bin ) {
    190     var r = 0;
    191 
    192     for ( l = bin.length; l < 32; l++ ) {
    193         bin = "0" + bin;
    194     }
    195 
    196     for ( j = 0; j < 31; j++ ) {
    197         r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
    198     }
    199 
    200     return r;
    201 }
    202 function Not( n ) {
    203     n = ToInt32(n);
    204     n = ToInt32BitString(n);
    205 
    206     r = ""
    207 
    208     for( var l = 0; l < n.length; l++  ) {
    209         r += ( n.charAt(l) == "0" ) ? "1" : "0";
    210     }
    211 
    212     n = ToInt32Decimal(r);
    213 
    214     return n;
    215 }