1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9
ECMA Section: 11.10-2 Binary Bitwise Operators: |
13
The production A : A @ B, where @ is one of the bitwise operators in the
14
productions &, ^, | , is evaluated as follows:
17
2. Call GetValue(Result(1)).
19
4. Call GetValue(Result(3)).
20
5. Call ToInt32(Result(2)).
21
6. Call ToInt32(Result(4)).
22
7. Apply the bitwise operator @ to Result(5) and Result(6). The result is
23
a signed 32 bit integer.
26
Author: christine@netscape.com
27
Date: 12 november 1997
29
var SECTION = "11.10-2";
30
var VERSION = "ECMA_1";
33
writeHeaderToLog( SECTION + " Binary Bitwise Operators: |");
38
for ( shiftpow = 0; shiftpow < 33; shiftpow++ ) {
39
shiftexp += Math.pow( 2, shiftpow );
41
for ( addpow = 0; addpow < 33; addpow++ ) {
42
addexp += Math.pow(2, addpow);
44
new TestCase( SECTION,
45
shiftexp + " | " + addexp,
46
Or( shiftexp, addexp ),
53
function ToInteger( n ) {
55
var sign = ( n < 0 ) ? -1 : 1;
60
if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
63
return ( sign * Math.floor(Math.abs(n)) );
65
function ToInt32( n ) {
67
var sign = ( n < 0 ) ? -1 : 1;
69
if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
73
n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
74
n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
78
function ToUint32( n ) {
80
var sign = ( n < 0 ) ? -1 : 1;
82
if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
85
n = sign * Math.floor( Math.abs(n) )
87
n = n % Math.pow(2,32);
95
function ToUint16( n ) {
96
var sign = ( n < 0 ) ? -1 : 1;
98
if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
102
n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
110
function Mask( b, n ) {
111
b = ToUint32BitString( b );
112
b = b.substring( b.length - n );
113
b = ToUint32Decimal( b );
116
function ToUint32BitString( n ) {
118
for ( p = 31; p >=0; p-- ) {
119
if ( n >= Math.pow(2,p) ) {
128
function ToInt32BitString( n ) {
130
var sign = ( n < 0 ) ? -1 : 1;
132
b += ( sign == 1 ) ? "0" : "1";
134
for ( p = 30; p >=0; p-- ) {
135
if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
136
b += ( sign == 1 ) ? "1" : "0";
137
n -= sign * Math.pow( 2, p );
139
b += ( sign == 1 ) ? "0" : "1";
145
function ToInt32Decimal( bin ) {
149
if ( Number(bin.charAt(0)) == 0 ) {
154
r = -(Math.pow(2,31));
157
for ( var j = 0; j < 31; j++ ) {
158
r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
163
function ToUint32Decimal( bin ) {
167
for ( l = bin.length; l < 32; l++ ) {
171
for ( j = 0; j < 31; j++ ) {
172
r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
178
function And( s, a ) {
182
var bs = ToInt32BitString( s );
183
var ba = ToInt32BitString( a );
187
for ( var bit = 0; bit < bs.length; bit++ ) {
188
if ( bs.charAt(bit) == "1" && ba.charAt(bit) == "1" ) {
194
return ToInt32Decimal(result);
196
function Xor( s, a ) {
200
var bs = ToInt32BitString( s );
201
var ba = ToInt32BitString( a );
205
for ( var bit = 0; bit < bs.length; bit++ ) {
206
if ( (bs.charAt(bit) == "1" && ba.charAt(bit) == "0") ||
207
(bs.charAt(bit) == "0" && ba.charAt(bit) == "1")
215
return ToInt32Decimal(result);
217
function Or( s, a ) {
221
var bs = ToInt32BitString( s );
222
var ba = ToInt32BitString( a );
226
for ( var bit = 0; bit < bs.length; bit++ ) {
227
if ( bs.charAt(bit) == "1" || ba.charAt(bit) == "1" ) {
234
return ToInt32Decimal(result);