1036 * Returns a hash code for this {@code Long}. The result is 1037 * the exclusive OR of the two halves of the primitive 1038 * {@code long} value held by this {@code Long} 1039 * object. That is, the hashcode is the value of the expression: 1040 * 1041 * <blockquote> 1042 * {@code (int)(this.longValue()^(this.longValue()>>>32))} 1043 * </blockquote> 1044 * 1045 * @return a hash code value for this object. 1046 */ 1047 @Override 1048 public int hashCode() { 1049 return Long.hashCode(value); 1050 } 1051 1052 /** 1053 * Returns a hash code for a {@code long} value; compatible with 1054 * {@code Long.hashCode()}. 1055 * 1056 * @since 1.8 1057 * 1058 * @return a hash code value for a {@code long} value. 1059 */ 1060 public static int hashCode(long value) { 1061 return (int)(value ^ (value >>> 32)); 1062 } 1063 1064 /** 1065 * Compares this object to the specified object. The result is 1066 * {@code true} if and only if the argument is not 1067 * {@code null} and is a {@code Long} object that 1068 * contains the same {@code long} value as this object. 1069 * 1070 * @param obj the object to compare with. 1071 * @return {@code true} if the objects are the same; 1072 * {@code false} otherwise. 1073 */ 1074 public boolean equals(Object obj) { 1075 if (obj instanceof Long) { 1076 return value == ((Long)obj).longValue(); 1077 } 1078 return false; 1340 * 1341 * @since 1.5 1342 */ 1343 @Native public static final int SIZE = 64; 1344 1345 /** 1346 * The number of bytes used to represent a {@code long} value in two's 1347 * complement binary form. 1348 * 1349 * @since 1.8 1350 */ 1351 public static final int BYTES = SIZE / Byte.SIZE; 1352 1353 /** 1354 * Returns a {@code long} value with at most a single one-bit, in the 1355 * position of the highest-order ("leftmost") one-bit in the specified 1356 * {@code long} value. Returns zero if the specified value has no 1357 * one-bits in its two's complement binary representation, that is, if it 1358 * is equal to zero. 1359 * 1360 * @return a {@code long} value with a single one-bit, in the position 1361 * of the highest-order one-bit in the specified value, or zero if 1362 * the specified value is itself equal to zero. 1363 * @since 1.5 1364 */ 1365 public static long highestOneBit(long i) { 1366 // HD, Figure 3-1 1367 i |= (i >> 1); 1368 i |= (i >> 2); 1369 i |= (i >> 4); 1370 i |= (i >> 8); 1371 i |= (i >> 16); 1372 i |= (i >> 32); 1373 return i - (i >>> 1); 1374 } 1375 1376 /** 1377 * Returns a {@code long} value with at most a single one-bit, in the 1378 * position of the lowest-order ("rightmost") one-bit in the specified 1379 * {@code long} value. Returns zero if the specified value has no 1380 * one-bits in its two's complement binary representation, that is, if it 1381 * is equal to zero. 1382 * 1383 * @return a {@code long} value with a single one-bit, in the position 1384 * of the lowest-order one-bit in the specified value, or zero if 1385 * the specified value is itself equal to zero. 1386 * @since 1.5 1387 */ 1388 public static long lowestOneBit(long i) { 1389 // HD, Section 2-1 1390 return i & -i; 1391 } 1392 1393 /** 1394 * Returns the number of zero bits preceding the highest-order 1395 * ("leftmost") one-bit in the two's complement binary representation 1396 * of the specified {@code long} value. Returns 64 if the 1397 * specified value has no one-bits in its two's complement representation, 1398 * in other words if it is equal to zero. 1399 * 1400 * <p>Note that this method is closely related to the logarithm base 2. 1401 * For all positive {@code long} values x: 1402 * <ul> 1403 * <li>floor(log<sub>2</sub>(x)) = {@code 63 - numberOfLeadingZeros(x)} 1404 * <li>ceil(log<sub>2</sub>(x)) = {@code 64 - numberOfLeadingZeros(x - 1)} 1405 * </ul> 1406 * 1407 * @return the number of zero bits preceding the highest-order 1408 * ("leftmost") one-bit in the two's complement binary representation 1409 * of the specified {@code long} value, or 64 if the value 1410 * is equal to zero. 1411 * @since 1.5 1412 */ 1413 public static int numberOfLeadingZeros(long i) { 1414 // HD, Figure 5-6 1415 if (i == 0) 1416 return 64; 1417 int n = 1; 1418 int x = (int)(i >>> 32); 1419 if (x == 0) { n += 32; x = (int)i; } 1420 if (x >>> 16 == 0) { n += 16; x <<= 16; } 1421 if (x >>> 24 == 0) { n += 8; x <<= 8; } 1422 if (x >>> 28 == 0) { n += 4; x <<= 4; } 1423 if (x >>> 30 == 0) { n += 2; x <<= 2; } 1424 n -= x >>> 31; 1425 return n; 1426 } 1427 1428 /** 1429 * Returns the number of zero bits following the lowest-order ("rightmost") 1430 * one-bit in the two's complement binary representation of the specified 1431 * {@code long} value. Returns 64 if the specified value has no 1432 * one-bits in its two's complement representation, in other words if it is 1433 * equal to zero. 1434 * 1435 * @return the number of zero bits following the lowest-order ("rightmost") 1436 * one-bit in the two's complement binary representation of the 1437 * specified {@code long} value, or 64 if the value is equal 1438 * to zero. 1439 * @since 1.5 1440 */ 1441 public static int numberOfTrailingZeros(long i) { 1442 // HD, Figure 5-14 1443 int x, y; 1444 if (i == 0) return 64; 1445 int n = 63; 1446 y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32); 1447 y = x <<16; if (y != 0) { n = n -16; x = y; } 1448 y = x << 8; if (y != 0) { n = n - 8; x = y; } 1449 y = x << 4; if (y != 0) { n = n - 4; x = y; } 1450 y = x << 2; if (y != 0) { n = n - 2; x = y; } 1451 return n - ((x << 1) >>> 31); 1452 } 1453 1454 /** 1455 * Returns the number of one-bits in the two's complement binary 1456 * representation of the specified {@code long} value. This function is 1457 * sometimes referred to as the <i>population count</i>. 1458 * 1459 * @return the number of one-bits in the two's complement binary 1460 * representation of the specified {@code long} value. 1461 * @since 1.5 1462 */ 1463 public static int bitCount(long i) { 1464 // HD, Figure 5-14 1465 i = i - ((i >>> 1) & 0x5555555555555555L); 1466 i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L); 1467 i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL; 1468 i = i + (i >>> 8); 1469 i = i + (i >>> 16); 1470 i = i + (i >>> 32); 1471 return (int)i & 0x7f; 1472 } 1473 1474 /** 1475 * Returns the value obtained by rotating the two's complement binary 1476 * representation of the specified {@code long} value left by the 1477 * specified number of bits. (Bits shifted out of the left hand, or 1478 * high-order, side reenter on the right, or low-order.) 1479 * 1480 * <p>Note that left rotation with a negative distance is equivalent to 1481 * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val, 1482 * distance)}. Note also that rotation by any multiple of 64 is a 1483 * no-op, so all but the last six bits of the rotation distance can be 1484 * ignored, even if the distance is negative: {@code rotateLeft(val, 1485 * distance) == rotateLeft(val, distance & 0x3F)}. 1486 * 1487 * @return the value obtained by rotating the two's complement binary 1488 * representation of the specified {@code long} value left by the 1489 * specified number of bits. 1490 * @since 1.5 1491 */ 1492 public static long rotateLeft(long i, int distance) { 1493 return (i << distance) | (i >>> -distance); 1494 } 1495 1496 /** 1497 * Returns the value obtained by rotating the two's complement binary 1498 * representation of the specified {@code long} value right by the 1499 * specified number of bits. (Bits shifted out of the right hand, or 1500 * low-order, side reenter on the left, or high-order.) 1501 * 1502 * <p>Note that right rotation with a negative distance is equivalent to 1503 * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val, 1504 * distance)}. Note also that rotation by any multiple of 64 is a 1505 * no-op, so all but the last six bits of the rotation distance can be 1506 * ignored, even if the distance is negative: {@code rotateRight(val, 1507 * distance) == rotateRight(val, distance & 0x3F)}. 1508 * 1509 * @return the value obtained by rotating the two's complement binary 1510 * representation of the specified {@code long} value right by the 1511 * specified number of bits. 1512 * @since 1.5 1513 */ 1514 public static long rotateRight(long i, int distance) { 1515 return (i >>> distance) | (i << -distance); 1516 } 1517 1518 /** 1519 * Returns the value obtained by reversing the order of the bits in the 1520 * two's complement binary representation of the specified {@code long} 1521 * value. 1522 * 1523 * @return the value obtained by reversing order of the bits in the 1524 * specified {@code long} value. 1525 * @since 1.5 1526 */ 1527 public static long reverse(long i) { 1528 // HD, Figure 7-1 1529 i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L; 1530 i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L; 1531 i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL; 1532 i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; 1533 i = (i << 48) | ((i & 0xffff0000L) << 16) | 1534 ((i >>> 16) & 0xffff0000L) | (i >>> 48); 1535 return i; 1536 } 1537 1538 /** 1539 * Returns the signum function of the specified {@code long} value. (The 1540 * return value is -1 if the specified value is negative; 0 if the 1541 * specified value is zero; and 1 if the specified value is positive.) 1542 * 1543 * @return the signum function of the specified {@code long} value. 1544 * @since 1.5 1545 */ 1546 public static int signum(long i) { 1547 // HD, Section 2-7 1548 return (int) ((i >> 63) | (-i >>> 63)); 1549 } 1550 1551 /** 1552 * Returns the value obtained by reversing the order of the bytes in the 1553 * two's complement representation of the specified {@code long} value. 1554 * 1555 * @return the value obtained by reversing the bytes in the specified 1556 * {@code long} value. 1557 * @since 1.5 1558 */ 1559 public static long reverseBytes(long i) { 1560 i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; 1561 return (i << 48) | ((i & 0xffff0000L) << 16) | 1562 ((i >>> 16) & 0xffff0000L) | (i >>> 48); 1563 } 1564 1565 /** 1566 * Adds two {@code long} values together as per the + operator. 1567 * 1568 * @param a the first operand 1569 * @param b the second operand 1570 * @return the sum of {@code a} and {@code b} 1571 * @see java.util.function.BinaryOperator 1572 * @since 1.8 1573 */ 1574 public static long sum(long a, long b) { | 1036 * Returns a hash code for this {@code Long}. The result is 1037 * the exclusive OR of the two halves of the primitive 1038 * {@code long} value held by this {@code Long} 1039 * object. That is, the hashcode is the value of the expression: 1040 * 1041 * <blockquote> 1042 * {@code (int)(this.longValue()^(this.longValue()>>>32))} 1043 * </blockquote> 1044 * 1045 * @return a hash code value for this object. 1046 */ 1047 @Override 1048 public int hashCode() { 1049 return Long.hashCode(value); 1050 } 1051 1052 /** 1053 * Returns a hash code for a {@code long} value; compatible with 1054 * {@code Long.hashCode()}. 1055 * 1056 * @param value the value to hash 1057 * @return a hash code value for a {@code long} value. 1058 * @since 1.8 1059 */ 1060 public static int hashCode(long value) { 1061 return (int)(value ^ (value >>> 32)); 1062 } 1063 1064 /** 1065 * Compares this object to the specified object. The result is 1066 * {@code true} if and only if the argument is not 1067 * {@code null} and is a {@code Long} object that 1068 * contains the same {@code long} value as this object. 1069 * 1070 * @param obj the object to compare with. 1071 * @return {@code true} if the objects are the same; 1072 * {@code false} otherwise. 1073 */ 1074 public boolean equals(Object obj) { 1075 if (obj instanceof Long) { 1076 return value == ((Long)obj).longValue(); 1077 } 1078 return false; 1340 * 1341 * @since 1.5 1342 */ 1343 @Native public static final int SIZE = 64; 1344 1345 /** 1346 * The number of bytes used to represent a {@code long} value in two's 1347 * complement binary form. 1348 * 1349 * @since 1.8 1350 */ 1351 public static final int BYTES = SIZE / Byte.SIZE; 1352 1353 /** 1354 * Returns a {@code long} value with at most a single one-bit, in the 1355 * position of the highest-order ("leftmost") one-bit in the specified 1356 * {@code long} value. Returns zero if the specified value has no 1357 * one-bits in its two's complement binary representation, that is, if it 1358 * is equal to zero. 1359 * 1360 * @param i the value whose highest one bit is to be computed 1361 * @return a {@code long} value with a single one-bit, in the position 1362 * of the highest-order one-bit in the specified value, or zero if 1363 * the specified value is itself equal to zero. 1364 * @since 1.5 1365 */ 1366 public static long highestOneBit(long i) { 1367 // HD, Figure 3-1 1368 i |= (i >> 1); 1369 i |= (i >> 2); 1370 i |= (i >> 4); 1371 i |= (i >> 8); 1372 i |= (i >> 16); 1373 i |= (i >> 32); 1374 return i - (i >>> 1); 1375 } 1376 1377 /** 1378 * Returns a {@code long} value with at most a single one-bit, in the 1379 * position of the lowest-order ("rightmost") one-bit in the specified 1380 * {@code long} value. Returns zero if the specified value has no 1381 * one-bits in its two's complement binary representation, that is, if it 1382 * is equal to zero. 1383 * 1384 * @param i the value whose lowest one bit is to be computed 1385 * @return a {@code long} value with a single one-bit, in the position 1386 * of the lowest-order one-bit in the specified value, or zero if 1387 * the specified value is itself equal to zero. 1388 * @since 1.5 1389 */ 1390 public static long lowestOneBit(long i) { 1391 // HD, Section 2-1 1392 return i & -i; 1393 } 1394 1395 /** 1396 * Returns the number of zero bits preceding the highest-order 1397 * ("leftmost") one-bit in the two's complement binary representation 1398 * of the specified {@code long} value. Returns 64 if the 1399 * specified value has no one-bits in its two's complement representation, 1400 * in other words if it is equal to zero. 1401 * 1402 * <p>Note that this method is closely related to the logarithm base 2. 1403 * For all positive {@code long} values x: 1404 * <ul> 1405 * <li>floor(log<sub>2</sub>(x)) = {@code 63 - numberOfLeadingZeros(x)} 1406 * <li>ceil(log<sub>2</sub>(x)) = {@code 64 - numberOfLeadingZeros(x - 1)} 1407 * </ul> 1408 * 1409 * @param i the value whose number of leading zeros is to be computed 1410 * @return the number of zero bits preceding the highest-order 1411 * ("leftmost") one-bit in the two's complement binary representation 1412 * of the specified {@code long} value, or 64 if the value 1413 * is equal to zero. 1414 * @since 1.5 1415 */ 1416 public static int numberOfLeadingZeros(long i) { 1417 // HD, Figure 5-6 1418 if (i == 0) 1419 return 64; 1420 int n = 1; 1421 int x = (int)(i >>> 32); 1422 if (x == 0) { n += 32; x = (int)i; } 1423 if (x >>> 16 == 0) { n += 16; x <<= 16; } 1424 if (x >>> 24 == 0) { n += 8; x <<= 8; } 1425 if (x >>> 28 == 0) { n += 4; x <<= 4; } 1426 if (x >>> 30 == 0) { n += 2; x <<= 2; } 1427 n -= x >>> 31; 1428 return n; 1429 } 1430 1431 /** 1432 * Returns the number of zero bits following the lowest-order ("rightmost") 1433 * one-bit in the two's complement binary representation of the specified 1434 * {@code long} value. Returns 64 if the specified value has no 1435 * one-bits in its two's complement representation, in other words if it is 1436 * equal to zero. 1437 * 1438 * @param i the value whose number of trailing zeros is to be computed 1439 * @return the number of zero bits following the lowest-order ("rightmost") 1440 * one-bit in the two's complement binary representation of the 1441 * specified {@code long} value, or 64 if the value is equal 1442 * to zero. 1443 * @since 1.5 1444 */ 1445 public static int numberOfTrailingZeros(long i) { 1446 // HD, Figure 5-14 1447 int x, y; 1448 if (i == 0) return 64; 1449 int n = 63; 1450 y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32); 1451 y = x <<16; if (y != 0) { n = n -16; x = y; } 1452 y = x << 8; if (y != 0) { n = n - 8; x = y; } 1453 y = x << 4; if (y != 0) { n = n - 4; x = y; } 1454 y = x << 2; if (y != 0) { n = n - 2; x = y; } 1455 return n - ((x << 1) >>> 31); 1456 } 1457 1458 /** 1459 * Returns the number of one-bits in the two's complement binary 1460 * representation of the specified {@code long} value. This function is 1461 * sometimes referred to as the <i>population count</i>. 1462 * 1463 * @param i the value whose bits are to be counted 1464 * @return the number of one-bits in the two's complement binary 1465 * representation of the specified {@code long} value. 1466 * @since 1.5 1467 */ 1468 public static int bitCount(long i) { 1469 // HD, Figure 5-14 1470 i = i - ((i >>> 1) & 0x5555555555555555L); 1471 i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L); 1472 i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL; 1473 i = i + (i >>> 8); 1474 i = i + (i >>> 16); 1475 i = i + (i >>> 32); 1476 return (int)i & 0x7f; 1477 } 1478 1479 /** 1480 * Returns the value obtained by rotating the two's complement binary 1481 * representation of the specified {@code long} value left by the 1482 * specified number of bits. (Bits shifted out of the left hand, or 1483 * high-order, side reenter on the right, or low-order.) 1484 * 1485 * <p>Note that left rotation with a negative distance is equivalent to 1486 * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val, 1487 * distance)}. Note also that rotation by any multiple of 64 is a 1488 * no-op, so all but the last six bits of the rotation distance can be 1489 * ignored, even if the distance is negative: {@code rotateLeft(val, 1490 * distance) == rotateLeft(val, distance & 0x3F)}. 1491 * 1492 * @param i the value whose bits are to be rotated left 1493 * @param distance the number of bit positions to rotate left 1494 * @return the value obtained by rotating the two's complement binary 1495 * representation of the specified {@code long} value left by the 1496 * specified number of bits. 1497 * @since 1.5 1498 */ 1499 public static long rotateLeft(long i, int distance) { 1500 return (i << distance) | (i >>> -distance); 1501 } 1502 1503 /** 1504 * Returns the value obtained by rotating the two's complement binary 1505 * representation of the specified {@code long} value right by the 1506 * specified number of bits. (Bits shifted out of the right hand, or 1507 * low-order, side reenter on the left, or high-order.) 1508 * 1509 * <p>Note that right rotation with a negative distance is equivalent to 1510 * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val, 1511 * distance)}. Note also that rotation by any multiple of 64 is a 1512 * no-op, so all but the last six bits of the rotation distance can be 1513 * ignored, even if the distance is negative: {@code rotateRight(val, 1514 * distance) == rotateRight(val, distance & 0x3F)}. 1515 * 1516 * @param i the value whose bits are to be rotated right 1517 * @param distance the number of bit positions to rotate right 1518 * @return the value obtained by rotating the two's complement binary 1519 * representation of the specified {@code long} value right by the 1520 * specified number of bits. 1521 * @since 1.5 1522 */ 1523 public static long rotateRight(long i, int distance) { 1524 return (i >>> distance) | (i << -distance); 1525 } 1526 1527 /** 1528 * Returns the value obtained by reversing the order of the bits in the 1529 * two's complement binary representation of the specified {@code long} 1530 * value. 1531 * 1532 * @param i the value to be reversed 1533 * @return the value obtained by reversing order of the bits in the 1534 * specified {@code long} value. 1535 * @since 1.5 1536 */ 1537 public static long reverse(long i) { 1538 // HD, Figure 7-1 1539 i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L; 1540 i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L; 1541 i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL; 1542 i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; 1543 i = (i << 48) | ((i & 0xffff0000L) << 16) | 1544 ((i >>> 16) & 0xffff0000L) | (i >>> 48); 1545 return i; 1546 } 1547 1548 /** 1549 * Returns the signum function of the specified {@code long} value. (The 1550 * return value is -1 if the specified value is negative; 0 if the 1551 * specified value is zero; and 1 if the specified value is positive.) 1552 * 1553 * @param i the value whose signum is to be computed 1554 * @return the signum function of the specified {@code long} value. 1555 * @since 1.5 1556 */ 1557 public static int signum(long i) { 1558 // HD, Section 2-7 1559 return (int) ((i >> 63) | (-i >>> 63)); 1560 } 1561 1562 /** 1563 * Returns the value obtained by reversing the order of the bytes in the 1564 * two's complement representation of the specified {@code long} value. 1565 * 1566 * @param i the value whose bytes are to be reversed 1567 * @return the value obtained by reversing the bytes in the specified 1568 * {@code long} value. 1569 * @since 1.5 1570 */ 1571 public static long reverseBytes(long i) { 1572 i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; 1573 return (i << 48) | ((i & 0xffff0000L) << 16) | 1574 ((i >>> 16) & 0xffff0000L) | (i >>> 48); 1575 } 1576 1577 /** 1578 * Adds two {@code long} values together as per the + operator. 1579 * 1580 * @param a the first operand 1581 * @param b the second operand 1582 * @return the sum of {@code a} and {@code b} 1583 * @see java.util.function.BinaryOperator 1584 * @since 1.8 1585 */ 1586 public static long sum(long a, long b) { |