|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 File Name: 11.10-3.js |
|
9 ECMA Section: 11.10-3 Binary Bitwise Operators: ^ |
|
10 Description: |
|
11 Semantics |
|
12 |
|
13 The production A : A @ B, where @ is one of the bitwise operators in the |
|
14 productions &, ^, | , is evaluated as follows: |
|
15 |
|
16 1. Evaluate A. |
|
17 2. Call GetValue(Result(1)). |
|
18 3. Evaluate B. |
|
19 4. Call GetValue(Result(3)). |
|
20 5. Call ToInt32(Result(2)). |
|
21 6. Call ToInt32(Result(4)). |
|
22 7. Apply the bitwise operator @ to Result(5) and Result(6). The result is |
|
23 a signed 32 bit integer. |
|
24 8. Return Result(7). |
|
25 |
|
26 Author: christine@netscape.com |
|
27 Date: 12 november 1997 |
|
28 */ |
|
29 var SECTION = "11.10-3"; |
|
30 var VERSION = "ECMA_1"; |
|
31 startTest(); |
|
32 |
|
33 writeHeaderToLog( SECTION + " Binary Bitwise Operators: ^"); |
|
34 |
|
35 var shiftexp = 0; |
|
36 var addexp = 0; |
|
37 |
|
38 for ( shiftpow = 0; shiftpow < 33; shiftpow++ ) { |
|
39 shiftexp += Math.pow( 2, shiftpow ); |
|
40 |
|
41 for ( addpow = 0; addpow < 33; addpow++ ) { |
|
42 addexp += Math.pow(2, addpow); |
|
43 |
|
44 new TestCase( SECTION, |
|
45 shiftexp + " ^ " + addexp, |
|
46 Xor( shiftexp, addexp ), |
|
47 shiftexp ^ addexp ); |
|
48 } |
|
49 } |
|
50 |
|
51 test(); |
|
52 |
|
53 function ToInteger( n ) { |
|
54 n = Number( n ); |
|
55 var sign = ( n < 0 ) ? -1 : 1; |
|
56 |
|
57 if ( n != n ) { |
|
58 return 0; |
|
59 } |
|
60 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) { |
|
61 return n; |
|
62 } |
|
63 return ( sign * Math.floor(Math.abs(n)) ); |
|
64 } |
|
65 function ToInt32( n ) { |
|
66 n = Number( n ); |
|
67 var sign = ( n < 0 ) ? -1 : 1; |
|
68 |
|
69 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
|
70 return 0; |
|
71 } |
|
72 |
|
73 n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32); |
|
74 n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n; |
|
75 |
|
76 return ( n ); |
|
77 } |
|
78 function ToUint32( n ) { |
|
79 n = Number( n ); |
|
80 var sign = ( n < 0 ) ? -1 : 1; |
|
81 |
|
82 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
|
83 return 0; |
|
84 } |
|
85 n = sign * Math.floor( Math.abs(n) ) |
|
86 |
|
87 n = n % Math.pow(2,32); |
|
88 |
|
89 if ( n < 0 ){ |
|
90 n += Math.pow(2,32); |
|
91 } |
|
92 |
|
93 return ( n ); |
|
94 } |
|
95 function ToUint16( n ) { |
|
96 var sign = ( n < 0 ) ? -1 : 1; |
|
97 |
|
98 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
|
99 return 0; |
|
100 } |
|
101 |
|
102 n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16); |
|
103 |
|
104 if (n <0) { |
|
105 n += Math.pow(2,16); |
|
106 } |
|
107 |
|
108 return ( n ); |
|
109 } |
|
110 function Mask( b, n ) { |
|
111 b = ToUint32BitString( b ); |
|
112 b = b.substring( b.length - n ); |
|
113 b = ToUint32Decimal( b ); |
|
114 return ( b ); |
|
115 } |
|
116 function ToUint32BitString( n ) { |
|
117 var b = ""; |
|
118 for ( p = 31; p >=0; p-- ) { |
|
119 if ( n >= Math.pow(2,p) ) { |
|
120 b += "1"; |
|
121 n -= Math.pow(2,p); |
|
122 } else { |
|
123 b += "0"; |
|
124 } |
|
125 } |
|
126 return b; |
|
127 } |
|
128 function ToInt32BitString( n ) { |
|
129 var b = ""; |
|
130 var sign = ( n < 0 ) ? -1 : 1; |
|
131 |
|
132 b += ( sign == 1 ) ? "0" : "1"; |
|
133 |
|
134 for ( p = 30; p >=0; p-- ) { |
|
135 if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) { |
|
136 b += ( sign == 1 ) ? "1" : "0"; |
|
137 n -= sign * Math.pow( 2, p ); |
|
138 } else { |
|
139 b += ( sign == 1 ) ? "0" : "1"; |
|
140 } |
|
141 } |
|
142 |
|
143 return b; |
|
144 } |
|
145 function ToInt32Decimal( bin ) { |
|
146 var r = 0; |
|
147 var sign; |
|
148 |
|
149 if ( Number(bin.charAt(0)) == 0 ) { |
|
150 sign = 1; |
|
151 r = 0; |
|
152 } else { |
|
153 sign = -1; |
|
154 r = -(Math.pow(2,31)); |
|
155 } |
|
156 |
|
157 for ( var j = 0; j < 31; j++ ) { |
|
158 r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
|
159 } |
|
160 |
|
161 return r; |
|
162 } |
|
163 function ToUint32Decimal( bin ) { |
|
164 var r = 0; |
|
165 |
|
166 for ( l = bin.length; l < 32; l++ ) { |
|
167 bin = "0" + bin; |
|
168 } |
|
169 |
|
170 for ( j = 0; j < 31; j++ ) { |
|
171 r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
|
172 |
|
173 } |
|
174 |
|
175 return r; |
|
176 } |
|
177 function And( s, a ) { |
|
178 s = ToInt32( s ); |
|
179 a = ToInt32( a ); |
|
180 |
|
181 var bs = ToInt32BitString( s ); |
|
182 var ba = ToInt32BitString( a ); |
|
183 |
|
184 var result = ""; |
|
185 |
|
186 for ( var bit = 0; bit < bs.length; bit++ ) { |
|
187 if ( bs.charAt(bit) == "1" && ba.charAt(bit) == "1" ) { |
|
188 result += "1"; |
|
189 } else { |
|
190 result += "0"; |
|
191 } |
|
192 } |
|
193 return ToInt32Decimal(result); |
|
194 } |
|
195 function Xor( s, a ) { |
|
196 s = ToInt32( s ); |
|
197 a = ToInt32( a ); |
|
198 |
|
199 var bs = ToInt32BitString( s ); |
|
200 var ba = ToInt32BitString( a ); |
|
201 |
|
202 var result = ""; |
|
203 |
|
204 for ( var bit = 0; bit < bs.length; bit++ ) { |
|
205 if ( (bs.charAt(bit) == "1" && ba.charAt(bit) == "0") || |
|
206 (bs.charAt(bit) == "0" && ba.charAt(bit) == "1") |
|
207 ) { |
|
208 result += "1"; |
|
209 } else { |
|
210 result += "0"; |
|
211 } |
|
212 } |
|
213 |
|
214 return ToInt32Decimal(result); |
|
215 } |
|
216 function Or( s, a ) { |
|
217 s = ToInt32( s ); |
|
218 a = ToInt32( a ); |
|
219 |
|
220 var bs = ToInt32BitString( s ); |
|
221 var ba = ToInt32BitString( a ); |
|
222 |
|
223 var result = ""; |
|
224 |
|
225 for ( var bit = 0; bit < bs.length; bit++ ) { |
|
226 if ( bs.charAt(bit) == "1" || ba.charAt(bit) == "1" ) { |
|
227 result += "1"; |
|
228 } else { |
|
229 result += "0"; |
|
230 } |
|
231 } |
|
232 |
|
233 return ToInt32Decimal(result); |
|
234 } |