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.4.8.js michael@0: ECMA Section: 11.4.8 Bitwise NOT Operator michael@0: Description: flip bits up to 32 bits michael@0: no special cases michael@0: Author: christine@netscape.com michael@0: Date: 7 july 1997 michael@0: michael@0: Data File Fields: michael@0: VALUE value passed as an argument to the ~ operator michael@0: E_RESULT expected return value of ~ VALUE; michael@0: michael@0: Static variables: michael@0: none michael@0: michael@0: */ michael@0: michael@0: var SECTION = "11.4.8"; michael@0: var VERSION = "ECMA_1"; michael@0: startTest(); michael@0: michael@0: writeHeaderToLog( SECTION + " Bitwise Not operator"); michael@0: michael@0: for ( var i = 0; i < 35; i++ ) { michael@0: var p = Math.pow(2,i); michael@0: michael@0: new TestCase( SECTION, "~"+p, Not(p), ~p ); michael@0: michael@0: } michael@0: for ( i = 0; i < 35; i++ ) { michael@0: var p = -Math.pow(2,i); michael@0: michael@0: new TestCase( SECTION, "~"+p, Not(p), ~p ); 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 ( var 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 ( var 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 ( var l = bin.length; l < 32; l++ ) { michael@0: bin = "0" + bin; 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 Not( n ) { michael@0: n = ToInt32(n); michael@0: n = ToInt32BitString(n); michael@0: michael@0: var r = ""; michael@0: michael@0: for( var l = 0; l < n.length; l++ ) { michael@0: r += ( n.charAt(l) == "0" ) ? "1" : "0"; michael@0: } michael@0: michael@0: n = ToInt32Decimal(r); michael@0: michael@0: return n; michael@0: }