|
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.7.3.js |
|
9 ECMA Section: 11.7.3 The unsigned right shift operator ( >>> ) |
|
10 Description: |
|
11 11.7.3 The unsigned right shift operator ( >>> ) |
|
12 Performs a zero-filling bitwise right shift operation on the left argument |
|
13 by the amount specified by the right argument. |
|
14 |
|
15 The production ShiftExpression : ShiftExpression >>> AdditiveExpression is |
|
16 evaluated as follows: |
|
17 |
|
18 1. Evaluate ShiftExpression. |
|
19 2. Call GetValue(Result(1)). |
|
20 3. Evaluate AdditiveExpression. |
|
21 4. Call GetValue(Result(3)). |
|
22 5. Call ToUint32(Result(2)). |
|
23 6. Call ToUint32(Result(4)). |
|
24 7. Mask out all but the least significant 5 bits of Result(6), that is, |
|
25 compute Result(6) & 0x1F. |
|
26 8. Perform zero-filling right shift of Result(5) by Result(7) bits. |
|
27 Vacated bits are filled with zero. The result is an unsigned 32 bit |
|
28 integer. |
|
29 9. Return Result(8). |
|
30 |
|
31 Author: christine@netscape.com |
|
32 Date: 12 november 1997 |
|
33 */ |
|
34 var SECTION = "11.7.3"; |
|
35 var VERSION = "ECMA_1"; |
|
36 startTest(); |
|
37 |
|
38 writeHeaderToLog( SECTION + " The unsigned right shift operator ( >>> )"); |
|
39 |
|
40 var addexp = 0; |
|
41 var power = 0; |
|
42 |
|
43 for ( power = 0; power <= 32; power++ ) { |
|
44 shiftexp = Math.pow( 2, power ); |
|
45 |
|
46 for ( addexp = 0; addexp <= 32; addexp++ ) { |
|
47 new TestCase( SECTION, |
|
48 shiftexp + " >>> " + addexp, |
|
49 UnsignedRightShift( shiftexp, addexp ), |
|
50 shiftexp >>> addexp ); |
|
51 } |
|
52 } |
|
53 |
|
54 test(); |
|
55 |
|
56 |
|
57 function ToInteger( n ) { |
|
58 n = Number( n ); |
|
59 var sign = ( n < 0 ) ? -1 : 1; |
|
60 |
|
61 if ( n != n ) { |
|
62 return 0; |
|
63 } |
|
64 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) { |
|
65 return n; |
|
66 } |
|
67 return ( sign * Math.floor(Math.abs(n)) ); |
|
68 } |
|
69 function ToInt32( n ) { |
|
70 n = Number( n ); |
|
71 var sign = ( n < 0 ) ? -1 : 1; |
|
72 |
|
73 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
|
74 return 0; |
|
75 } |
|
76 |
|
77 n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32); |
|
78 n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n; |
|
79 |
|
80 return ( n ); |
|
81 } |
|
82 function ToUint32( n ) { |
|
83 n = Number( n ); |
|
84 var sign = ( n < 0 ) ? -1 : 1; |
|
85 |
|
86 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
|
87 return 0; |
|
88 } |
|
89 n = sign * Math.floor( Math.abs(n) ) |
|
90 |
|
91 n = n % Math.pow(2,32); |
|
92 |
|
93 if ( n < 0 ){ |
|
94 n += Math.pow(2,32); |
|
95 } |
|
96 |
|
97 return ( n ); |
|
98 } |
|
99 function ToUint16( n ) { |
|
100 var sign = ( n < 0 ) ? -1 : 1; |
|
101 |
|
102 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
|
103 return 0; |
|
104 } |
|
105 |
|
106 n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16); |
|
107 |
|
108 if (n <0) { |
|
109 n += Math.pow(2,16); |
|
110 } |
|
111 |
|
112 return ( n ); |
|
113 } |
|
114 function Mask( b, n ) { |
|
115 b = ToUint32BitString( b ); |
|
116 b = b.substring( b.length - n ); |
|
117 b = ToUint32Decimal( b ); |
|
118 return ( b ); |
|
119 } |
|
120 function ToUint32BitString( n ) { |
|
121 var b = ""; |
|
122 for ( p = 31; p >=0; p-- ) { |
|
123 if ( n >= Math.pow(2,p) ) { |
|
124 b += "1"; |
|
125 n -= Math.pow(2,p); |
|
126 } else { |
|
127 b += "0"; |
|
128 } |
|
129 } |
|
130 return b; |
|
131 } |
|
132 function ToInt32BitString( n ) { |
|
133 var b = ""; |
|
134 var sign = ( n < 0 ) ? -1 : 1; |
|
135 |
|
136 b += ( sign == 1 ) ? "0" : "1"; |
|
137 |
|
138 for ( p = 30; p >=0; p-- ) { |
|
139 if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) { |
|
140 b += ( sign == 1 ) ? "1" : "0"; |
|
141 n -= sign * Math.pow( 2, p ); |
|
142 } else { |
|
143 b += ( sign == 1 ) ? "0" : "1"; |
|
144 } |
|
145 } |
|
146 |
|
147 return b; |
|
148 } |
|
149 function ToInt32Decimal( bin ) { |
|
150 var r = 0; |
|
151 var sign; |
|
152 |
|
153 if ( Number(bin.charAt(0)) == 0 ) { |
|
154 sign = 1; |
|
155 r = 0; |
|
156 } else { |
|
157 sign = -1; |
|
158 r = -(Math.pow(2,31)); |
|
159 } |
|
160 |
|
161 for ( var j = 0; j < 31; j++ ) { |
|
162 r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
|
163 } |
|
164 |
|
165 return r; |
|
166 } |
|
167 function ToUint32Decimal( bin ) { |
|
168 var r = 0; |
|
169 |
|
170 |
|
171 for ( l = bin.length; l < 32; l++ ) { |
|
172 bin = "0" + bin; |
|
173 } |
|
174 |
|
175 for ( j = 0; j < 32; j++ ) { |
|
176 r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
|
177 |
|
178 } |
|
179 |
|
180 return r; |
|
181 } |
|
182 function RShift( s, a ) { |
|
183 s = ToUint32BitString( s ); |
|
184 for ( z = 0; z < a; z++ ) { |
|
185 s = "0" + s; |
|
186 } |
|
187 s = s.substring( 0, s.length - a ); |
|
188 |
|
189 return ToUint32Decimal(s); |
|
190 } |
|
191 function UnsignedRightShift( s, a ) { |
|
192 s = ToUint32( s ); |
|
193 a = ToUint32( a ); |
|
194 a = Mask( a, 5 ); |
|
195 return ( RShift( s, a ) ); |
|
196 } |