michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: File Name: 11.7.2.js michael@0: ECMA Section: 11.7.2 The signed right shift operator ( >> ) michael@0: Description: michael@0: Performs a sign-filling bitwise right shift operation on the left argument michael@0: by the amount specified by the right argument. michael@0: michael@0: The production ShiftExpression : ShiftExpression >> AdditiveExpression is michael@0: evaluated as follows: michael@0: michael@0: 1. Evaluate ShiftExpression. michael@0: 2. Call GetValue(Result(1)). michael@0: 3. Evaluate AdditiveExpression. michael@0: 4. Call GetValue(Result(3)). michael@0: 5. Call ToInt32(Result(2)). michael@0: 6. Call ToUint32(Result(4)). michael@0: 7. Mask out all but the least significant 5 bits of Result(6), that is, michael@0: compute Result(6) & 0x1F. michael@0: 8. Perform sign-extending right shift of Result(5) by Result(7) bits. The michael@0: most significant bit is propagated. The result is a signed 32 bit michael@0: integer. michael@0: 9. Return Result(8). michael@0: michael@0: Author: christine@netscape.com michael@0: Date: 12 november 1997 michael@0: */ michael@0: var SECTION = "11.7.2"; michael@0: var VERSION = "ECMA_1"; michael@0: startTest(); michael@0: michael@0: writeHeaderToLog( SECTION + " The signed right shift operator ( >> )"); michael@0: michael@0: var power = 0; michael@0: var addexp = 0; michael@0: michael@0: for ( power = 0; power <= 32; power++ ) { michael@0: shiftexp = Math.pow( 2, power ); michael@0: michael@0: for ( addexp = 0; addexp <= 32; addexp++ ) { michael@0: new TestCase( SECTION, michael@0: shiftexp + " >> " + addexp, michael@0: SignedRightShift( shiftexp, addexp ), michael@0: shiftexp >> addexp ); michael@0: } michael@0: } michael@0: michael@0: for ( power = 0; power <= 32; power++ ) { michael@0: shiftexp = -Math.pow( 2, power ); michael@0: michael@0: for ( addexp = 0; addexp <= 32; addexp++ ) { michael@0: new TestCase( SECTION, michael@0: shiftexp + " >> " + addexp, michael@0: SignedRightShift( shiftexp, addexp ), michael@0: shiftexp >> addexp ); michael@0: } michael@0: } michael@0: michael@0: test(); michael@0: michael@0: function ToInteger( n ) { michael@0: n = Number( n ); michael@0: var sign = ( n < 0 ) ? -1 : 1; michael@0: michael@0: if ( n != n ) { michael@0: return 0; michael@0: } michael@0: if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) { michael@0: return n; michael@0: } michael@0: return ( sign * Math.floor(Math.abs(n)) ); michael@0: } michael@0: function ToInt32( n ) { michael@0: n = Number( n ); michael@0: var sign = ( n < 0 ) ? -1 : 1; michael@0: michael@0: if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { michael@0: return 0; michael@0: } michael@0: michael@0: n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32); michael@0: n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n; michael@0: michael@0: return ( n ); michael@0: } michael@0: function ToUint32( n ) { michael@0: n = Number( n ); michael@0: var sign = ( n < 0 ) ? -1 : 1; michael@0: michael@0: if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { michael@0: return 0; michael@0: } michael@0: n = sign * Math.floor( Math.abs(n) ) michael@0: michael@0: n = n % Math.pow(2,32); michael@0: michael@0: if ( n < 0 ){ michael@0: n += Math.pow(2,32); michael@0: } michael@0: michael@0: return ( n ); michael@0: } michael@0: function ToUint16( n ) { michael@0: var sign = ( n < 0 ) ? -1 : 1; michael@0: michael@0: if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { michael@0: return 0; michael@0: } michael@0: michael@0: n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16); michael@0: michael@0: if (n <0) { michael@0: n += Math.pow(2,16); michael@0: } michael@0: michael@0: return ( n ); michael@0: } michael@0: function Mask( b, n ) { michael@0: b = ToUint32BitString( b ); michael@0: b = b.substring( b.length - n ); michael@0: b = ToUint32Decimal( b ); michael@0: return ( b ); michael@0: } michael@0: function ToUint32BitString( n ) { michael@0: var b = ""; michael@0: for ( p = 31; p >=0; p-- ) { michael@0: if ( n >= Math.pow(2,p) ) { michael@0: b += "1"; michael@0: n -= Math.pow(2,p); michael@0: } else { michael@0: b += "0"; michael@0: } michael@0: } michael@0: return b; michael@0: } michael@0: function ToInt32BitString( n ) { michael@0: var b = ""; michael@0: var sign = ( n < 0 ) ? -1 : 1; michael@0: michael@0: b += ( sign == 1 ) ? "0" : "1"; michael@0: michael@0: for ( p = 30; p >=0; p-- ) { michael@0: if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) { michael@0: b += ( sign == 1 ) ? "1" : "0"; michael@0: n -= sign * Math.pow( 2, p ); michael@0: } else { michael@0: b += ( sign == 1 ) ? "0" : "1"; michael@0: } michael@0: } michael@0: michael@0: return b; michael@0: } michael@0: function ToInt32Decimal( bin ) { michael@0: var r = 0; michael@0: var sign; michael@0: michael@0: if ( Number(bin.charAt(0)) == 0 ) { michael@0: sign = 1; michael@0: r = 0; michael@0: } else { michael@0: sign = -1; michael@0: r = -(Math.pow(2,31)); michael@0: } michael@0: michael@0: for ( var j = 0; j < 31; j++ ) { michael@0: r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); michael@0: } michael@0: michael@0: return r; michael@0: } michael@0: function ToUint32Decimal( bin ) { michael@0: var r = 0; michael@0: michael@0: for ( l = bin.length; l < 32; l++ ) { michael@0: bin = "0" + bin; michael@0: } michael@0: michael@0: for ( j = 0; j < 31; j++ ) { michael@0: r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); michael@0: } michael@0: michael@0: return r; michael@0: } michael@0: function SignedRightShift( s, a ) { michael@0: s = ToInt32( s ); michael@0: a = ToUint32( a ); michael@0: a = Mask( a, 5 ); michael@0: return ( SignedRShift( s, a ) ); michael@0: } michael@0: function SignedRShift( s, a ) { michael@0: s = ToInt32BitString( s ); michael@0: michael@0: var firstbit = s.substring(0,1); michael@0: michael@0: s = s.substring( 1, s.length ); michael@0: michael@0: for ( var z = 0; z < a; z++ ) { michael@0: s = firstbit + s; michael@0: } michael@0: michael@0: s = s.substring( 0, s.length - a); michael@0: michael@0: s = firstbit +s; michael@0: michael@0: michael@0: return ToInt32(ToInt32Decimal(s)); michael@0: }