2
|
1 |
/*
|
|
2 |
* Copyright 1994-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.lang;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* The {@code Long} class wraps a value of the primitive type {@code
|
|
30 |
* long} in an object. An object of type {@code Long} contains a
|
|
31 |
* single field whose type is {@code long}.
|
|
32 |
*
|
|
33 |
* <p> In addition, this class provides several methods for converting
|
|
34 |
* a {@code long} to a {@code String} and a {@code String} to a {@code
|
|
35 |
* long}, as well as other constants and methods useful when dealing
|
|
36 |
* with a {@code long}.
|
|
37 |
*
|
|
38 |
* <p>Implementation note: The implementations of the "bit twiddling"
|
|
39 |
* methods (such as {@link #highestOneBit(long) highestOneBit} and
|
|
40 |
* {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
|
|
41 |
* based on material from Henry S. Warren, Jr.'s <i>Hacker's
|
|
42 |
* Delight</i>, (Addison Wesley, 2002).
|
|
43 |
*
|
|
44 |
* @author Lee Boynton
|
|
45 |
* @author Arthur van Hoff
|
|
46 |
* @author Josh Bloch
|
|
47 |
* @author Joseph D. Darcy
|
|
48 |
* @since JDK1.0
|
|
49 |
*/
|
|
50 |
public final class Long extends Number implements Comparable<Long> {
|
|
51 |
/**
|
|
52 |
* A constant holding the minimum value a {@code long} can
|
|
53 |
* have, -2<sup>63</sup>.
|
|
54 |
*/
|
|
55 |
public static final long MIN_VALUE = 0x8000000000000000L;
|
|
56 |
|
|
57 |
/**
|
|
58 |
* A constant holding the maximum value a {@code long} can
|
|
59 |
* have, 2<sup>63</sup>-1.
|
|
60 |
*/
|
|
61 |
public static final long MAX_VALUE = 0x7fffffffffffffffL;
|
|
62 |
|
|
63 |
/**
|
|
64 |
* The {@code Class} instance representing the primitive type
|
|
65 |
* {@code long}.
|
|
66 |
*
|
|
67 |
* @since JDK1.1
|
|
68 |
*/
|
|
69 |
public static final Class<Long> TYPE = (Class<Long>) Class.getPrimitiveClass("long");
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Returns a string representation of the first argument in the
|
|
73 |
* radix specified by the second argument.
|
|
74 |
*
|
|
75 |
* <p>If the radix is smaller than {@code Character.MIN_RADIX}
|
|
76 |
* or larger than {@code Character.MAX_RADIX}, then the radix
|
|
77 |
* {@code 10} is used instead.
|
|
78 |
*
|
|
79 |
* <p>If the first argument is negative, the first element of the
|
|
80 |
* result is the ASCII minus sign {@code '-'}
|
|
81 |
* (<code>'\u002d'</code>). If the first argument is not
|
|
82 |
* negative, no sign character appears in the result.
|
|
83 |
*
|
|
84 |
* <p>The remaining characters of the result represent the magnitude
|
|
85 |
* of the first argument. If the magnitude is zero, it is
|
|
86 |
* represented by a single zero character {@code '0'}
|
|
87 |
* (<code>'\u0030'</code>); otherwise, the first character of
|
|
88 |
* the representation of the magnitude will not be the zero
|
|
89 |
* character. The following ASCII characters are used as digits:
|
|
90 |
*
|
|
91 |
* <blockquote>
|
|
92 |
* {@code 0123456789abcdefghijklmnopqrstuvwxyz}
|
|
93 |
* </blockquote>
|
|
94 |
*
|
|
95 |
* These are <code>'\u0030'</code> through
|
|
96 |
* <code>'\u0039'</code> and <code>'\u0061'</code> through
|
|
97 |
* <code>'\u007a'</code>. If {@code radix} is
|
|
98 |
* <var>N</var>, then the first <var>N</var> of these characters
|
|
99 |
* are used as radix-<var>N</var> digits in the order shown. Thus,
|
|
100 |
* the digits for hexadecimal (radix 16) are
|
|
101 |
* {@code 0123456789abcdef}. If uppercase letters are
|
|
102 |
* desired, the {@link java.lang.String#toUpperCase()} method may
|
|
103 |
* be called on the result:
|
|
104 |
*
|
|
105 |
* <blockquote>
|
|
106 |
* {@code Long.toString(n, 16).toUpperCase()}
|
|
107 |
* </blockquote>
|
|
108 |
*
|
|
109 |
* @param i a {@code long} to be converted to a string.
|
|
110 |
* @param radix the radix to use in the string representation.
|
|
111 |
* @return a string representation of the argument in the specified radix.
|
|
112 |
* @see java.lang.Character#MAX_RADIX
|
|
113 |
* @see java.lang.Character#MIN_RADIX
|
|
114 |
*/
|
|
115 |
public static String toString(long i, int radix) {
|
|
116 |
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
|
|
117 |
radix = 10;
|
|
118 |
if (radix == 10)
|
|
119 |
return toString(i);
|
|
120 |
char[] buf = new char[65];
|
|
121 |
int charPos = 64;
|
|
122 |
boolean negative = (i < 0);
|
|
123 |
|
|
124 |
if (!negative) {
|
|
125 |
i = -i;
|
|
126 |
}
|
|
127 |
|
|
128 |
while (i <= -radix) {
|
|
129 |
buf[charPos--] = Integer.digits[(int)(-(i % radix))];
|
|
130 |
i = i / radix;
|
|
131 |
}
|
|
132 |
buf[charPos] = Integer.digits[(int)(-i)];
|
|
133 |
|
|
134 |
if (negative) {
|
|
135 |
buf[--charPos] = '-';
|
|
136 |
}
|
|
137 |
|
|
138 |
return new String(buf, charPos, (65 - charPos));
|
|
139 |
}
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Returns a string representation of the {@code long}
|
|
143 |
* argument as an unsigned integer in base 16.
|
|
144 |
*
|
|
145 |
* <p>The unsigned {@code long} value is the argument plus
|
|
146 |
* 2<sup>64</sup> if the argument is negative; otherwise, it is
|
|
147 |
* equal to the argument. This value is converted to a string of
|
|
148 |
* ASCII digits in hexadecimal (base 16) with no extra
|
|
149 |
* leading {@code 0}s. If the unsigned magnitude is zero, it
|
|
150 |
* is represented by a single zero character {@code '0'}
|
|
151 |
* (<code>'\u0030'</code>); otherwise, the first character of
|
|
152 |
* the representation of the unsigned magnitude will not be the
|
|
153 |
* zero character. The following characters are used as
|
|
154 |
* hexadecimal digits:
|
|
155 |
*
|
|
156 |
* <blockquote>
|
|
157 |
* {@code 0123456789abcdef}
|
|
158 |
* </blockquote>
|
|
159 |
*
|
|
160 |
* These are the characters <code>'\u0030'</code> through
|
|
161 |
* <code>'\u0039'</code> and <code>'\u0061'</code> through
|
|
162 |
* <code>'\u0066'</code>. If uppercase letters are desired,
|
|
163 |
* the {@link java.lang.String#toUpperCase()} method may be called
|
|
164 |
* on the result:
|
|
165 |
*
|
|
166 |
* <blockquote>
|
|
167 |
* {@code Long.toHexString(n).toUpperCase()}
|
|
168 |
* </blockquote>
|
|
169 |
*
|
|
170 |
* @param i a {@code long} to be converted to a string.
|
|
171 |
* @return the string representation of the unsigned {@code long}
|
|
172 |
* value represented by the argument in hexadecimal
|
|
173 |
* (base 16).
|
|
174 |
* @since JDK 1.0.2
|
|
175 |
*/
|
|
176 |
public static String toHexString(long i) {
|
|
177 |
return toUnsignedString(i, 4);
|
|
178 |
}
|
|
179 |
|
|
180 |
/**
|
|
181 |
* Returns a string representation of the {@code long}
|
|
182 |
* argument as an unsigned integer in base 8.
|
|
183 |
*
|
|
184 |
* <p>The unsigned {@code long} value is the argument plus
|
|
185 |
* 2<sup>64</sup> if the argument is negative; otherwise, it is
|
|
186 |
* equal to the argument. This value is converted to a string of
|
|
187 |
* ASCII digits in octal (base 8) with no extra leading
|
|
188 |
* {@code 0}s.
|
|
189 |
*
|
|
190 |
* <p>If the unsigned magnitude is zero, it is represented by a
|
|
191 |
* single zero character {@code '0'}
|
|
192 |
* (<code>'\u0030'</code>); otherwise, the first character of
|
|
193 |
* the representation of the unsigned magnitude will not be the
|
|
194 |
* zero character. The following characters are used as octal
|
|
195 |
* digits:
|
|
196 |
*
|
|
197 |
* <blockquote>
|
|
198 |
* {@code 01234567}
|
|
199 |
* </blockquote>
|
|
200 |
*
|
|
201 |
* These are the characters <code>'\u0030'</code> through
|
|
202 |
* <code>'\u0037'</code>.
|
|
203 |
*
|
|
204 |
* @param i a {@code long} to be converted to a string.
|
|
205 |
* @return the string representation of the unsigned {@code long}
|
|
206 |
* value represented by the argument in octal (base 8).
|
|
207 |
* @since JDK 1.0.2
|
|
208 |
*/
|
|
209 |
public static String toOctalString(long i) {
|
|
210 |
return toUnsignedString(i, 3);
|
|
211 |
}
|
|
212 |
|
|
213 |
/**
|
|
214 |
* Returns a string representation of the {@code long}
|
|
215 |
* argument as an unsigned integer in base 2.
|
|
216 |
*
|
|
217 |
* <p>The unsigned {@code long} value is the argument plus
|
|
218 |
* 2<sup>64</sup> if the argument is negative; otherwise, it is
|
|
219 |
* equal to the argument. This value is converted to a string of
|
|
220 |
* ASCII digits in binary (base 2) with no extra leading
|
|
221 |
* {@code 0}s. If the unsigned magnitude is zero, it is
|
|
222 |
* represented by a single zero character {@code '0'}
|
|
223 |
* (<code>'\u0030'</code>); otherwise, the first character of
|
|
224 |
* the representation of the unsigned magnitude will not be the
|
|
225 |
* zero character. The characters {@code '0'}
|
|
226 |
* (<code>'\u0030'</code>) and {@code '1'}
|
|
227 |
* (<code>'\u0031'</code>) are used as binary digits.
|
|
228 |
*
|
|
229 |
* @param i a {@code long} to be converted to a string.
|
|
230 |
* @return the string representation of the unsigned {@code long}
|
|
231 |
* value represented by the argument in binary (base 2).
|
|
232 |
* @since JDK 1.0.2
|
|
233 |
*/
|
|
234 |
public static String toBinaryString(long i) {
|
|
235 |
return toUnsignedString(i, 1);
|
|
236 |
}
|
|
237 |
|
|
238 |
/**
|
|
239 |
* Convert the integer to an unsigned number.
|
|
240 |
*/
|
|
241 |
private static String toUnsignedString(long i, int shift) {
|
|
242 |
char[] buf = new char[64];
|
|
243 |
int charPos = 64;
|
|
244 |
int radix = 1 << shift;
|
|
245 |
long mask = radix - 1;
|
|
246 |
do {
|
|
247 |
buf[--charPos] = Integer.digits[(int)(i & mask)];
|
|
248 |
i >>>= shift;
|
|
249 |
} while (i != 0);
|
|
250 |
return new String(buf, charPos, (64 - charPos));
|
|
251 |
}
|
|
252 |
|
|
253 |
/**
|
|
254 |
* Returns a {@code String} object representing the specified
|
|
255 |
* {@code long}. The argument is converted to signed decimal
|
|
256 |
* representation and returned as a string, exactly as if the
|
|
257 |
* argument and the radix 10 were given as arguments to the {@link
|
|
258 |
* #toString(long, int)} method.
|
|
259 |
*
|
|
260 |
* @param i a {@code long} to be converted.
|
|
261 |
* @return a string representation of the argument in base 10.
|
|
262 |
*/
|
|
263 |
public static String toString(long i) {
|
|
264 |
if (i == Long.MIN_VALUE)
|
|
265 |
return "-9223372036854775808";
|
|
266 |
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
|
|
267 |
char[] buf = new char[size];
|
|
268 |
getChars(i, size, buf);
|
|
269 |
return new String(0, size, buf);
|
|
270 |
}
|
|
271 |
|
|
272 |
/**
|
|
273 |
* Places characters representing the integer i into the
|
|
274 |
* character array buf. The characters are placed into
|
|
275 |
* the buffer backwards starting with the least significant
|
|
276 |
* digit at the specified index (exclusive), and working
|
|
277 |
* backwards from there.
|
|
278 |
*
|
|
279 |
* Will fail if i == Long.MIN_VALUE
|
|
280 |
*/
|
|
281 |
static void getChars(long i, int index, char[] buf) {
|
|
282 |
long q;
|
|
283 |
int r;
|
|
284 |
int charPos = index;
|
|
285 |
char sign = 0;
|
|
286 |
|
|
287 |
if (i < 0) {
|
|
288 |
sign = '-';
|
|
289 |
i = -i;
|
|
290 |
}
|
|
291 |
|
|
292 |
// Get 2 digits/iteration using longs until quotient fits into an int
|
|
293 |
while (i > Integer.MAX_VALUE) {
|
|
294 |
q = i / 100;
|
|
295 |
// really: r = i - (q * 100);
|
|
296 |
r = (int)(i - ((q << 6) + (q << 5) + (q << 2)));
|
|
297 |
i = q;
|
|
298 |
buf[--charPos] = Integer.DigitOnes[r];
|
|
299 |
buf[--charPos] = Integer.DigitTens[r];
|
|
300 |
}
|
|
301 |
|
|
302 |
// Get 2 digits/iteration using ints
|
|
303 |
int q2;
|
|
304 |
int i2 = (int)i;
|
|
305 |
while (i2 >= 65536) {
|
|
306 |
q2 = i2 / 100;
|
|
307 |
// really: r = i2 - (q * 100);
|
|
308 |
r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
|
|
309 |
i2 = q2;
|
|
310 |
buf[--charPos] = Integer.DigitOnes[r];
|
|
311 |
buf[--charPos] = Integer.DigitTens[r];
|
|
312 |
}
|
|
313 |
|
|
314 |
// Fall thru to fast mode for smaller numbers
|
|
315 |
// assert(i2 <= 65536, i2);
|
|
316 |
for (;;) {
|
|
317 |
q2 = (i2 * 52429) >>> (16+3);
|
|
318 |
r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
|
|
319 |
buf[--charPos] = Integer.digits[r];
|
|
320 |
i2 = q2;
|
|
321 |
if (i2 == 0) break;
|
|
322 |
}
|
|
323 |
if (sign != 0) {
|
|
324 |
buf[--charPos] = sign;
|
|
325 |
}
|
|
326 |
}
|
|
327 |
|
|
328 |
// Requires positive x
|
|
329 |
static int stringSize(long x) {
|
|
330 |
long p = 10;
|
|
331 |
for (int i=1; i<19; i++) {
|
|
332 |
if (x < p)
|
|
333 |
return i;
|
|
334 |
p = 10*p;
|
|
335 |
}
|
|
336 |
return 19;
|
|
337 |
}
|
|
338 |
|
|
339 |
/**
|
|
340 |
* Parses the string argument as a signed {@code long} in the
|
|
341 |
* radix specified by the second argument. The characters in the
|
|
342 |
* string must all be digits of the specified radix (as determined
|
|
343 |
* by whether {@link java.lang.Character#digit(char, int)} returns
|
|
344 |
* a nonnegative value), except that the first character may be an
|
|
345 |
* ASCII minus sign {@code '-'} (<code>'\u002D'</code>) to
|
|
346 |
* indicate a negative value or an ASCII plus sign {@code '+'}
|
|
347 |
* (<code>'\u002B'</code>) to indicate a positive value. The
|
|
348 |
* resulting {@code long} value is returned.
|
|
349 |
*
|
|
350 |
* <p>Note that neither the character {@code L}
|
|
351 |
* (<code>'\u004C'</code>) nor {@code l}
|
|
352 |
* (<code>'\u006C'</code>) is permitted to appear at the end
|
|
353 |
* of the string as a type indicator, as would be permitted in
|
|
354 |
* Java programming language source code - except that either
|
|
355 |
* {@code L} or {@code l} may appear as a digit for a
|
|
356 |
* radix greater than 22.
|
|
357 |
*
|
|
358 |
* <p>An exception of type {@code NumberFormatException} is
|
|
359 |
* thrown if any of the following situations occurs:
|
|
360 |
* <ul>
|
|
361 |
*
|
|
362 |
* <li>The first argument is {@code null} or is a string of
|
|
363 |
* length zero.
|
|
364 |
*
|
|
365 |
* <li>The {@code radix} is either smaller than {@link
|
|
366 |
* java.lang.Character#MIN_RADIX} or larger than {@link
|
|
367 |
* java.lang.Character#MAX_RADIX}.
|
|
368 |
*
|
|
369 |
* <li>Any character of the string is not a digit of the specified
|
|
370 |
* radix, except that the first character may be a minus sign
|
|
371 |
* {@code '-'} (<code>'\u002d'</code>) or plus sign {@code
|
|
372 |
* '+'} (<code>'\u002B'</code>) provided that the string is
|
|
373 |
* longer than length 1.
|
|
374 |
*
|
|
375 |
* <li>The value represented by the string is not a value of type
|
|
376 |
* {@code long}.
|
|
377 |
* </ul>
|
|
378 |
*
|
|
379 |
* <p>Examples:
|
|
380 |
* <blockquote><pre>
|
|
381 |
* parseLong("0", 10) returns 0L
|
|
382 |
* parseLong("473", 10) returns 473L
|
|
383 |
* parseLong("+42", 10) returns 42L
|
|
384 |
* parseLong("-0", 10) returns 0L
|
|
385 |
* parseLong("-FF", 16) returns -255L
|
|
386 |
* parseLong("1100110", 2) returns 102L
|
|
387 |
* parseLong("99", 8) throws a NumberFormatException
|
|
388 |
* parseLong("Hazelnut", 10) throws a NumberFormatException
|
|
389 |
* parseLong("Hazelnut", 36) returns 1356099454469L
|
|
390 |
* </pre></blockquote>
|
|
391 |
*
|
|
392 |
* @param s the {@code String} containing the
|
|
393 |
* {@code long} representation to be parsed.
|
|
394 |
* @param radix the radix to be used while parsing {@code s}.
|
|
395 |
* @return the {@code long} represented by the string argument in
|
|
396 |
* the specified radix.
|
|
397 |
* @throws NumberFormatException if the string does not contain a
|
|
398 |
* parsable {@code long}.
|
|
399 |
*/
|
|
400 |
public static long parseLong(String s, int radix)
|
|
401 |
throws NumberFormatException
|
|
402 |
{
|
|
403 |
if (s == null) {
|
|
404 |
throw new NumberFormatException("null");
|
|
405 |
}
|
|
406 |
|
|
407 |
if (radix < Character.MIN_RADIX) {
|
|
408 |
throw new NumberFormatException("radix " + radix +
|
|
409 |
" less than Character.MIN_RADIX");
|
|
410 |
}
|
|
411 |
if (radix > Character.MAX_RADIX) {
|
|
412 |
throw new NumberFormatException("radix " + radix +
|
|
413 |
" greater than Character.MAX_RADIX");
|
|
414 |
}
|
|
415 |
|
|
416 |
long result = 0;
|
|
417 |
boolean negative = false;
|
|
418 |
int i = 0, len = s.length();
|
|
419 |
long limit = -Long.MAX_VALUE;
|
|
420 |
long multmin;
|
|
421 |
int digit;
|
|
422 |
|
|
423 |
if (len > 0) {
|
|
424 |
char firstChar = s.charAt(0);
|
|
425 |
if (firstChar < '0') { // Possible leading "+" or "-"
|
|
426 |
if (firstChar == '-') {
|
|
427 |
negative = true;
|
|
428 |
limit = Long.MIN_VALUE;
|
|
429 |
} else if (firstChar != '+')
|
|
430 |
throw NumberFormatException.forInputString(s);
|
|
431 |
|
|
432 |
if (len == 1) // Cannot have lone "+" or "-"
|
|
433 |
throw NumberFormatException.forInputString(s);
|
|
434 |
i++;
|
|
435 |
}
|
|
436 |
multmin = limit / radix;
|
|
437 |
while (i < len) {
|
|
438 |
// Accumulating negatively avoids surprises near MAX_VALUE
|
|
439 |
digit = Character.digit(s.charAt(i++),radix);
|
|
440 |
if (digit < 0) {
|
|
441 |
throw NumberFormatException.forInputString(s);
|
|
442 |
}
|
|
443 |
if (result < multmin) {
|
|
444 |
throw NumberFormatException.forInputString(s);
|
|
445 |
}
|
|
446 |
result *= radix;
|
|
447 |
if (result < limit + digit) {
|
|
448 |
throw NumberFormatException.forInputString(s);
|
|
449 |
}
|
|
450 |
result -= digit;
|
|
451 |
}
|
|
452 |
} else {
|
|
453 |
throw NumberFormatException.forInputString(s);
|
|
454 |
}
|
|
455 |
return negative ? result : -result;
|
|
456 |
}
|
|
457 |
|
|
458 |
/**
|
|
459 |
* Parses the string argument as a signed decimal {@code long}.
|
|
460 |
* The characters in the string must all be decimal digits, except
|
|
461 |
* that the first character may be an ASCII minus sign {@code '-'}
|
|
462 |
* (<code>\u002D'</code>) to indicate a negative value or an
|
|
463 |
* ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to
|
|
464 |
* indicate a positive value. The resulting {@code long} value is
|
|
465 |
* returned, exactly as if the argument and the radix {@code 10}
|
|
466 |
* were given as arguments to the {@link
|
|
467 |
* #parseLong(java.lang.String, int)} method.
|
|
468 |
*
|
|
469 |
* <p>Note that neither the character {@code L}
|
|
470 |
* (<code>'\u004C'</code>) nor {@code l}
|
|
471 |
* (<code>'\u006C'</code>) is permitted to appear at the end
|
|
472 |
* of the string as a type indicator, as would be permitted in
|
|
473 |
* Java programming language source code.
|
|
474 |
*
|
|
475 |
* @param s a {@code String} containing the {@code long}
|
|
476 |
* representation to be parsed
|
|
477 |
* @return the {@code long} represented by the argument in
|
|
478 |
* decimal.
|
|
479 |
* @throws NumberFormatException if the string does not contain a
|
|
480 |
* parsable {@code long}.
|
|
481 |
*/
|
|
482 |
public static long parseLong(String s) throws NumberFormatException {
|
|
483 |
return parseLong(s, 10);
|
|
484 |
}
|
|
485 |
|
|
486 |
/**
|
|
487 |
* Returns a {@code Long} object holding the value
|
|
488 |
* extracted from the specified {@code String} when parsed
|
|
489 |
* with the radix given by the second argument. The first
|
|
490 |
* argument is interpreted as representing a signed
|
|
491 |
* {@code long} in the radix specified by the second
|
|
492 |
* argument, exactly as if the arguments were given to the {@link
|
|
493 |
* #parseLong(java.lang.String, int)} method. The result is a
|
|
494 |
* {@code Long} object that represents the {@code long}
|
|
495 |
* value specified by the string.
|
|
496 |
*
|
|
497 |
* <p>In other words, this method returns a {@code Long} object equal
|
|
498 |
* to the value of:
|
|
499 |
*
|
|
500 |
* <blockquote>
|
|
501 |
* {@code new Long(Long.parseLong(s, radix))}
|
|
502 |
* </blockquote>
|
|
503 |
*
|
|
504 |
* @param s the string to be parsed
|
|
505 |
* @param radix the radix to be used in interpreting {@code s}
|
|
506 |
* @return a {@code Long} object holding the value
|
|
507 |
* represented by the string argument in the specified
|
|
508 |
* radix.
|
|
509 |
* @throws NumberFormatException If the {@code String} does not
|
|
510 |
* contain a parsable {@code long}.
|
|
511 |
*/
|
|
512 |
public static Long valueOf(String s, int radix) throws NumberFormatException {
|
|
513 |
return new Long(parseLong(s, radix));
|
|
514 |
}
|
|
515 |
|
|
516 |
/**
|
|
517 |
* Returns a {@code Long} object holding the value
|
|
518 |
* of the specified {@code String}. The argument is
|
|
519 |
* interpreted as representing a signed decimal {@code long},
|
|
520 |
* exactly as if the argument were given to the {@link
|
|
521 |
* #parseLong(java.lang.String)} method. The result is a
|
|
522 |
* {@code Long} object that represents the integer value
|
|
523 |
* specified by the string.
|
|
524 |
*
|
|
525 |
* <p>In other words, this method returns a {@code Long} object
|
|
526 |
* equal to the value of:
|
|
527 |
*
|
|
528 |
* <blockquote>
|
|
529 |
* {@code new Long(Long.parseLong(s))}
|
|
530 |
* </blockquote>
|
|
531 |
*
|
|
532 |
* @param s the string to be parsed.
|
|
533 |
* @return a {@code Long} object holding the value
|
|
534 |
* represented by the string argument.
|
|
535 |
* @throws NumberFormatException If the string cannot be parsed
|
|
536 |
* as a {@code long}.
|
|
537 |
*/
|
|
538 |
public static Long valueOf(String s) throws NumberFormatException
|
|
539 |
{
|
|
540 |
return new Long(parseLong(s, 10));
|
|
541 |
}
|
|
542 |
|
|
543 |
private static class LongCache {
|
|
544 |
private LongCache(){}
|
|
545 |
|
|
546 |
static final Long cache[] = new Long[-(-128) + 127 + 1];
|
|
547 |
|
|
548 |
static {
|
|
549 |
for(int i = 0; i < cache.length; i++)
|
|
550 |
cache[i] = new Long(i - 128);
|
|
551 |
}
|
|
552 |
}
|
|
553 |
|
|
554 |
/**
|
|
555 |
* Returns a {@code Long} instance representing the specified
|
|
556 |
* {@code long} value.
|
|
557 |
* If a new {@code Long} instance is not required, this method
|
|
558 |
* should generally be used in preference to the constructor
|
|
559 |
* {@link #Long(long)}, as this method is likely to yield
|
|
560 |
* significantly better space and time performance by caching
|
|
561 |
* frequently requested values.
|
|
562 |
*
|
|
563 |
* @param l a long value.
|
|
564 |
* @return a {@code Long} instance representing {@code l}.
|
|
565 |
* @since 1.5
|
|
566 |
*/
|
|
567 |
public static Long valueOf(long l) {
|
|
568 |
final int offset = 128;
|
|
569 |
if (l >= -128 && l <= 127) { // will cache
|
|
570 |
return LongCache.cache[(int)l + offset];
|
|
571 |
}
|
|
572 |
return new Long(l);
|
|
573 |
}
|
|
574 |
|
|
575 |
/**
|
|
576 |
* Decodes a {@code String} into a {@code Long}.
|
|
577 |
* Accepts decimal, hexadecimal, and octal numbers given by the
|
|
578 |
* following grammar:
|
|
579 |
*
|
|
580 |
* <blockquote>
|
|
581 |
* <dl>
|
|
582 |
* <dt><i>DecodableString:</i>
|
|
583 |
* <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
|
|
584 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
|
|
585 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
|
|
586 |
* <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
|
|
587 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
|
|
588 |
* <p>
|
|
589 |
* <dt><i>Sign:</i>
|
|
590 |
* <dd>{@code -}
|
|
591 |
* <dd>{@code +}
|
|
592 |
* </dl>
|
|
593 |
* </blockquote>
|
|
594 |
*
|
|
595 |
* <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
|
|
596 |
* are defined in <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282">§3.10.1</a>
|
|
597 |
* of the <a href="http://java.sun.com/docs/books/jls/html/">Java
|
|
598 |
* Language Specification</a>.
|
|
599 |
*
|
|
600 |
* <p>The sequence of characters following an optional
|
|
601 |
* sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
|
|
602 |
* "{@code #}", or leading zero) is parsed as by the {@code
|
|
603 |
* Long.parseLong} method with the indicated radix (10, 16, or 8).
|
|
604 |
* This sequence of characters must represent a positive value or
|
|
605 |
* a {@link NumberFormatException} will be thrown. The result is
|
|
606 |
* negated if first character of the specified {@code String} is
|
|
607 |
* the minus sign. No whitespace characters are permitted in the
|
|
608 |
* {@code String}.
|
|
609 |
*
|
|
610 |
* @param nm the {@code String} to decode.
|
|
611 |
* @return a {@code Long} object holding the {@code long}
|
|
612 |
* value represented by {@code nm}
|
|
613 |
* @throws NumberFormatException if the {@code String} does not
|
|
614 |
* contain a parsable {@code long}.
|
|
615 |
* @see java.lang.Long#parseLong(String, int)
|
|
616 |
* @since 1.2
|
|
617 |
*/
|
|
618 |
public static Long decode(String nm) throws NumberFormatException {
|
|
619 |
int radix = 10;
|
|
620 |
int index = 0;
|
|
621 |
boolean negative = false;
|
|
622 |
Long result;
|
|
623 |
|
|
624 |
if (nm.length() == 0)
|
|
625 |
throw new NumberFormatException("Zero length string");
|
|
626 |
char firstChar = nm.charAt(0);
|
|
627 |
// Handle sign, if present
|
|
628 |
if (firstChar == '-') {
|
|
629 |
negative = true;
|
|
630 |
index++;
|
|
631 |
} else if (firstChar == '+')
|
|
632 |
index++;
|
|
633 |
|
|
634 |
// Handle radix specifier, if present
|
|
635 |
if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
|
|
636 |
index += 2;
|
|
637 |
radix = 16;
|
|
638 |
}
|
|
639 |
else if (nm.startsWith("#", index)) {
|
|
640 |
index ++;
|
|
641 |
radix = 16;
|
|
642 |
}
|
|
643 |
else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
|
|
644 |
index ++;
|
|
645 |
radix = 8;
|
|
646 |
}
|
|
647 |
|
|
648 |
if (nm.startsWith("-", index) || nm.startsWith("+", index))
|
|
649 |
throw new NumberFormatException("Sign character in wrong position");
|
|
650 |
|
|
651 |
try {
|
|
652 |
result = Long.valueOf(nm.substring(index), radix);
|
|
653 |
result = negative ? new Long((long)-result.longValue()) : result;
|
|
654 |
} catch (NumberFormatException e) {
|
|
655 |
// If number is Long.MIN_VALUE, we'll end up here. The next line
|
|
656 |
// handles this case, and causes any genuine format error to be
|
|
657 |
// rethrown.
|
|
658 |
String constant = negative ? ("-" + nm.substring(index))
|
|
659 |
: nm.substring(index);
|
|
660 |
result = Long.valueOf(constant, radix);
|
|
661 |
}
|
|
662 |
return result;
|
|
663 |
}
|
|
664 |
|
|
665 |
/**
|
|
666 |
* The value of the {@code Long}.
|
|
667 |
*
|
|
668 |
* @serial
|
|
669 |
*/
|
|
670 |
private final long value;
|
|
671 |
|
|
672 |
/**
|
|
673 |
* Constructs a newly allocated {@code Long} object that
|
|
674 |
* represents the specified {@code long} argument.
|
|
675 |
*
|
|
676 |
* @param value the value to be represented by the
|
|
677 |
* {@code Long} object.
|
|
678 |
*/
|
|
679 |
public Long(long value) {
|
|
680 |
this.value = value;
|
|
681 |
}
|
|
682 |
|
|
683 |
/**
|
|
684 |
* Constructs a newly allocated {@code Long} object that
|
|
685 |
* represents the {@code long} value indicated by the
|
|
686 |
* {@code String} parameter. The string is converted to a
|
|
687 |
* {@code long} value in exactly the manner used by the
|
|
688 |
* {@code parseLong} method for radix 10.
|
|
689 |
*
|
|
690 |
* @param s the {@code String} to be converted to a
|
|
691 |
* {@code Long}.
|
|
692 |
* @throws NumberFormatException if the {@code String} does not
|
|
693 |
* contain a parsable {@code long}.
|
|
694 |
* @see java.lang.Long#parseLong(java.lang.String, int)
|
|
695 |
*/
|
|
696 |
public Long(String s) throws NumberFormatException {
|
|
697 |
this.value = parseLong(s, 10);
|
|
698 |
}
|
|
699 |
|
|
700 |
/**
|
|
701 |
* Returns the value of this {@code Long} as a
|
|
702 |
* {@code byte}.
|
|
703 |
*/
|
|
704 |
public byte byteValue() {
|
|
705 |
return (byte)value;
|
|
706 |
}
|
|
707 |
|
|
708 |
/**
|
|
709 |
* Returns the value of this {@code Long} as a
|
|
710 |
* {@code short}.
|
|
711 |
*/
|
|
712 |
public short shortValue() {
|
|
713 |
return (short)value;
|
|
714 |
}
|
|
715 |
|
|
716 |
/**
|
|
717 |
* Returns the value of this {@code Long} as an
|
|
718 |
* {@code int}.
|
|
719 |
*/
|
|
720 |
public int intValue() {
|
|
721 |
return (int)value;
|
|
722 |
}
|
|
723 |
|
|
724 |
/**
|
|
725 |
* Returns the value of this {@code Long} as a
|
|
726 |
* {@code long} value.
|
|
727 |
*/
|
|
728 |
public long longValue() {
|
|
729 |
return (long)value;
|
|
730 |
}
|
|
731 |
|
|
732 |
/**
|
|
733 |
* Returns the value of this {@code Long} as a
|
|
734 |
* {@code float}.
|
|
735 |
*/
|
|
736 |
public float floatValue() {
|
|
737 |
return (float)value;
|
|
738 |
}
|
|
739 |
|
|
740 |
/**
|
|
741 |
* Returns the value of this {@code Long} as a
|
|
742 |
* {@code double}.
|
|
743 |
*/
|
|
744 |
public double doubleValue() {
|
|
745 |
return (double)value;
|
|
746 |
}
|
|
747 |
|
|
748 |
/**
|
|
749 |
* Returns a {@code String} object representing this
|
|
750 |
* {@code Long}'s value. The value is converted to signed
|
|
751 |
* decimal representation and returned as a string, exactly as if
|
|
752 |
* the {@code long} value were given as an argument to the
|
|
753 |
* {@link java.lang.Long#toString(long)} method.
|
|
754 |
*
|
|
755 |
* @return a string representation of the value of this object in
|
|
756 |
* base 10.
|
|
757 |
*/
|
|
758 |
public String toString() {
|
|
759 |
return String.valueOf(value);
|
|
760 |
}
|
|
761 |
|
|
762 |
/**
|
|
763 |
* Returns a hash code for this {@code Long}. The result is
|
|
764 |
* the exclusive OR of the two halves of the primitive
|
|
765 |
* {@code long} value held by this {@code Long}
|
|
766 |
* object. That is, the hashcode is the value of the expression:
|
|
767 |
*
|
|
768 |
* <blockquote>
|
|
769 |
* {@code (int)(this.longValue()^(this.longValue()>>>32))}
|
|
770 |
* </blockquote>
|
|
771 |
*
|
|
772 |
* @return a hash code value for this object.
|
|
773 |
*/
|
|
774 |
public int hashCode() {
|
|
775 |
return (int)(value ^ (value >>> 32));
|
|
776 |
}
|
|
777 |
|
|
778 |
/**
|
|
779 |
* Compares this object to the specified object. The result is
|
|
780 |
* {@code true} if and only if the argument is not
|
|
781 |
* {@code null} and is a {@code Long} object that
|
|
782 |
* contains the same {@code long} value as this object.
|
|
783 |
*
|
|
784 |
* @param obj the object to compare with.
|
|
785 |
* @return {@code true} if the objects are the same;
|
|
786 |
* {@code false} otherwise.
|
|
787 |
*/
|
|
788 |
public boolean equals(Object obj) {
|
|
789 |
if (obj instanceof Long) {
|
|
790 |
return value == ((Long)obj).longValue();
|
|
791 |
}
|
|
792 |
return false;
|
|
793 |
}
|
|
794 |
|
|
795 |
/**
|
|
796 |
* Determines the {@code long} value of the system property
|
|
797 |
* with the specified name.
|
|
798 |
*
|
|
799 |
* <p>The first argument is treated as the name of a system property.
|
|
800 |
* System properties are accessible through the {@link
|
|
801 |
* java.lang.System#getProperty(java.lang.String)} method. The
|
|
802 |
* string value of this property is then interpreted as a
|
|
803 |
* {@code long} value and a {@code Long} object
|
|
804 |
* representing this value is returned. Details of possible
|
|
805 |
* numeric formats can be found with the definition of
|
|
806 |
* {@code getProperty}.
|
|
807 |
*
|
|
808 |
* <p>If there is no property with the specified name, if the
|
|
809 |
* specified name is empty or {@code null}, or if the
|
|
810 |
* property does not have the correct numeric format, then
|
|
811 |
* {@code null} is returned.
|
|
812 |
*
|
|
813 |
* <p>In other words, this method returns a {@code Long} object equal to
|
|
814 |
* the value of:
|
|
815 |
*
|
|
816 |
* <blockquote>
|
|
817 |
* {@code getLong(nm, null)}
|
|
818 |
* </blockquote>
|
|
819 |
*
|
|
820 |
* @param nm property name.
|
|
821 |
* @return the {@code Long} value of the property.
|
|
822 |
* @see java.lang.System#getProperty(java.lang.String)
|
|
823 |
* @see java.lang.System#getProperty(java.lang.String, java.lang.String)
|
|
824 |
*/
|
|
825 |
public static Long getLong(String nm) {
|
|
826 |
return getLong(nm, null);
|
|
827 |
}
|
|
828 |
|
|
829 |
/**
|
|
830 |
* Determines the {@code long} value of the system property
|
|
831 |
* with the specified name.
|
|
832 |
*
|
|
833 |
* <p>The first argument is treated as the name of a system property.
|
|
834 |
* System properties are accessible through the {@link
|
|
835 |
* java.lang.System#getProperty(java.lang.String)} method. The
|
|
836 |
* string value of this property is then interpreted as a
|
|
837 |
* {@code long} value and a {@code Long} object
|
|
838 |
* representing this value is returned. Details of possible
|
|
839 |
* numeric formats can be found with the definition of
|
|
840 |
* {@code getProperty}.
|
|
841 |
*
|
|
842 |
* <p>The second argument is the default value. A {@code Long} object
|
|
843 |
* that represents the value of the second argument is returned if there
|
|
844 |
* is no property of the specified name, if the property does not have
|
|
845 |
* the correct numeric format, or if the specified name is empty or null.
|
|
846 |
*
|
|
847 |
* <p>In other words, this method returns a {@code Long} object equal
|
|
848 |
* to the value of:
|
|
849 |
*
|
|
850 |
* <blockquote>
|
|
851 |
* {@code getLong(nm, new Long(val))}
|
|
852 |
* </blockquote>
|
|
853 |
*
|
|
854 |
* but in practice it may be implemented in a manner such as:
|
|
855 |
*
|
|
856 |
* <blockquote><pre>
|
|
857 |
* Long result = getLong(nm, null);
|
|
858 |
* return (result == null) ? new Long(val) : result;
|
|
859 |
* </pre></blockquote>
|
|
860 |
*
|
|
861 |
* to avoid the unnecessary allocation of a {@code Long} object when
|
|
862 |
* the default value is not needed.
|
|
863 |
*
|
|
864 |
* @param nm property name.
|
|
865 |
* @param val default value.
|
|
866 |
* @return the {@code Long} value of the property.
|
|
867 |
* @see java.lang.System#getProperty(java.lang.String)
|
|
868 |
* @see java.lang.System#getProperty(java.lang.String, java.lang.String)
|
|
869 |
*/
|
|
870 |
public static Long getLong(String nm, long val) {
|
|
871 |
Long result = Long.getLong(nm, null);
|
|
872 |
return (result == null) ? new Long(val) : result;
|
|
873 |
}
|
|
874 |
|
|
875 |
/**
|
|
876 |
* Returns the {@code long} value of the system property with
|
|
877 |
* the specified name. The first argument is treated as the name
|
|
878 |
* of a system property. System properties are accessible through
|
|
879 |
* the {@link java.lang.System#getProperty(java.lang.String)}
|
|
880 |
* method. The string value of this property is then interpreted
|
|
881 |
* as a {@code long} value, as per the
|
|
882 |
* {@code Long.decode} method, and a {@code Long} object
|
|
883 |
* representing this value is returned.
|
|
884 |
*
|
|
885 |
* <ul>
|
|
886 |
* <li>If the property value begins with the two ASCII characters
|
|
887 |
* {@code 0x} or the ASCII character {@code #}, not followed by
|
|
888 |
* a minus sign, then the rest of it is parsed as a hexadecimal integer
|
|
889 |
* exactly as for the method {@link #valueOf(java.lang.String, int)}
|
|
890 |
* with radix 16.
|
|
891 |
* <li>If the property value begins with the ASCII character
|
|
892 |
* {@code 0} followed by another character, it is parsed as
|
|
893 |
* an octal integer exactly as by the method {@link
|
|
894 |
* #valueOf(java.lang.String, int)} with radix 8.
|
|
895 |
* <li>Otherwise the property value is parsed as a decimal
|
|
896 |
* integer exactly as by the method
|
|
897 |
* {@link #valueOf(java.lang.String, int)} with radix 10.
|
|
898 |
* </ul>
|
|
899 |
*
|
|
900 |
* <p>Note that, in every case, neither {@code L}
|
|
901 |
* (<code>'\u004C'</code>) nor {@code l}
|
|
902 |
* (<code>'\u006C'</code>) is permitted to appear at the end
|
|
903 |
* of the property value as a type indicator, as would be
|
|
904 |
* permitted in Java programming language source code.
|
|
905 |
*
|
|
906 |
* <p>The second argument is the default value. The default value is
|
|
907 |
* returned if there is no property of the specified name, if the
|
|
908 |
* property does not have the correct numeric format, or if the
|
|
909 |
* specified name is empty or {@code null}.
|
|
910 |
*
|
|
911 |
* @param nm property name.
|
|
912 |
* @param val default value.
|
|
913 |
* @return the {@code Long} value of the property.
|
|
914 |
* @see java.lang.System#getProperty(java.lang.String)
|
|
915 |
* @see java.lang.System#getProperty(java.lang.String, java.lang.String)
|
|
916 |
* @see java.lang.Long#decode
|
|
917 |
*/
|
|
918 |
public static Long getLong(String nm, Long val) {
|
|
919 |
String v = null;
|
|
920 |
try {
|
|
921 |
v = System.getProperty(nm);
|
|
922 |
} catch (IllegalArgumentException e) {
|
|
923 |
} catch (NullPointerException e) {
|
|
924 |
}
|
|
925 |
if (v != null) {
|
|
926 |
try {
|
|
927 |
return Long.decode(v);
|
|
928 |
} catch (NumberFormatException e) {
|
|
929 |
}
|
|
930 |
}
|
|
931 |
return val;
|
|
932 |
}
|
|
933 |
|
|
934 |
/**
|
|
935 |
* Compares two {@code Long} objects numerically.
|
|
936 |
*
|
|
937 |
* @param anotherLong the {@code Long} to be compared.
|
|
938 |
* @return the value {@code 0} if this {@code Long} is
|
|
939 |
* equal to the argument {@code Long}; a value less than
|
|
940 |
* {@code 0} if this {@code Long} is numerically less
|
|
941 |
* than the argument {@code Long}; and a value greater
|
|
942 |
* than {@code 0} if this {@code Long} is numerically
|
|
943 |
* greater than the argument {@code Long} (signed
|
|
944 |
* comparison).
|
|
945 |
* @since 1.2
|
|
946 |
*/
|
|
947 |
public int compareTo(Long anotherLong) {
|
|
948 |
long thisVal = this.value;
|
|
949 |
long anotherVal = anotherLong.value;
|
|
950 |
return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
|
|
951 |
}
|
|
952 |
|
|
953 |
|
|
954 |
// Bit Twiddling
|
|
955 |
|
|
956 |
/**
|
|
957 |
* The number of bits used to represent a {@code long} value in two's
|
|
958 |
* complement binary form.
|
|
959 |
*
|
|
960 |
* @since 1.5
|
|
961 |
*/
|
|
962 |
public static final int SIZE = 64;
|
|
963 |
|
|
964 |
/**
|
|
965 |
* Returns a {@code long} value with at most a single one-bit, in the
|
|
966 |
* position of the highest-order ("leftmost") one-bit in the specified
|
|
967 |
* {@code long} value. Returns zero if the specified value has no
|
|
968 |
* one-bits in its two's complement binary representation, that is, if it
|
|
969 |
* is equal to zero.
|
|
970 |
*
|
|
971 |
* @return a {@code long} value with a single one-bit, in the position
|
|
972 |
* of the highest-order one-bit in the specified value, or zero if
|
|
973 |
* the specified value is itself equal to zero.
|
|
974 |
* @since 1.5
|
|
975 |
*/
|
|
976 |
public static long highestOneBit(long i) {
|
|
977 |
// HD, Figure 3-1
|
|
978 |
i |= (i >> 1);
|
|
979 |
i |= (i >> 2);
|
|
980 |
i |= (i >> 4);
|
|
981 |
i |= (i >> 8);
|
|
982 |
i |= (i >> 16);
|
|
983 |
i |= (i >> 32);
|
|
984 |
return i - (i >>> 1);
|
|
985 |
}
|
|
986 |
|
|
987 |
/**
|
|
988 |
* Returns a {@code long} value with at most a single one-bit, in the
|
|
989 |
* position of the lowest-order ("rightmost") one-bit in the specified
|
|
990 |
* {@code long} value. Returns zero if the specified value has no
|
|
991 |
* one-bits in its two's complement binary representation, that is, if it
|
|
992 |
* is equal to zero.
|
|
993 |
*
|
|
994 |
* @return a {@code long} value with a single one-bit, in the position
|
|
995 |
* of the lowest-order one-bit in the specified value, or zero if
|
|
996 |
* the specified value is itself equal to zero.
|
|
997 |
* @since 1.5
|
|
998 |
*/
|
|
999 |
public static long lowestOneBit(long i) {
|
|
1000 |
// HD, Section 2-1
|
|
1001 |
return i & -i;
|
|
1002 |
}
|
|
1003 |
|
|
1004 |
/**
|
|
1005 |
* Returns the number of zero bits preceding the highest-order
|
|
1006 |
* ("leftmost") one-bit in the two's complement binary representation
|
|
1007 |
* of the specified {@code long} value. Returns 64 if the
|
|
1008 |
* specified value has no one-bits in its two's complement representation,
|
|
1009 |
* in other words if it is equal to zero.
|
|
1010 |
*
|
|
1011 |
* <p>Note that this method is closely related to the logarithm base 2.
|
|
1012 |
* For all positive {@code long} values x:
|
|
1013 |
* <ul>
|
|
1014 |
* <li>floor(log<sub>2</sub>(x)) = {@code 63 - numberOfLeadingZeros(x)}
|
|
1015 |
* <li>ceil(log<sub>2</sub>(x)) = {@code 64 - numberOfLeadingZeros(x - 1)}
|
|
1016 |
* </ul>
|
|
1017 |
*
|
|
1018 |
* @return the number of zero bits preceding the highest-order
|
|
1019 |
* ("leftmost") one-bit in the two's complement binary representation
|
|
1020 |
* of the specified {@code long} value, or 64 if the value
|
|
1021 |
* is equal to zero.
|
|
1022 |
* @since 1.5
|
|
1023 |
*/
|
|
1024 |
public static int numberOfLeadingZeros(long i) {
|
|
1025 |
// HD, Figure 5-6
|
|
1026 |
if (i == 0)
|
|
1027 |
return 64;
|
|
1028 |
int n = 1;
|
|
1029 |
int x = (int)(i >>> 32);
|
|
1030 |
if (x == 0) { n += 32; x = (int)i; }
|
|
1031 |
if (x >>> 16 == 0) { n += 16; x <<= 16; }
|
|
1032 |
if (x >>> 24 == 0) { n += 8; x <<= 8; }
|
|
1033 |
if (x >>> 28 == 0) { n += 4; x <<= 4; }
|
|
1034 |
if (x >>> 30 == 0) { n += 2; x <<= 2; }
|
|
1035 |
n -= x >>> 31;
|
|
1036 |
return n;
|
|
1037 |
}
|
|
1038 |
|
|
1039 |
/**
|
|
1040 |
* Returns the number of zero bits following the lowest-order ("rightmost")
|
|
1041 |
* one-bit in the two's complement binary representation of the specified
|
|
1042 |
* {@code long} value. Returns 64 if the specified value has no
|
|
1043 |
* one-bits in its two's complement representation, in other words if it is
|
|
1044 |
* equal to zero.
|
|
1045 |
*
|
|
1046 |
* @return the number of zero bits following the lowest-order ("rightmost")
|
|
1047 |
* one-bit in the two's complement binary representation of the
|
|
1048 |
* specified {@code long} value, or 64 if the value is equal
|
|
1049 |
* to zero.
|
|
1050 |
* @since 1.5
|
|
1051 |
*/
|
|
1052 |
public static int numberOfTrailingZeros(long i) {
|
|
1053 |
// HD, Figure 5-14
|
|
1054 |
int x, y;
|
|
1055 |
if (i == 0) return 64;
|
|
1056 |
int n = 63;
|
|
1057 |
y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32);
|
|
1058 |
y = x <<16; if (y != 0) { n = n -16; x = y; }
|
|
1059 |
y = x << 8; if (y != 0) { n = n - 8; x = y; }
|
|
1060 |
y = x << 4; if (y != 0) { n = n - 4; x = y; }
|
|
1061 |
y = x << 2; if (y != 0) { n = n - 2; x = y; }
|
|
1062 |
return n - ((x << 1) >>> 31);
|
|
1063 |
}
|
|
1064 |
|
|
1065 |
/**
|
|
1066 |
* Returns the number of one-bits in the two's complement binary
|
|
1067 |
* representation of the specified {@code long} value. This function is
|
|
1068 |
* sometimes referred to as the <i>population count</i>.
|
|
1069 |
*
|
|
1070 |
* @return the number of one-bits in the two's complement binary
|
|
1071 |
* representation of the specified {@code long} value.
|
|
1072 |
* @since 1.5
|
|
1073 |
*/
|
|
1074 |
public static int bitCount(long i) {
|
|
1075 |
// HD, Figure 5-14
|
|
1076 |
i = i - ((i >>> 1) & 0x5555555555555555L);
|
|
1077 |
i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L);
|
|
1078 |
i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
|
|
1079 |
i = i + (i >>> 8);
|
|
1080 |
i = i + (i >>> 16);
|
|
1081 |
i = i + (i >>> 32);
|
|
1082 |
return (int)i & 0x7f;
|
|
1083 |
}
|
|
1084 |
|
|
1085 |
/**
|
|
1086 |
* Returns the value obtained by rotating the two's complement binary
|
|
1087 |
* representation of the specified {@code long} value left by the
|
|
1088 |
* specified number of bits. (Bits shifted out of the left hand, or
|
|
1089 |
* high-order, side reenter on the right, or low-order.)
|
|
1090 |
*
|
|
1091 |
* <p>Note that left rotation with a negative distance is equivalent to
|
|
1092 |
* right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
|
|
1093 |
* distance)}. Note also that rotation by any multiple of 64 is a
|
|
1094 |
* no-op, so all but the last six bits of the rotation distance can be
|
|
1095 |
* ignored, even if the distance is negative: {@code rotateLeft(val,
|
|
1096 |
* distance) == rotateLeft(val, distance & 0x3F)}.
|
|
1097 |
*
|
|
1098 |
* @return the value obtained by rotating the two's complement binary
|
|
1099 |
* representation of the specified {@code long} value left by the
|
|
1100 |
* specified number of bits.
|
|
1101 |
* @since 1.5
|
|
1102 |
*/
|
|
1103 |
public static long rotateLeft(long i, int distance) {
|
|
1104 |
return (i << distance) | (i >>> -distance);
|
|
1105 |
}
|
|
1106 |
|
|
1107 |
/**
|
|
1108 |
* Returns the value obtained by rotating the two's complement binary
|
|
1109 |
* representation of the specified {@code long} value right by the
|
|
1110 |
* specified number of bits. (Bits shifted out of the right hand, or
|
|
1111 |
* low-order, side reenter on the left, or high-order.)
|
|
1112 |
*
|
|
1113 |
* <p>Note that right rotation with a negative distance is equivalent to
|
|
1114 |
* left rotation: {@code rotateRight(val, -distance) == rotateLeft(val,
|
|
1115 |
* distance)}. Note also that rotation by any multiple of 64 is a
|
|
1116 |
* no-op, so all but the last six bits of the rotation distance can be
|
|
1117 |
* ignored, even if the distance is negative: {@code rotateRight(val,
|
|
1118 |
* distance) == rotateRight(val, distance & 0x3F)}.
|
|
1119 |
*
|
|
1120 |
* @return the value obtained by rotating the two's complement binary
|
|
1121 |
* representation of the specified {@code long} value right by the
|
|
1122 |
* specified number of bits.
|
|
1123 |
* @since 1.5
|
|
1124 |
*/
|
|
1125 |
public static long rotateRight(long i, int distance) {
|
|
1126 |
return (i >>> distance) | (i << -distance);
|
|
1127 |
}
|
|
1128 |
|
|
1129 |
/**
|
|
1130 |
* Returns the value obtained by reversing the order of the bits in the
|
|
1131 |
* two's complement binary representation of the specified {@code long}
|
|
1132 |
* value.
|
|
1133 |
*
|
|
1134 |
* @return the value obtained by reversing order of the bits in the
|
|
1135 |
* specified {@code long} value.
|
|
1136 |
* @since 1.5
|
|
1137 |
*/
|
|
1138 |
public static long reverse(long i) {
|
|
1139 |
// HD, Figure 7-1
|
|
1140 |
i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L;
|
|
1141 |
i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L;
|
|
1142 |
i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL;
|
|
1143 |
i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
|
|
1144 |
i = (i << 48) | ((i & 0xffff0000L) << 16) |
|
|
1145 |
((i >>> 16) & 0xffff0000L) | (i >>> 48);
|
|
1146 |
return i;
|
|
1147 |
}
|
|
1148 |
|
|
1149 |
/**
|
|
1150 |
* Returns the signum function of the specified {@code long} value. (The
|
|
1151 |
* return value is -1 if the specified value is negative; 0 if the
|
|
1152 |
* specified value is zero; and 1 if the specified value is positive.)
|
|
1153 |
*
|
|
1154 |
* @return the signum function of the specified {@code long} value.
|
|
1155 |
* @since 1.5
|
|
1156 |
*/
|
|
1157 |
public static int signum(long i) {
|
|
1158 |
// HD, Section 2-7
|
|
1159 |
return (int) ((i >> 63) | (-i >>> 63));
|
|
1160 |
}
|
|
1161 |
|
|
1162 |
/**
|
|
1163 |
* Returns the value obtained by reversing the order of the bytes in the
|
|
1164 |
* two's complement representation of the specified {@code long} value.
|
|
1165 |
*
|
|
1166 |
* @return the value obtained by reversing the bytes in the specified
|
|
1167 |
* {@code long} value.
|
|
1168 |
* @since 1.5
|
|
1169 |
*/
|
|
1170 |
public static long reverseBytes(long i) {
|
|
1171 |
i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
|
|
1172 |
return (i << 48) | ((i & 0xffff0000L) << 16) |
|
|
1173 |
((i >>> 16) & 0xffff0000L) | (i >>> 48);
|
|
1174 |
}
|
|
1175 |
|
|
1176 |
/** use serialVersionUID from JDK 1.0.2 for interoperability */
|
|
1177 |
private static final long serialVersionUID = 4290774380558885855L;
|
|
1178 |
}
|