|
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-1.js |
|
9 ECMA Section: 11.10-1 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-1"; |
|
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 for ( shiftpow = 0; shiftpow < 1; shiftpow++ ) { |
|
40 shiftexp += Math.pow( 2, shiftpow ); |
|
41 |
|
42 for ( addpow = 0; addpow < 33; addpow++ ) { |
|
43 addexp += Math.pow(2, addpow); |
|
44 |
|
45 new TestCase( SECTION, |
|
46 shiftexp + " & " + addexp, |
|
47 And( shiftexp, addexp ), |
|
48 shiftexp & addexp ); |
|
49 } |
|
50 } |
|
51 |
|
52 test(); |
|
53 |
|
54 function ToInteger( n ) { |
|
55 n = Number( n ); |
|
56 var sign = ( n < 0 ) ? -1 : 1; |
|
57 |
|
58 if ( n != n ) { |
|
59 return 0; |
|
60 } |
|
61 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) { |
|
62 return n; |
|
63 } |
|
64 return ( sign * Math.floor(Math.abs(n)) ); |
|
65 } |
|
66 function ToInt32( n ) { |
|
67 n = Number( n ); |
|
68 var sign = ( n < 0 ) ? -1 : 1; |
|
69 |
|
70 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
|
71 return 0; |
|
72 } |
|
73 |
|
74 n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32); |
|
75 n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n; |
|
76 |
|
77 return ( n ); |
|
78 } |
|
79 function ToUint32( n ) { |
|
80 n = Number( n ); |
|
81 var sign = ( n < 0 ) ? -1 : 1; |
|
82 |
|
83 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
|
84 return 0; |
|
85 } |
|
86 n = sign * Math.floor( Math.abs(n) ) |
|
87 |
|
88 n = n % Math.pow(2,32); |
|
89 |
|
90 if ( n < 0 ){ |
|
91 n += Math.pow(2,32); |
|
92 } |
|
93 |
|
94 return ( n ); |
|
95 } |
|
96 function ToUint16( n ) { |
|
97 var sign = ( n < 0 ) ? -1 : 1; |
|
98 |
|
99 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
|
100 return 0; |
|
101 } |
|
102 |
|
103 n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16); |
|
104 |
|
105 if (n <0) { |
|
106 n += Math.pow(2,16); |
|
107 } |
|
108 |
|
109 return ( n ); |
|
110 } |
|
111 function Mask( b, n ) { |
|
112 b = ToUint32BitString( b ); |
|
113 b = b.substring( b.length - n ); |
|
114 b = ToUint32Decimal( b ); |
|
115 return ( b ); |
|
116 } |
|
117 function ToUint32BitString( n ) { |
|
118 var b = ""; |
|
119 for ( p = 31; p >=0; p-- ) { |
|
120 if ( n >= Math.pow(2,p) ) { |
|
121 b += "1"; |
|
122 n -= Math.pow(2,p); |
|
123 } else { |
|
124 b += "0"; |
|
125 } |
|
126 } |
|
127 return b; |
|
128 } |
|
129 function ToInt32BitString( n ) { |
|
130 var b = ""; |
|
131 var sign = ( n < 0 ) ? -1 : 1; |
|
132 |
|
133 b += ( sign == 1 ) ? "0" : "1"; |
|
134 |
|
135 for ( p = 30; p >=0; p-- ) { |
|
136 if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) { |
|
137 b += ( sign == 1 ) ? "1" : "0"; |
|
138 n -= sign * Math.pow( 2, p ); |
|
139 } else { |
|
140 b += ( sign == 1 ) ? "0" : "1"; |
|
141 } |
|
142 } |
|
143 |
|
144 return b; |
|
145 } |
|
146 function ToInt32Decimal( bin ) { |
|
147 var r = 0; |
|
148 var sign; |
|
149 |
|
150 if ( Number(bin.charAt(0)) == 0 ) { |
|
151 sign = 1; |
|
152 r = 0; |
|
153 } else { |
|
154 sign = -1; |
|
155 r = -(Math.pow(2,31)); |
|
156 } |
|
157 |
|
158 for ( var j = 0; j < 31; j++ ) { |
|
159 r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
|
160 } |
|
161 |
|
162 return r; |
|
163 } |
|
164 function ToUint32Decimal( bin ) { |
|
165 var r = 0; |
|
166 |
|
167 |
|
168 for ( l = bin.length; l < 32; l++ ) { |
|
169 bin = "0" + bin; |
|
170 } |
|
171 |
|
172 for ( j = 0; j < 31; j++ ) { |
|
173 r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
|
174 |
|
175 } |
|
176 |
|
177 return r; |
|
178 } |
|
179 function And( s, a ) { |
|
180 s = ToInt32( s ); |
|
181 a = ToInt32( a ); |
|
182 |
|
183 var bs = ToInt32BitString( s ); |
|
184 var ba = ToInt32BitString( a ); |
|
185 |
|
186 var result = ""; |
|
187 |
|
188 for ( var bit = 0; bit < bs.length; bit++ ) { |
|
189 if ( bs.charAt(bit) == "1" && ba.charAt(bit) == "1" ) { |
|
190 result += "1"; |
|
191 } else { |
|
192 result += "0"; |
|
193 } |
|
194 } |
|
195 return ToInt32Decimal(result); |
|
196 } |
|
197 function Xor( s, a ) { |
|
198 s = ToInt32( s ); |
|
199 a = ToInt32( a ); |
|
200 |
|
201 var bs = ToInt32BitString( s ); |
|
202 var ba = ToInt32BitString( a ); |
|
203 |
|
204 var result = ""; |
|
205 |
|
206 for ( var bit = 0; bit < bs.length; bit++ ) { |
|
207 if ( (bs.charAt(bit) == "1" && ba.charAt(bit) == "0") || |
|
208 (bs.charAt(bit) == "0" && ba.charAt(bit) == "1") |
|
209 ) { |
|
210 result += "1"; |
|
211 } else { |
|
212 result += "0"; |
|
213 } |
|
214 } |
|
215 |
|
216 return ToInt32Decimal(result); |
|
217 } |
|
218 function Or( s, a ) { |
|
219 s = ToInt32( s ); |
|
220 a = ToInt32( a ); |
|
221 |
|
222 var bs = ToInt32BitString( s ); |
|
223 var ba = ToInt32BitString( a ); |
|
224 |
|
225 var result = ""; |
|
226 |
|
227 for ( var bit = 0; bit < bs.length; bit++ ) { |
|
228 if ( bs.charAt(bit) == "1" || ba.charAt(bit) == "1" ) { |
|
229 result += "1"; |
|
230 } else { |
|
231 result += "0"; |
|
232 } |
|
233 } |
|
234 |
|
235 return ToInt32Decimal(result); |
|
236 } |