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.4.8 Bitwise NOT Operator
43 Description: flip bits up to 32 bits
45 Author: christine@netscape.com
49 VALUE value passed as an argument to the ~ operator
50 E_RESULT expected return value of ~ VALUE;
57 var SECTION = "11.4.8";
58 var VERSION = "ECMA_1";
61 writeHeaderToLog( SECTION + " Bitwise Not operator");
63 for ( var i = 0; i < 35; i++ ) {
64 var p = Math.pow(2,i);
66 new TestCase( SECTION, "~"+p, Not(p), ~p );
69 for ( i = 0; i < 35; i++ ) {
70 var p = -Math.pow(2,i);
72 new TestCase( SECTION, "~"+p, Not(p), ~p );
78 function ToInteger( n ) {
80 var sign = ( n < 0 ) ? -1 : 1;
85 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
88 return ( sign * Math.floor(Math.abs(n)) );
90 function ToInt32( n ) {
92 var sign = ( n < 0 ) ? -1 : 1;
94 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
98 n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
99 n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
103 function ToUint32( n ) {
105 var sign = ( n < 0 ) ? -1 : 1;
107 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
110 n = sign * Math.floor( Math.abs(n) )
112 n = n % Math.pow(2,32);
120 function ToUint16( n ) {
121 var sign = ( n < 0 ) ? -1 : 1;
123 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
127 n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
135 function Mask( b, n ) {
136 b = ToUint32BitString( b );
137 b = b.substring( b.length - n );
138 b = ToUint32Decimal( b );
141 function ToUint32BitString( n ) {
143 for ( var p = 31; p >=0; p-- ) {
144 if ( n >= Math.pow(2,p) ) {
153 function ToInt32BitString( n ) {
155 var sign = ( n < 0 ) ? -1 : 1;
157 b += ( sign == 1 ) ? "0" : "1";
159 for ( var p = 30; p >=0; p-- ) {
160 if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
161 b += ( sign == 1 ) ? "1" : "0";
162 n -= sign * Math.pow( 2, p );
164 b += ( sign == 1 ) ? "0" : "1";
170 function ToInt32Decimal( bin ) {
174 if ( Number(bin.charAt(0)) == 0 ) {
179 r = -(Math.pow(2,31));
182 for ( var j = 0; j < 31; j++ ) {
183 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
188 function ToUint32Decimal( bin ) {
191 for ( var l = bin.length; l < 32; l++ ) {
195 for ( var j = 0; j < 31; j++ ) {
196 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
203 n = ToInt32BitString(n);
207 for( var l = 0; l < n.length; l++ ) {
208 r += ( n.charAt(l) == "0" ) ? "1" : "0";
211 n = ToInt32Decimal(r);