|
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/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 File Name: 11.4.8.js |
|
9 ECMA Section: 11.4.8 Bitwise NOT Operator |
|
10 Description: flip bits up to 32 bits |
|
11 no special cases |
|
12 Author: christine@netscape.com |
|
13 Date: 7 july 1997 |
|
14 |
|
15 Data File Fields: |
|
16 VALUE value passed as an argument to the ~ operator |
|
17 E_RESULT expected return value of ~ VALUE; |
|
18 |
|
19 Static variables: |
|
20 none |
|
21 |
|
22 */ |
|
23 |
|
24 var SECTION = "11.4.8"; |
|
25 var VERSION = "ECMA_1"; |
|
26 startTest(); |
|
27 |
|
28 writeHeaderToLog( SECTION + " Bitwise Not operator"); |
|
29 |
|
30 for ( var i = 0; i < 35; i++ ) { |
|
31 var p = Math.pow(2,i); |
|
32 |
|
33 new TestCase( SECTION, "~"+p, Not(p), ~p ); |
|
34 |
|
35 } |
|
36 for ( i = 0; i < 35; i++ ) { |
|
37 var p = -Math.pow(2,i); |
|
38 |
|
39 new TestCase( SECTION, "~"+p, Not(p), ~p ); |
|
40 |
|
41 } |
|
42 |
|
43 test(); |
|
44 |
|
45 function ToInteger( n ) { |
|
46 n = Number( n ); |
|
47 var sign = ( n < 0 ) ? -1 : 1; |
|
48 |
|
49 if ( n != n ) { |
|
50 return 0; |
|
51 } |
|
52 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) { |
|
53 return n; |
|
54 } |
|
55 return ( sign * Math.floor(Math.abs(n)) ); |
|
56 } |
|
57 function ToInt32( n ) { |
|
58 n = Number( n ); |
|
59 var sign = ( n < 0 ) ? -1 : 1; |
|
60 |
|
61 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
|
62 return 0; |
|
63 } |
|
64 |
|
65 n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32); |
|
66 n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n; |
|
67 |
|
68 return ( n ); |
|
69 } |
|
70 function ToUint32( n ) { |
|
71 n = Number( n ); |
|
72 var sign = ( n < 0 ) ? -1 : 1; |
|
73 |
|
74 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
|
75 return 0; |
|
76 } |
|
77 n = sign * Math.floor( Math.abs(n) ) |
|
78 |
|
79 n = n % Math.pow(2,32); |
|
80 |
|
81 if ( n < 0 ){ |
|
82 n += Math.pow(2,32); |
|
83 } |
|
84 |
|
85 return ( n ); |
|
86 } |
|
87 function ToUint16( n ) { |
|
88 var sign = ( n < 0 ) ? -1 : 1; |
|
89 |
|
90 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
|
91 return 0; |
|
92 } |
|
93 |
|
94 n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16); |
|
95 |
|
96 if (n <0) { |
|
97 n += Math.pow(2,16); |
|
98 } |
|
99 |
|
100 return ( n ); |
|
101 } |
|
102 function Mask( b, n ) { |
|
103 b = ToUint32BitString( b ); |
|
104 b = b.substring( b.length - n ); |
|
105 b = ToUint32Decimal( b ); |
|
106 return ( b ); |
|
107 } |
|
108 function ToUint32BitString( n ) { |
|
109 var b = ""; |
|
110 for ( var p = 31; p >=0; p-- ) { |
|
111 if ( n >= Math.pow(2,p) ) { |
|
112 b += "1"; |
|
113 n -= Math.pow(2,p); |
|
114 } else { |
|
115 b += "0"; |
|
116 } |
|
117 } |
|
118 return b; |
|
119 } |
|
120 function ToInt32BitString( n ) { |
|
121 var b = ""; |
|
122 var sign = ( n < 0 ) ? -1 : 1; |
|
123 |
|
124 b += ( sign == 1 ) ? "0" : "1"; |
|
125 |
|
126 for ( var p = 30; p >=0; p-- ) { |
|
127 if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) { |
|
128 b += ( sign == 1 ) ? "1" : "0"; |
|
129 n -= sign * Math.pow( 2, p ); |
|
130 } else { |
|
131 b += ( sign == 1 ) ? "0" : "1"; |
|
132 } |
|
133 } |
|
134 |
|
135 return b; |
|
136 } |
|
137 function ToInt32Decimal( bin ) { |
|
138 var r = 0; |
|
139 var sign; |
|
140 |
|
141 if ( Number(bin.charAt(0)) == 0 ) { |
|
142 sign = 1; |
|
143 r = 0; |
|
144 } else { |
|
145 sign = -1; |
|
146 r = -(Math.pow(2,31)); |
|
147 } |
|
148 |
|
149 for ( var j = 0; j < 31; j++ ) { |
|
150 r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
|
151 } |
|
152 |
|
153 return r; |
|
154 } |
|
155 function ToUint32Decimal( bin ) { |
|
156 var r = 0; |
|
157 |
|
158 for ( var l = bin.length; l < 32; l++ ) { |
|
159 bin = "0" + bin; |
|
160 } |
|
161 |
|
162 for ( var j = 0; j < 31; j++ ) { |
|
163 r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
|
164 } |
|
165 |
|
166 return r; |
|
167 } |
|
168 function Not( n ) { |
|
169 n = ToInt32(n); |
|
170 n = ToInt32BitString(n); |
|
171 |
|
172 var r = ""; |
|
173 |
|
174 for( var l = 0; l < n.length; l++ ) { |
|
175 r += ( n.charAt(l) == "0" ) ? "1" : "0"; |
|
176 } |
|
177 |
|
178 n = ToInt32Decimal(r); |
|
179 |
|
180 return n; |
|
181 } |