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/ 5 * 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. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 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 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22/** 23 File Name: 11.7.1.js 24 ECMA Section: 11.7.1 The Left Shift Operator ( << ) 25 Description: 26 Performs a bitwise left shift operation on the left argument by the amount 27 specified by the right argument. 28 29 The production ShiftExpression : ShiftExpression << AdditiveExpression is 30 evaluated as follows: 31 32 1. Evaluate ShiftExpression. 33 2. Call GetValue(Result(1)). 34 3. Evaluate AdditiveExpression. 35 4. Call GetValue(Result(3)). 36 5. Call ToInt32(Result(2)). 37 6. Call ToUint32(Result(4)). 38 7. Mask out all but the least significant 5 bits of Result(6), that is, 39 compute Result(6) & 0x1F. 40 8. Left shift Result(5) by Result(7) bits. The result is a signed 32 bit 41 integer. 42 9. Return Result(8). 43 44 Author: christine@netscape.com 45 Date: 12 november 1997 46*/ 47 var SECTION = "11.7.1"; 48 var VERSION = "ECMA_1"; 49 startTest(); 50 51 var testcases = getTestCases(); 52 53 writeHeaderToLog( SECTION + " The left shift operator ( << )"); 54 test(); 55 56function test() { 57 for ( tc=0; tc < testcases.length; tc++ ) { 58 testcases[tc].passed = writeTestCaseResult( 59 testcases[tc].expect, 60 testcases[tc].actual, 61 testcases[tc].description +" = "+ 62 testcases[tc].actual ); 63 64 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 65 } 66 stopTest(); 67 return ( testcases ); 68} 69function getTestCases() { 70 var array = new Array(); 71 var item = 0; 72 73 for ( power = 0; power < 33; power++ ) { 74 shiftexp = Math.pow( 2, power ); 75 76 for ( addexp = 0; addexp < 33; addexp++ ) { 77 array[item++] = new TestCase( SECTION, 78 shiftexp + " << " + addexp, 79 LeftShift( shiftexp, addexp ), 80 shiftexp << addexp ); 81 } 82 } 83 84 return ( array ); 85} 86function ToInteger( n ) { 87 n = Number( n ); 88 var sign = ( n < 0 ) ? -1 : 1; 89 90 if ( n != n ) { 91 return 0; 92 } 93 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) { 94 return n; 95 } 96 return ( sign * Math.floor(Math.abs(n)) ); 97} 98function ToInt32( n ) { 99 n = Number( 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,32); 107 n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n; 108 109 return ( n ); 110} 111function ToUint32( n ) { 112 n = Number( n ); 113 var sign = ( n < 0 ) ? -1 : 1; 114 115 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { 116 return 0; 117 } 118 n = sign * Math.floor( Math.abs(n) ) 119 120 n = n % Math.pow(2,32); 121 122 if ( n < 0 ){ 123 n += Math.pow(2,32); 124 } 125 126 return ( n ); 127} 128function ToUint16( n ) { 129 var sign = ( n < 0 ) ? -1 : 1; 130 131 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { 132 return 0; 133 } 134 135 n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16); 136 137 if (n <0) { 138 n += Math.pow(2,16); 139 } 140 141 return ( n ); 142} 143function Mask( b, n ) { 144 b = ToUint32BitString( b ); 145 b = b.substring( b.length - n ); 146 b = ToUint32Decimal( b ); 147 return ( b ); 148} 149function ToUint32BitString( n ) { 150 var b = ""; 151 for ( p = 31; p >=0; p-- ) { 152 if ( n >= Math.pow(2,p) ) { 153 b += "1"; 154 n -= Math.pow(2,p); 155 } else { 156 b += "0"; 157 } 158 } 159 return b; 160} 161function ToInt32BitString( n ) { 162 var b = ""; 163 var sign = ( n < 0 ) ? -1 : 1; 164 165 b += ( sign == 1 ) ? "0" : "1"; 166 167 for ( p = 30; p >=0; p-- ) { 168 if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) { 169 b += ( sign == 1 ) ? "1" : "0"; 170 n -= sign * Math.pow( 2, p ); 171 } else { 172 b += ( sign == 1 ) ? "0" : "1"; 173 } 174 } 175 176 return b; 177} 178function ToInt32Decimal( bin ) { 179 var r = 0; 180 var sign; 181 182 if ( Number(bin.charAt(0)) == 0 ) { 183 sign = 1; 184 r = 0; 185 } else { 186 sign = -1; 187 r = -(Math.pow(2,31)); 188 } 189 190 for ( var j = 0; j < 31; j++ ) { 191 r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); 192 } 193 194 return r; 195} 196function ToUint32Decimal( bin ) { 197 var r = 0; 198 199 200 for ( l = bin.length; l < 32; l++ ) { 201 bin = "0" + bin; 202 } 203 204 for ( j = 0; j < 31; j++ ) { 205 r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); 206 207 } 208 209 return r; 210} 211function LeftShift( s, a ) { 212 var shift = ToInt32( s ); 213 var add = ToUint32( a ); 214 add = Mask( add, 5 ); 215 var exp = LShift( shift, add ); 216 217 return ( exp ); 218} 219function LShift( s, a ) { 220 s = ToInt32BitString( s ); 221 222 for ( var z = 0; z < a; z++ ) { 223 s += "0"; 224 } 225 226 s = s.substring( a, s.length); 227 228 return ToInt32(ToInt32Decimal(s)); 229} 230