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/
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.
11 * The Original Code is Mozilla Communicator client code, released March
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
24 ECMA Section: 11.4.8 Bitwise NOT Operator
25 Description: flip bits up to 32 bits
27 Author: christine@netscape.com
31 VALUE value passed as an argument to the ~ operator
32 E_RESULT expected return value of ~ VALUE;
39 var SECTION = "11.4.8";
40 var VERSION = "ECMA_1";
42 var testcases = getTestCases();
44 writeHeaderToLog( SECTION + " Bitwise Not operator");
48 for ( tc=0; tc < testcases.length; tc++ ) {
49 testcases[tc].passed = writeTestCaseResult(
52 testcases[tc].description +" = "+
53 testcases[tc].actual );
55 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
60 function getTestCases() {
61 var array = new Array();
64 for ( var i = 0; i < 35; i++ ) {
65 var p = Math.pow(2,i);
67 array[item++] = new TestCase( SECTION, "~"+p, Not(p), ~p );
70 for ( i = 0; i < 35; i++ ) {
71 var p = -Math.pow(2,i);
73 array[item++] = new TestCase( SECTION, "~"+p, Not(p), ~p );
79 function ToInteger( n ) {
81 var sign = ( n < 0 ) ? -1 : 1;
86 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
89 return ( sign * Math.floor(Math.abs(n)) );
91 function ToInt32( n ) {
93 var sign = ( n < 0 ) ? -1 : 1;
95 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
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;
104 function ToUint32( n ) {
106 var sign = ( n < 0 ) ? -1 : 1;
108 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
111 n = sign * Math.floor( Math.abs(n) )
113 n = n % Math.pow(2,32);
121 function ToUint16( n ) {
122 var sign = ( n < 0 ) ? -1 : 1;
124 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
128 n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
136 function Mask( b, n ) {
137 b = ToUint32BitString( b );
138 b = b.substring( b.length - n );
139 b = ToUint32Decimal( b );
142 function ToUint32BitString( n ) {
144 for ( p = 31; p >=0; p-- ) {
145 if ( n >= Math.pow(2,p) ) {
154 function ToInt32BitString( n ) {
156 var sign = ( n < 0 ) ? -1 : 1;
158 b += ( sign == 1 ) ? "0" : "1";
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 );
165 b += ( sign == 1 ) ? "0" : "1";
171 function ToInt32Decimal( bin ) {
175 if ( Number(bin.charAt(0)) == 0 ) {
180 r = -(Math.pow(2,31));
183 for ( var j = 0; j < 31; j++ ) {
184 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
189 function ToUint32Decimal( bin ) {
192 for ( l = bin.length; l < 32; l++ ) {
196 for ( j = 0; j < 31; j++ ) {
197 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
204 n = ToInt32BitString(n);
208 for( var l = 0; l < n.length; l++ ) {
209 r += ( n.charAt(l) == "0" ) ? "1" : "0";
212 n = ToInt32Decimal(r);