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.10-3 Binary Bitwise Operators: ^
28 The production A : A @ B, where @ is one of the bitwise operators in the
29 productions &, ^, | , is evaluated as follows:
32 2. Call GetValue(Result(1)).
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.
41 Author: christine@netscape.com
42 Date: 12 november 1997
44 var SECTION = "11.10-3";
45 var VERSION = "ECMA_1";
48 var testcases = getTestCases();
50 writeHeaderToLog( SECTION + " Binary Bitwise Operators: ^");
54 for ( tc=0; tc < testcases.length; tc++ ) {
55 testcases[tc].passed = writeTestCaseResult(
58 testcases[tc].description +" = "+
59 testcases[tc].actual );
61 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
66 function getTestCases() {
67 var array = new Array();
72 for ( shiftpow = 0; shiftpow < 33; shiftpow++ ) {
73 shiftexp += Math.pow( 2, shiftpow );
75 for ( addpow = 0; addpow < 33; addpow++ ) {
76 addexp += Math.pow(2, addpow);
78 array[item++] = new TestCase( SECTION,
79 shiftexp + " ^ " + addexp,
80 Xor( shiftexp, addexp ),
87 function ToInteger( n ) {
89 var sign = ( n < 0 ) ? -1 : 1;
94 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
97 return ( sign * Math.floor(Math.abs(n)) );
99 function ToInt32( n ) {
101 var sign = ( n < 0 ) ? -1 : 1;
103 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
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;
112 function ToUint32( n ) {
114 var sign = ( n < 0 ) ? -1 : 1;
116 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
119 n = sign * Math.floor( Math.abs(n) )
121 n = n % Math.pow(2,32);
129 function ToUint16( n ) {
130 var sign = ( n < 0 ) ? -1 : 1;
132 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
136 n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
144 function Mask( b, n ) {
145 b = ToUint32BitString( b );
146 b = b.substring( b.length - n );
147 b = ToUint32Decimal( b );
150 function ToUint32BitString( n ) {
152 for ( p = 31; p >=0; p-- ) {
153 if ( n >= Math.pow(2,p) ) {
162 function ToInt32BitString( n ) {
164 var sign = ( n < 0 ) ? -1 : 1;
166 b += ( sign == 1 ) ? "0" : "1";
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 );
173 b += ( sign == 1 ) ? "0" : "1";
179 function ToInt32Decimal( bin ) {
183 if ( Number(bin.charAt(0)) == 0 ) {
188 r = -(Math.pow(2,31));
191 for ( var j = 0; j < 31; j++ ) {
192 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
197 function ToUint32Decimal( bin ) {
200 for ( l = bin.length; l < 32; l++ ) {
204 for ( j = 0; j < 31; j++ ) {
205 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
211 function And( s, a ) {
215 var bs = ToInt32BitString( s );
216 var ba = ToInt32BitString( a );
220 for ( var bit = 0; bit < bs.length; bit++ ) {
221 if ( bs.charAt(bit) == "1" && ba.charAt(bit) == "1" ) {
227 return ToInt32Decimal(result);
229 function Xor( s, a ) {
233 var bs = ToInt32BitString( s );
234 var ba = ToInt32BitString( a );
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")
248 return ToInt32Decimal(result);
250 function Or( s, a ) {
254 var bs = ToInt32BitString( s );
255 var ba = ToInt32BitString( a );
259 for ( var bit = 0; bit < bs.length; bit++ ) {
260 if ( bs.charAt(bit) == "1" || ba.charAt(bit) == "1" ) {
267 return ToInt32Decimal(result);