1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
42 ECMA Section: 11.7.3 The unsigned right shift operator ( >>> )
44 11.7.3 The unsigned right shift operator ( >>> )
45 Performs a zero-filling bitwise right shift operation on the left argument
46 by the amount specified by the right argument.
48 The production ShiftExpression : ShiftExpression >>> AdditiveExpression is
51 1. Evaluate ShiftExpression.
52 2. Call GetValue(Result(1)).
53 3. Evaluate AdditiveExpression.
54 4. Call GetValue(Result(3)).
55 5. Call ToUint32(Result(2)).
56 6. Call ToUint32(Result(4)).
57 7. Mask out all but the least significant 5 bits of Result(6), that is,
58 compute Result(6) & 0x1F.
59 8. Perform zero-filling right shift of Result(5) by Result(7) bits.
60 Vacated bits are filled with zero. The result is an unsigned 32 bit
64 Author: christine@netscape.com
65 Date: 12 november 1997
67 var SECTION = "11.7.3";
68 var VERSION = "ECMA_1";
71 writeHeaderToLog( SECTION + " The unsigned right shift operator ( >>> )");
76 for ( power = 0; power <= 32; power++ ) {
77 shiftexp = Math.pow( 2, power );
79 for ( addexp = 0; addexp <= 32; addexp++ ) {
80 new TestCase( SECTION,
81 shiftexp + " >>> " + addexp,
82 UnsignedRightShift( shiftexp, addexp ),
83 shiftexp >>> addexp );
90 function ToInteger( n ) {
92 var sign = ( n < 0 ) ? -1 : 1;
97 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
100 return ( sign * Math.floor(Math.abs(n)) );
102 function ToInt32( n ) {
104 var sign = ( n < 0 ) ? -1 : 1;
106 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
110 n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
111 n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
115 function ToUint32( n ) {
117 var sign = ( n < 0 ) ? -1 : 1;
119 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
122 n = sign * Math.floor( Math.abs(n) )
124 n = n % Math.pow(2,32);
132 function ToUint16( n ) {
133 var sign = ( n < 0 ) ? -1 : 1;
135 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
139 n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
147 function Mask( b, n ) {
148 b = ToUint32BitString( b );
149 b = b.substring( b.length - n );
150 b = ToUint32Decimal( b );
153 function ToUint32BitString( n ) {
155 for ( p = 31; p >=0; p-- ) {
156 if ( n >= Math.pow(2,p) ) {
165 function ToInt32BitString( n ) {
167 var sign = ( n < 0 ) ? -1 : 1;
169 b += ( sign == 1 ) ? "0" : "1";
171 for ( p = 30; p >=0; p-- ) {
172 if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
173 b += ( sign == 1 ) ? "1" : "0";
174 n -= sign * Math.pow( 2, p );
176 b += ( sign == 1 ) ? "0" : "1";
182 function ToInt32Decimal( bin ) {
186 if ( Number(bin.charAt(0)) == 0 ) {
191 r = -(Math.pow(2,31));
194 for ( var j = 0; j < 31; j++ ) {
195 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
200 function ToUint32Decimal( bin ) {
204 for ( l = bin.length; l < 32; l++ ) {
208 for ( j = 0; j < 32; j++ ) {
209 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
215 function RShift( s, a ) {
216 s = ToUint32BitString( s );
217 for ( z = 0; z < a; z++ ) {
220 s = s.substring( 0, s.length - a );
222 return ToUint32Decimal(s);
224 function UnsignedRightShift( s, a ) {
228 return ( RShift( s, a ) );