Java : Long with Examples

Long (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Long methods.


Summary

The Long class wraps a value of the primitive type long in an object. An object of type Long contains a single field whose type is long.

Class diagram

// Compile error.
final var list = new ArrayList<long>();
// OK.
final var list = new ArrayList<Long>();
list.add(1L);
list.add(2L);
list.add(3L);

System.out.println(list); // [1, 2, 3]

Fields

static final int BYTES

The number of bytes used to represent a long value in two's complement binary form.

System.out.println(Long.BYTES); // 8

static final long MAX_VALUE

A constant holding the maximum value a long can have, 263-1.

System.out.println(Long.MAX_VALUE); // 9223372036854775807
System.out.println("0x" + Long.toHexString(Long.MAX_VALUE)); // "0x7fffffffffffffff"

static final long MIN_VALUE

A constant holding the minimum value a long can have, -263.

System.out.println(Long.MIN_VALUE); // -9223372036854775808
System.out.println("0x" + Long.toHexString(Long.MIN_VALUE)); // "0x8000000000000000"

static final int SIZE

The number of bits used to represent a long value in two's complement binary form.

System.out.println(Long.SIZE); // 64

static final Class<Long> TYPE

The Class instance representing the primitive type long.

System.out.println(Long.TYPE.getSimpleName()); // "long"
System.out.println(Long.TYPE.isPrimitive()); // true

Constructors

Long (long value)

Deprecated, for removal: This API element is subject to removal in a future version. It is rarely appropriate to use this constructor.

Deprecated. Use valueOf(long l) instead.

Long (String s)

Deprecated, for removal: This API element is subject to removal in a future version. It is rarely appropriate to use this constructor.

Deprecated. Use valueOf(String s) instead.

Methods

static int bitCount (long i)

Returns the number of one-bits in the two's complement binary representation of the specified long value.

final long value = 0;
System.out.println(Long.toBinaryString(value)); // "0"
System.out.println(Long.bitCount(value)); // 0
final long value = 1;
System.out.println(Long.toBinaryString(value)); // "1"
System.out.println(Long.bitCount(value)); // 1
final long value = 2;
System.out.println(Long.toBinaryString(value)); // "10"
System.out.println(Long.bitCount(value)); // 1
final long value = 3;
System.out.println(Long.toBinaryString(value)); // "11"
System.out.println(Long.bitCount(value)); // 2
final long value = 4;
System.out.println(Long.toBinaryString(value)); // "100"
System.out.println(Long.bitCount(value)); // 1
final long value = 5;
System.out.println(Long.toBinaryString(value)); // "101"
System.out.println(Long.bitCount(value)); // 2
final long value = 6;
System.out.println(Long.toBinaryString(value)); // "110"
System.out.println(Long.bitCount(value)); // 2
final long value = 7;
System.out.println(Long.toBinaryString(value)); // "111"
System.out.println(Long.bitCount(value)); // 3
final long value = Long.MAX_VALUE;

// "111111111111111111111111111111111111111111111111111111111111111"
System.out.println(Long.toBinaryString(value));
System.out.println(Long.bitCount(value)); // 63
final long value = Long.MIN_VALUE;

// "1000000000000000000000000000000000000000000000000000000000000000"
System.out.println(Long.toBinaryString(value));
System.out.println(Long.bitCount(value)); // 1

byte byteValue ()

Returns the value of this Long as a byte after a narrowing primitive conversion.

final var ret1 = Long.valueOf(0).byteValue();
System.out.println(ret1); // 0

final var ret2 = Long.valueOf(123).byteValue();
System.out.println(ret2); // 123

final var ret3 = Long.valueOf(-45).byteValue();
System.out.println(ret3); // -45
final var ret1 = Long.valueOf(Byte.MAX_VALUE).byteValue();
System.out.println(ret1); // 127

final var ret2 = Long.valueOf(Byte.MIN_VALUE).byteValue();
System.out.println(ret2); // -128
final var ret1 = Long.valueOf(127).byteValue();
System.out.println(ret1); // 127

final var ret3 = Long.valueOf(128).byteValue();
System.out.println(ret3); // -128

final var ret4 = Long.valueOf(129).byteValue();
System.out.println(ret4); // -127
final var ret1 = Long.valueOf(-128).byteValue();
System.out.println(ret1); // -128

final var ret3 = Long.valueOf(-129).byteValue();
System.out.println(ret3); // 127

final var ret4 = Long.valueOf(-130).byteValue();
System.out.println(ret4); // 126

static int compare (long x, long y)

Compares two long values numerically.

System.out.println(Long.compare(0, 0)); // 0
System.out.println(Long.compare(0, 1)); // -1
System.out.println(Long.compare(1, 0)); // 1
System.out.println(Long.compare(1, -2)); // 1
System.out.println(Long.compare(-4, -2)); // -1
System.out.println(Long.compare(10000000000L, -10000000000L)); // 1

int compareTo (Long anotherLong)

Compares two Long objects numerically.

final var value1 = Long.valueOf(0);
final var value2 = Long.valueOf(0);
System.out.println(value1.compareTo(value2)); // 0
final var value1 = Long.valueOf(0);
final var value2 = Long.valueOf(1);
System.out.println(value1.compareTo(value2)); // -1
final var value1 = Long.valueOf(1);
final var value2 = Long.valueOf(0);
System.out.println(value1.compareTo(value2)); // 1
final var value1 = Long.valueOf(1);
final var value2 = Long.valueOf(-2);
System.out.println(value1.compareTo(value2)); // 1
final var value1 = Long.valueOf(-4);
final var value2 = Long.valueOf(-2);
System.out.println(value1.compareTo(value2)); // -1
final var value1 = Long.valueOf(Long.MAX_VALUE);
final var value2 = Long.valueOf(Long.MIN_VALUE);
System.out.println(value1.compareTo(value2)); // 1

static int compareUnsigned (long x, long y)

Compares two long values numerically treating the values as unsigned.

final long value1 = 0;
final long value2 = 0;
System.out.println("0x" + Long.toHexString(value1)); // "0x0"
System.out.println("0x" + Long.toHexString(value2)); // "0x0"

System.out.println(Long.compare(value1, value2)); // 0
System.out.println(Long.compareUnsigned(value1, value2)); // 0
final long value1 = 5;
final long value2 = 10;
System.out.println("0x" + Long.toHexString(value1)); // "0x5"
System.out.println("0x" + Long.toHexString(value2)); // "0xa"

System.out.println(Long.compare(value1, value2)); // -1
System.out.println(Long.compareUnsigned(value1, value2)); // -1
final long value1 = -1;
final long value2 = 1;
System.out.println("0x" + Long.toHexString(value1)); // "0xffffffffffffffff"
System.out.println("0x" + Long.toHexString(value2)); // "0x1"

System.out.println(Long.compare(value1, value2)); // -1
System.out.println(Long.compareUnsigned(value1, value2)); // 1
final long value1 = -1;
final long value2 = -2;
System.out.println("0x" + Long.toHexString(value1)); // "0xffffffffffffffff"
System.out.println("0x" + Long.toHexString(value2)); // "0xfffffffffffffffe"

System.out.println(Long.compare(value1, value2)); // 1
System.out.println(Long.compareUnsigned(value1, value2)); // 1
final long value1 = Long.MAX_VALUE;
final long value2 = Long.MIN_VALUE;
System.out.println("0x" + Long.toHexString(value1)); // "0x7fffffffffffffff"
System.out.println("0x" + Long.toHexString(value2)); // "0x8000000000000000"

System.out.println(Long.compare(value1, value2)); // 1
System.out.println(Long.compareUnsigned(value1, value2)); // -1

static Long decode (String nm)

Decodes a String into a Long.

System.out.println(Long.decode("0")); // 0
System.out.println(Long.decode("123")); // 123
System.out.println(Long.decode("+456")); // 456
System.out.println(Long.decode("-789")); // -789

//Long.decode("xyz"); // NumberFormatException
// Hexadecimal
final var ret1 = Long.decode("0xff");
System.out.println(ret1); // 255
System.out.println(Long.toHexString(ret1)); // "ff"

final var ret2 = Long.decode("-0xff");
System.out.println(ret2); // -255
System.out.println(Long.toHexString(ret2)); // "ffffffffffffff01"

final var ret3 = Long.decode("#abcd");
System.out.println(ret3); // 43981
System.out.println(Long.toHexString(ret3)); // "abcd"

final var ret4 = Long.decode("0X7fffffffffffffff");
System.out.println(ret4); // 9223372036854775807
System.out.println(Long.toHexString(ret4)); // "7fffffffffffffff"
// Octal
final var ret1 = Long.decode("01234");
System.out.println(ret1); // 668
System.out.println(Long.toOctalString(ret1)); // "1234"

final var ret2 = Long.decode("-01234");
System.out.println(ret2); // -668
System.out.println(Long.toOctalString(ret2)); // "1777777777777777776544"

Optional<Long> describeConstable ()

Returns an Optional containing the nominal descriptor for this instance, which is the instance itself.

final var ret = Long.valueOf(123).describeConstable();
System.out.println(ret); // Optional[123]

static long divideUnsigned (long dividend, long divisor)

Returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.

final var value = -8446744073709551616L;
System.out.println(value / 2); // -4223372036854775808

System.out.println(Long.toUnsignedString(value)); // 10000000000000000000
System.out.println(Long.divideUnsigned(value, 2)); // 5000000000000000000

double doubleValue ()

Returns the value of this Long as a double after a widening primitive conversion.

final var value = Long.valueOf(1234).doubleValue();
System.out.println(value); // 1234.0
final var value = Long.valueOf(-4567).doubleValue();
System.out.println(value); // -4567.0
final var value = Long.valueOf(Long.MAX_VALUE).doubleValue();
System.out.println(value); // 9.223372036854776E18
final var value = Long.valueOf(Long.MIN_VALUE).doubleValue();
System.out.println(value); // -9.223372036854776E18

boolean equals (Object obj)

Compares this object to the specified object.

final var value1 = Long.valueOf(100);
final var value2 = Long.valueOf(100);
final var value3 = Long.valueOf(999);

System.out.println(value1.equals(100L)); // true
System.out.println(value1.equals(value2)); // true
System.out.println(value1.equals(value3)); // false
final var value1 = Long.valueOf(100);
final var value2 = Integer.valueOf(100);

System.out.println(value1.equals(100)); // false
System.out.println(value1.equals(value2)); // false

float floatValue ()

Returns the value of this Long as a float after a widening primitive conversion.

final var value = Long.valueOf(1234).floatValue();
System.out.println(value); // 1234.0
final var value = Long.valueOf(-4567).floatValue();
System.out.println(value); // -4567.0
final var value = Long.valueOf(Long.MAX_VALUE).floatValue();
System.out.println(value); // 9.223372E18
final var value = Long.valueOf(Long.MIN_VALUE).floatValue();
System.out.println(value); // -9.223372E18

static Long getLong (String nm)

Determines the long value of the system property with the specified name.

final var key = "java.specification.version";

System.out.println(Long.getLong(key)); // 18
System.out.println(System.getProperty(key)); // "18"
final var key = "os.name";

System.out.println(Long.getLong(key)); // null
System.out.println(System.getProperty(key)); // "Windows 10"

static Long getLong (String nm, long val)

Determines the long value of the system property with the specified name.

final var key = "java.specification.version";

System.out.println(Long.getLong(key, 12345678900L)); // 18
System.out.println(System.getProperty(key)); // "18"
final var key = "os.name";

System.out.println(Long.getLong(key)); // null
System.out.println(Long.getLong(key, 12345678900L)); // 12345678900
System.out.println(System.getProperty(key)); // "Windows 10"

static Long getLong (String nm, Long val)

Returns the long value of the system property with the specified name.

final var key = "java.specification.version";

System.out.println(Long.getLong(key, Long.valueOf(12345678900L))); // 18
System.out.println(System.getProperty(key)); // "18"
final var key = "os.name";

System.out.println(Long.getLong(key)); // null
System.out.println(Long.getLong(key, Long.valueOf(12345678900L))); // 12345678900
System.out.println(System.getProperty(key)); // "Windows 10"

int hashCode ()

Returns a hash code for this Long.

System.out.println(Long.valueOf(0).hashCode()); // 0
System.out.println(Long.valueOf(1).hashCode()); // 1
System.out.println(Long.valueOf(2).hashCode()); // 2
System.out.println(Long.valueOf(3456).hashCode()); // 3456
System.out.println(Long.valueOf(-7890).hashCode()); // 7889

System.out.println(Long.valueOf(Long.MAX_VALUE).hashCode()); // -2147483648
System.out.println(Long.valueOf(Long.MIN_VALUE).hashCode()); // -2147483648

static int hashCode (long value)

Returns a hash code for a long value; compatible with Long.hashCode().

System.out.println(Long.hashCode(0)); // 0
System.out.println(Long.hashCode(1)); // 1
System.out.println(Long.hashCode(2)); // 2
System.out.println(Long.hashCode(3456)); // 3456
System.out.println(Long.hashCode(-7890)); // 7889

System.out.println(Long.hashCode(Long.MAX_VALUE)); // -2147483648
System.out.println(Long.hashCode(Long.MIN_VALUE)); // -2147483648

static long highestOneBit (long i)

Returns a long value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified long value.

final long value = 0;
System.out.println(Long.toBinaryString(value)); // "0"

final var ret = Long.highestOneBit(value);
System.out.println(ret); // 0
System.out.println(Long.toBinaryString(ret)); // "0"
final long value = 1;
System.out.println(Long.toBinaryString(value)); // "1"

final var ret = Long.highestOneBit(value);
System.out.println(ret); // 1
System.out.println(Long.toBinaryString(ret)); // "1"
final long value = 2;
System.out.println(Long.toBinaryString(value)); // "10"

final var ret = Long.highestOneBit(value);
System.out.println(ret); // 2
System.out.println(Long.toBinaryString(ret)); // "10"
final long value = 3;
System.out.println(Long.toBinaryString(value)); // "11"

final var ret = Long.highestOneBit(value);
System.out.println(ret); // 2
System.out.println(Long.toBinaryString(ret)); // "10"
final long value = 4;
System.out.println(Long.toBinaryString(value)); // "100"

final var ret = Long.highestOneBit(value);
System.out.println(ret); // 4
System.out.println(Long.toBinaryString(ret)); // "100"
final long value = 5;
System.out.println(Long.toBinaryString(value)); // "101"

final var ret = Long.highestOneBit(value);
System.out.println(ret); // 4
System.out.println(Long.toBinaryString(ret)); // "100"
final long value = 6;
System.out.println(Long.toBinaryString(value)); // "110"

final var ret = Long.highestOneBit(value);
System.out.println(ret); // 4
System.out.println(Long.toBinaryString(ret)); // "100"
final long value = 7;
System.out.println(Long.toBinaryString(value)); // "111"

final var ret = Long.highestOneBit(value);
System.out.println(ret); // 4
System.out.println(Long.toBinaryString(ret)); // "100"
final long value = 8;
System.out.println(Long.toBinaryString(value)); // "1000"

final var ret = Long.highestOneBit(value);
System.out.println(ret); // 8
System.out.println(Long.toBinaryString(ret)); // "1000"
final long value = Long.MAX_VALUE;

// "111111111111111111111111111111111111111111111111111111111111111"
System.out.println(Long.toBinaryString(value));

final var ret = Long.highestOneBit(value);
System.out.println(ret); // 4611686018427387904

// "100000000000000000000000000000000000000000000000000000000000000"
System.out.println(Long.toBinaryString(ret));
final long value = Long.MIN_VALUE;

// "1000000000000000000000000000000000000000000000000000000000000000"
System.out.println(Long.toBinaryString(value));

final var ret = Long.highestOneBit(value);
System.out.println(ret); // -9223372036854775808

// "1000000000000000000000000000000000000000000000000000000000000000"
System.out.println(Long.toBinaryString(ret));

int intValue ()

Returns the value of this Long as an int after a narrowing primitive conversion.

final var ret1 = Long.valueOf(0).intValue();
System.out.println(ret1); // 0

final var ret2 = Long.valueOf(123).intValue();
System.out.println(ret2); // 123

final var ret3 = Long.valueOf(-45).intValue();
System.out.println(ret3); // -45
final var ret1 = Long.valueOf(Integer.MAX_VALUE).intValue();
System.out.println(ret1); // 2147483647

final var ret2 = Long.valueOf(Integer.MIN_VALUE).intValue();
System.out.println(ret2); // -2147483648
final var ret1 = Long.valueOf(2147483647L).intValue();
System.out.println(ret1); // 2147483647

final var ret3 = Long.valueOf(2147483648L).intValue();
System.out.println(ret3); // -2147483648

final var ret4 = Long.valueOf(2147483649L).intValue();
System.out.println(ret4); // -2147483647
final var ret1 = Long.valueOf(-2147483648L).intValue();
System.out.println(ret1); // -2147483648

final var ret3 = Long.valueOf(-2147483649L).intValue();
System.out.println(ret3); // 2147483647

final var ret4 = Long.valueOf(-2147483650L).intValue();
System.out.println(ret4); // 2147483646

long longValue ()

Returns the value of this Long as a long value.

final var value = Long.valueOf(1234).longValue();
System.out.println(value); // 1234
final var value = Long.valueOf(-4567).longValue();
System.out.println(value); // -4567
final var value = Long.valueOf(Long.MAX_VALUE).longValue();
System.out.println(value); // 9223372036854775807
final var value = Long.valueOf(Long.MIN_VALUE).longValue();
System.out.println(value); // -9223372036854775808

static long lowestOneBit (long i)

Returns a long value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified long value.

final long value = 0;
System.out.println(Long.toBinaryString(value)); // "0"

final var ret = Long.lowestOneBit(value);
System.out.println(ret); // 0
System.out.println(Long.toBinaryString(ret)); // "0"
final long value = 1;
System.out.println(Long.toBinaryString(value)); // "1"

final var ret = Long.lowestOneBit(value);
System.out.println(ret); // 1
System.out.println(Long.toBinaryString(ret)); // "1"
final long value = 2;
System.out.println(Long.toBinaryString(value)); // "10"

final var ret = Long.lowestOneBit(value);
System.out.println(ret); // 2
System.out.println(Long.toBinaryString(ret)); // "10"
final long value = 3;
System.out.println(Long.toBinaryString(value)); // "11"

final var ret = Long.lowestOneBit(value);
System.out.println(ret); // 1
System.out.println(Long.toBinaryString(ret)); // "1"
final long value = 4;
System.out.println(Long.toBinaryString(value)); // "100"

final var ret = Long.lowestOneBit(value);
System.out.println(ret); // 4
System.out.println(Long.toBinaryString(ret)); // "100"
final long value = 5;
System.out.println(Long.toBinaryString(value)); // "101"

final var ret = Long.lowestOneBit(value);
System.out.println(ret); // 1
System.out.println(Long.toBinaryString(ret)); // "1"
final long value = 6;
System.out.println(Long.toBinaryString(value)); // "110"

final var ret = Long.lowestOneBit(value);
System.out.println(ret); // 2
System.out.println(Long.toBinaryString(ret)); // "10"
final long value = 7;
System.out.println(Long.toBinaryString(value)); // "111"

final var ret = Long.lowestOneBit(value);
System.out.println(ret); // 1
System.out.println(Long.toBinaryString(ret)); // "1"
final long value = Long.MAX_VALUE;

// "111111111111111111111111111111111111111111111111111111111111111"
System.out.println(Long.toBinaryString(value));

final var ret = Long.lowestOneBit(value);
System.out.println(ret); // 1
System.out.println(Long.toBinaryString(ret)); // "1"
final long value = Long.MIN_VALUE;

// "1000000000000000000000000000000000000000000000000000000000000000"
System.out.println(Long.toBinaryString(value));

final var ret = Long.lowestOneBit(value);
System.out.println(ret); // -9223372036854775808

// "1000000000000000000000000000000000000000000000000000000000000000"
System.out.println(Long.toBinaryString(ret));

static long max (long a, long b)

Returns the greater of two long values as if by calling Math.max.

System.out.println(Long.max(1, 1)); // 1
System.out.println(Long.max(1, 9999999999L)); // 9999999999
System.out.println(Long.max(888, 2)); // 888

System.out.println(Long.max(1, -9999999999L)); // 1
System.out.println(Long.max(-888, 2)); // 2
System.out.println(Long.max(-1, -2)); // -1

static long min (long a, long b)

Returns the smaller of two long values as if by calling Math.min.

System.out.println(Long.min(1, 1)); // 1
System.out.println(Long.min(1, 9999999999L)); // 1
System.out.println(Long.min(888, 2)); // 2

System.out.println(Long.min(1, -9999999999L)); // -9999999999
System.out.println(Long.min(-888, 2)); // -888
System.out.println(Long.min(-1, -2)); // -2

static int numberOfLeadingZeros (long i)

Returns the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary representation of the specified long value.

final long value = 0;
System.out.println(Long.toBinaryString(value)); // "0"
System.out.println(Long.numberOfLeadingZeros(value)); // 64
final long value = 1;
System.out.println(Long.toBinaryString(value)); // "1"
System.out.println(Long.numberOfLeadingZeros(value)); // 63
final long value = 2;
System.out.println(Long.toBinaryString(value)); // "10"
System.out.println(Long.numberOfLeadingZeros(value)); // 62
final long value = 3;
System.out.println(Long.toBinaryString(value)); // "11"
System.out.println(Long.numberOfLeadingZeros(value)); // 62
final long value = 4;
System.out.println(Long.toBinaryString(value)); // "100"
System.out.println(Long.numberOfLeadingZeros(value)); // 61
final long value = 5;
System.out.println(Long.toBinaryString(value)); // "101"
System.out.println(Long.numberOfLeadingZeros(value)); // 61
final long value = 6;
System.out.println(Long.toBinaryString(value)); // "110"
System.out.println(Long.numberOfLeadingZeros(value)); // 61
final long value = 7;
System.out.println(Long.toBinaryString(value)); // "111"
System.out.println(Long.numberOfLeadingZeros(value)); // 61
final long value = 8;
System.out.println(Long.toBinaryString(value)); // "1000"
System.out.println(Long.numberOfLeadingZeros(value)); // 60
final long value = 16;
System.out.println(Long.toBinaryString(value)); // "10000"
System.out.println(Long.numberOfLeadingZeros(value)); // 59
final long value = 32;
System.out.println(Long.toBinaryString(value)); // "100000"
System.out.println(Long.numberOfLeadingZeros(value)); // 58
final long value = Long.MAX_VALUE;

// "111111111111111111111111111111111111111111111111111111111111111"
System.out.println(Long.toBinaryString(value));
System.out.println(Long.numberOfLeadingZeros(value)); // 1
final long value = Long.MIN_VALUE;

// "1000000000000000000000000000000000000000000000000000000000000000"
System.out.println(Long.toBinaryString(value));
System.out.println(Long.numberOfLeadingZeros(value)); // 0

static int numberOfTrailingZeros (long i)

Returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified long value.

final long value = 0;
System.out.println(Long.toBinaryString(value)); // "0"
System.out.println(Long.numberOfTrailingZeros(value)); // 64
final long value = 1;
System.out.println(Long.toBinaryString(value)); // "1"
System.out.println(Long.numberOfTrailingZeros(value)); // 0
final long value = 2;
System.out.println(Long.toBinaryString(value)); // "10"
System.out.println(Long.numberOfTrailingZeros(value)); // 1
final long value = 3;
System.out.println(Long.toBinaryString(value)); // "11"
System.out.println(Long.numberOfTrailingZeros(value)); // 0
final long value = 4;
System.out.println(Long.toBinaryString(value)); // "100"
System.out.println(Long.numberOfTrailingZeros(value)); // 2
final long value = 5;
System.out.println(Long.toBinaryString(value)); // "101"
System.out.println(Long.numberOfTrailingZeros(value)); // 0
final long value = 6;
System.out.println(Long.toBinaryString(value)); // "110"
System.out.println(Long.numberOfTrailingZeros(value)); // 1
final long value = 7;
System.out.println(Long.toBinaryString(value)); // "111"
System.out.println(Long.numberOfTrailingZeros(value)); // 0
final long value = 8;
System.out.println(Long.toBinaryString(value)); // "1000"
System.out.println(Long.numberOfTrailingZeros(value)); // 3
final long value = 16;
System.out.println(Long.toBinaryString(value)); // "10000"
System.out.println(Long.numberOfTrailingZeros(value)); // 4
final long value = 32;
System.out.println(Long.toBinaryString(value)); // "100000"
System.out.println(Long.numberOfTrailingZeros(value)); // 5
final long value = Long.MAX_VALUE;

// "111111111111111111111111111111111111111111111111111111111111111"
System.out.println(Long.toBinaryString(value));
System.out.println(Long.numberOfTrailingZeros(value)); // 0
final long value = Long.MIN_VALUE;

// "1000000000000000000000000000000000000000000000000000000000000000"
System.out.println(Long.toBinaryString(value));
System.out.println(Long.numberOfTrailingZeros(value)); // 63

static long parseLong (CharSequence s, int beginIndex, int endIndex, int radix)

Parses the CharSequence argument as a signed long in the specified radix, beginning at the specified beginIndex and extending to endIndex - 1.

Please see also : parseLong(String s, int radix)

final var s = "12345";

//Long.parseLong(s, 0, 0, 10); // NumberFormatException
System.out.println(Long.parseLong(s, 0, 1, 10)); // 1
System.out.println(Long.parseLong(s, 0, 2, 10)); // 12
System.out.println(Long.parseLong(s, 0, 3, 10)); // 123
System.out.println(Long.parseLong(s, 0, 4, 10)); // 1234
System.out.println(Long.parseLong(s, 0, 5, 10)); // 12345
//Long.parseLong(s, 0, 6, 10); // IndexOutOfBoundsException

System.out.println(Long.parseLong(s, 1, 5, 10)); // 2345
System.out.println(Long.parseLong(s, 2, 5, 10)); // 345
System.out.println(Long.parseLong(s, 3, 5, 10)); // 45
System.out.println(Long.parseLong(s, 4, 5, 10)); // 5

static long parseLong (String s)

Parses the string argument as a signed decimal long.

System.out.println(Long.parseLong("0")); // 0
System.out.println(Long.parseLong("123")); // 123
System.out.println(Long.parseLong("+456")); // 456
System.out.println(Long.parseLong("-789")); // -789

System.out.println(Long.parseLong("9223372036854775807")); // 9223372036854775807
//Long.parseLong("9223372036854775808"); // NumberFormatException

System.out.println(Long.parseLong("-9223372036854775808")); // -9223372036854775808
//Long.parseLong("-9223372036854775809"); // NumberFormatException

static long parseLong (String s, int radix)

Parses the string argument as a signed long in the radix specified by the second argument.

System.out.println(Long.parseLong("0", 10)); // 0
System.out.println(Long.parseLong("473", 10)); // 473
System.out.println(Long.parseLong("+42", 10)); // 42
System.out.println(Long.parseLong("-0", 10)); // 0
System.out.println(Long.parseLong("-FF", 16)); // -255
System.out.println(Long.parseLong("1100110", 2)); // 102
System.out.println(Long.parseLong("9223372036854775807", 10)); // 9223372036854775807
System.out.println(Long.parseLong("-9223372036854775808", 10)); // -9223372036854775808

//Long.parseLong("9223372036854775808", 10); // NumberFormatException
//Long.parseLong("99", 8); // NumberFormatException
//Long.parseLong("Hazelnut", 10); // NumberFormatException

System.out.println(Long.parseLong("Hazelnut", 36)); // 1356099454469

static long parseUnsignedLong (CharSequence s, int beginIndex, int endIndex, int radix)

Parses the CharSequence argument as an unsigned long in the specified radix, beginning at the specified beginIndex and extending to endIndex - 1.

Please see also : parseUnsignedLong(String s, int radix)

final var s = "12345";

//Long.parseUnsignedLong(s, 0, 0, 10); // NumberFormatException
System.out.println(Long.parseUnsignedLong(s, 0, 1, 10)); // 1
System.out.println(Long.parseUnsignedLong(s, 0, 2, 10)); // 12
System.out.println(Long.parseUnsignedLong(s, 0, 3, 10)); // 123
System.out.println(Long.parseUnsignedLong(s, 0, 4, 10)); // 1234
System.out.println(Long.parseUnsignedLong(s, 0, 5, 10)); // 12345
//Long.parseUnsignedLong(s, 0, 6, 10); // IndexOutOfBoundsException

System.out.println(Long.parseUnsignedLong(s, 1, 5, 10)); // 2345
System.out.println(Long.parseUnsignedLong(s, 2, 5, 10)); // 345
System.out.println(Long.parseUnsignedLong(s, 3, 5, 10)); // 45
System.out.println(Long.parseUnsignedLong(s, 4, 5, 10)); // 5

static long parseUnsignedLong (String s)

Parses the string argument as an unsigned decimal long.

System.out.println(Long.parseUnsignedLong("0")); //  0
System.out.println(Long.parseUnsignedLong("123")); // 123
//Long.parseUnsignedLong("-456"); // NumberFormatException
System.out.println(Long.MAX_VALUE); // 9223372036854775807

System.out.println(Long.parseUnsignedLong("9223372036854775807")); // 9223372036854775807
System.out.println(Long.parseUnsignedLong("9223372036854775808")); // -9223372036854775808
System.out.println(Long.parseUnsignedLong("9223372036854775809")); // -9223372036854775807

static long parseUnsignedLong (String s, int radix)

Parses the string argument as an unsigned long in the radix specified by the second argument.

System.out.println(Long.parseUnsignedLong("0", 10)); //  0
System.out.println(Long.parseUnsignedLong("123", 10)); // 123
//Long.parseUnsignedLong("-456", 10); // NumberFormatException
System.out.println(Long.MAX_VALUE); // 9223372036854775807

System.out.println(Long.parseUnsignedLong("9223372036854775807", 10)); // 9223372036854775807
System.out.println(Long.parseUnsignedLong("9223372036854775808", 10)); // -9223372036854775808
System.out.println(Long.parseUnsignedLong("9223372036854775809", 10)); // -9223372036854775807
System.out.println(Long.parseUnsignedLong("FF", 16)); // 255
//Long.parseUnsignedLong("-AA", 16); // NumberFormatException

System.out.println(Long.parseUnsignedLong("1100110", 2)); // 102
//Long.parseUnsignedLong("-1010"); // NumberFormatException

static long remainderUnsigned (long dividend, long divisor)

Returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.

final var value = -8446744073709551612L;
System.out.println(value % 10); // -2

System.out.println(Long.toUnsignedString(value)); // 10000000000000000004
System.out.println(Long.remainderUnsigned(value, 10)); // 4

Long resolveConstantDesc (MethodHandles.Lookup lookup)

Resolves this instance as a ConstantDesc, the result of which is the instance itself.

final var value = Long.valueOf(123);

final var ret = value.resolveConstantDesc(MethodHandles.lookup());
System.out.println(ret); // 123

static long reverse (long i)

Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified long value.

final long value = 0;
System.out.println(value); // 0

final var ret = Long.reverse(value);
System.out.println(ret); // 0

System.out.println("-- print --");
System.out.println("%64s".formatted(Long.toBinaryString(value)).replace(" ", "0"));
System.out.println("%64s".formatted(Long.toBinaryString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//0000000000000000000000000000000000000000000000000000000000000000
//0000000000000000000000000000000000000000000000000000000000000000
final long value = 1;
System.out.println(value); // 1

final var ret = Long.reverse(value);
System.out.println(ret); // -9223372036854775808

System.out.println("-- print --");
System.out.println("%64s".formatted(Long.toBinaryString(value)).replace(" ", "0"));
System.out.println("%64s".formatted(Long.toBinaryString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//0000000000000000000000000000000000000000000000000000000000000001
//1000000000000000000000000000000000000000000000000000000000000000
final long value = 2;
System.out.println(value); // 2

final var ret = Long.reverse(value);
System.out.println(ret); // 4611686018427387904

System.out.println("-- print --");
System.out.println("%64s".formatted(Long.toBinaryString(value)).replace(" ", "0"));
System.out.println("%64s".formatted(Long.toBinaryString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//0000000000000000000000000000000000000000000000000000000000000010
//0100000000000000000000000000000000000000000000000000000000000000
final long value = 0x0f0f0f0f;
System.out.println(value); // 252645135

final var ret = Long.reverse(value);
System.out.println(ret); // -1085102596613472256

System.out.println("-- print --");
System.out.println("%64s".formatted(Long.toBinaryString(value)).replace(" ", "0"));
System.out.println("%64s".formatted(Long.toBinaryString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//0000000000000000000000000000000000001111000011110000111100001111
//1111000011110000111100001111000000000000000000000000000000000000
final long value = Long.MAX_VALUE;
System.out.println(value); // 9223372036854775807

final var ret = Long.reverse(value);
System.out.println(ret); // -2

System.out.println("-- print --");
System.out.println("%64s".formatted(Long.toBinaryString(value)).replace(" ", "0"));
System.out.println("%64s".formatted(Long.toBinaryString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//0111111111111111111111111111111111111111111111111111111111111111
//1111111111111111111111111111111111111111111111111111111111111110
final long value = Long.MIN_VALUE;
System.out.println(value); // -9223372036854775808

final var ret = Long.reverse(value);
System.out.println(ret); // 1

System.out.println("-- print --");
System.out.println("%64s".formatted(Long.toBinaryString(value)).replace(" ", "0"));
System.out.println("%64s".formatted(Long.toBinaryString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//1000000000000000000000000000000000000000000000000000000000000000
//0000000000000000000000000000000000000000000000000000000000000001

static long reverseBytes (long i)

Returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified long value.

final long value = 0x0;
final var ret = Long.reverseBytes(value);

System.out.println("-- print --");
System.out.println("%16s".formatted(Long.toHexString(value)).replace(" ", "0"));
System.out.println("%16s".formatted(Long.toHexString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//0000000000000000
//0000000000000000
final long value = 0x1;
final var ret = Long.reverseBytes(value);

System.out.println("-- print --");
System.out.println("%16s".formatted(Long.toHexString(value)).replace(" ", "0"));
System.out.println("%16s".formatted(Long.toHexString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//0000000000000001
//0100000000000000
final long value = 0xf;
final var ret = Long.reverseBytes(value);

System.out.println("-- print --");
System.out.println("%16s".formatted(Long.toHexString(value)).replace(" ", "0"));
System.out.println("%16s".formatted(Long.toHexString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//000000000000000f
//0f00000000000000
final long value = 0x10;
final var ret = Long.reverseBytes(value);

System.out.println("-- print --");
System.out.println("%16s".formatted(Long.toHexString(value)).replace(" ", "0"));
System.out.println("%16s".formatted(Long.toHexString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//0000000000000010
//1000000000000000
final long value = 0x1f;
final var ret = Long.reverseBytes(value);

System.out.println("-- print --");
System.out.println("%16s".formatted(Long.toHexString(value)).replace(" ", "0"));
System.out.println("%16s".formatted(Long.toHexString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//000000000000001f
//1f00000000000000
final long value = 0x123456789abcdefL;
final var ret = Long.reverseBytes(value);

System.out.println("-- print --");
System.out.println("%16s".formatted(Long.toHexString(value)).replace(" ", "0"));
System.out.println("%16s".formatted(Long.toHexString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//0123456789abcdef
//efcdab8967452301
final long value = Long.MAX_VALUE;
final var ret = Long.reverseBytes(value);

System.out.println("-- print --");
System.out.println("%16s".formatted(Long.toHexString(value)).replace(" ", "0"));
System.out.println("%16s".formatted(Long.toHexString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//7fffffffffffffff
//ffffffffffffff7f
final long value = Long.MIN_VALUE;
final var ret = Long.reverseBytes(value);

System.out.println("-- print --");
System.out.println("%16s".formatted(Long.toHexString(value)).replace(" ", "0"));
System.out.println("%16s".formatted(Long.toHexString(ret)).replace(" ", "0"));

// Result
// ↓
//-- print --
//8000000000000000
//0000000000000080

static long rotateLeft (long i, int distance)

Returns the value obtained by rotating the two's complement binary representation of the specified long value left by the specified number of bits.

final long value = 1;
for (int i = 0; i < 65; i++) {
    final var ret = Long.rotateLeft(value, i);
    System.out.printf("i = %2d : %s%n", i, Long.toBinaryString(ret));
}

// Result
// ↓
//i =  0 : 1
//i =  1 : 10
//i =  2 : 100
//i =  3 : 1000
//i =  4 : 10000
//i =  5 : 100000
//i =  6 : 1000000
//i =  7 : 10000000
//i =  8 : 100000000
//i =  9 : 1000000000
//i = 10 : 10000000000
//i = 11 : 100000000000
//i = 12 : 1000000000000
//i = 13 : 10000000000000
//i = 14 : 100000000000000
//i = 15 : 1000000000000000
//i = 16 : 10000000000000000
//i = 17 : 100000000000000000
//i = 18 : 1000000000000000000
//i = 19 : 10000000000000000000
//i = 20 : 100000000000000000000
//i = 21 : 1000000000000000000000
//i = 22 : 10000000000000000000000
//i = 23 : 100000000000000000000000
//i = 24 : 1000000000000000000000000
//i = 25 : 10000000000000000000000000
//i = 26 : 100000000000000000000000000
//i = 27 : 1000000000000000000000000000
//i = 28 : 10000000000000000000000000000
//i = 29 : 100000000000000000000000000000
//i = 30 : 1000000000000000000000000000000
//i = 31 : 10000000000000000000000000000000
//i = 32 : 100000000000000000000000000000000
//i = 33 : 1000000000000000000000000000000000
//i = 34 : 10000000000000000000000000000000000
//i = 35 : 100000000000000000000000000000000000
//i = 36 : 1000000000000000000000000000000000000
//i = 37 : 10000000000000000000000000000000000000
//i = 38 : 100000000000000000000000000000000000000
//i = 39 : 1000000000000000000000000000000000000000
//i = 40 : 10000000000000000000000000000000000000000
//i = 41 : 100000000000000000000000000000000000000000
//i = 42 : 1000000000000000000000000000000000000000000
//i = 43 : 10000000000000000000000000000000000000000000
//i = 44 : 100000000000000000000000000000000000000000000
//i = 45 : 1000000000000000000000000000000000000000000000
//i = 46 : 10000000000000000000000000000000000000000000000
//i = 47 : 100000000000000000000000000000000000000000000000
//i = 48 : 1000000000000000000000000000000000000000000000000
//i = 49 : 10000000000000000000000000000000000000000000000000
//i = 50 : 100000000000000000000000000000000000000000000000000
//i = 51 : 1000000000000000000000000000000000000000000000000000
//i = 52 : 10000000000000000000000000000000000000000000000000000
//i = 53 : 100000000000000000000000000000000000000000000000000000
//i = 54 : 1000000000000000000000000000000000000000000000000000000
//i = 55 : 10000000000000000000000000000000000000000000000000000000
//i = 56 : 100000000000000000000000000000000000000000000000000000000
//i = 57 : 1000000000000000000000000000000000000000000000000000000000
//i = 58 : 10000000000000000000000000000000000000000000000000000000000
//i = 59 : 100000000000000000000000000000000000000000000000000000000000
//i = 60 : 1000000000000000000000000000000000000000000000000000000000000
//i = 61 : 10000000000000000000000000000000000000000000000000000000000000
//i = 62 : 100000000000000000000000000000000000000000000000000000000000000
//i = 63 : 1000000000000000000000000000000000000000000000000000000000000000
//i = 64 : 1
final long value = Long.MAX_VALUE;
for (int i = 0; i < 65; i++) {
    final var ret = Long.rotateLeft(value, i);
    System.out.printf("i = %2d : %64s%n", i, Long.toBinaryString(ret));
}

// Result
// ↓
//i =  0 :  111111111111111111111111111111111111111111111111111111111111111
//i =  1 : 1111111111111111111111111111111111111111111111111111111111111110
//i =  2 : 1111111111111111111111111111111111111111111111111111111111111101
//i =  3 : 1111111111111111111111111111111111111111111111111111111111111011
//i =  4 : 1111111111111111111111111111111111111111111111111111111111110111
//i =  5 : 1111111111111111111111111111111111111111111111111111111111101111
//i =  6 : 1111111111111111111111111111111111111111111111111111111111011111
//i =  7 : 1111111111111111111111111111111111111111111111111111111110111111
//i =  8 : 1111111111111111111111111111111111111111111111111111111101111111
//i =  9 : 1111111111111111111111111111111111111111111111111111111011111111
//i = 10 : 1111111111111111111111111111111111111111111111111111110111111111
//i = 11 : 1111111111111111111111111111111111111111111111111111101111111111
//i = 12 : 1111111111111111111111111111111111111111111111111111011111111111
//i = 13 : 1111111111111111111111111111111111111111111111111110111111111111
//i = 14 : 1111111111111111111111111111111111111111111111111101111111111111
//i = 15 : 1111111111111111111111111111111111111111111111111011111111111111
//i = 16 : 1111111111111111111111111111111111111111111111110111111111111111
//i = 17 : 1111111111111111111111111111111111111111111111101111111111111111
//i = 18 : 1111111111111111111111111111111111111111111111011111111111111111
//i = 19 : 1111111111111111111111111111111111111111111110111111111111111111
//i = 20 : 1111111111111111111111111111111111111111111101111111111111111111
//i = 21 : 1111111111111111111111111111111111111111111011111111111111111111
//i = 22 : 1111111111111111111111111111111111111111110111111111111111111111
//i = 23 : 1111111111111111111111111111111111111111101111111111111111111111
//i = 24 : 1111111111111111111111111111111111111111011111111111111111111111
//i = 25 : 1111111111111111111111111111111111111110111111111111111111111111
//i = 26 : 1111111111111111111111111111111111111101111111111111111111111111
//i = 27 : 1111111111111111111111111111111111111011111111111111111111111111
//i = 28 : 1111111111111111111111111111111111110111111111111111111111111111
//i = 29 : 1111111111111111111111111111111111101111111111111111111111111111
//i = 30 : 1111111111111111111111111111111111011111111111111111111111111111
//i = 31 : 1111111111111111111111111111111110111111111111111111111111111111
//i = 32 : 1111111111111111111111111111111101111111111111111111111111111111
//i = 33 : 1111111111111111111111111111111011111111111111111111111111111111
//i = 34 : 1111111111111111111111111111110111111111111111111111111111111111
//i = 35 : 1111111111111111111111111111101111111111111111111111111111111111
//i = 36 : 1111111111111111111111111111011111111111111111111111111111111111
//i = 37 : 1111111111111111111111111110111111111111111111111111111111111111
//i = 38 : 1111111111111111111111111101111111111111111111111111111111111111
//i = 39 : 1111111111111111111111111011111111111111111111111111111111111111
//i = 40 : 1111111111111111111111110111111111111111111111111111111111111111
//i = 41 : 1111111111111111111111101111111111111111111111111111111111111111
//i = 42 : 1111111111111111111111011111111111111111111111111111111111111111
//i = 43 : 1111111111111111111110111111111111111111111111111111111111111111
//i = 44 : 1111111111111111111101111111111111111111111111111111111111111111
//i = 45 : 1111111111111111111011111111111111111111111111111111111111111111
//i = 46 : 1111111111111111110111111111111111111111111111111111111111111111
//i = 47 : 1111111111111111101111111111111111111111111111111111111111111111
//i = 48 : 1111111111111111011111111111111111111111111111111111111111111111
//i = 49 : 1111111111111110111111111111111111111111111111111111111111111111
//i = 50 : 1111111111111101111111111111111111111111111111111111111111111111
//i = 51 : 1111111111111011111111111111111111111111111111111111111111111111
//i = 52 : 1111111111110111111111111111111111111111111111111111111111111111
//i = 53 : 1111111111101111111111111111111111111111111111111111111111111111
//i = 54 : 1111111111011111111111111111111111111111111111111111111111111111
//i = 55 : 1111111110111111111111111111111111111111111111111111111111111111
//i = 56 : 1111111101111111111111111111111111111111111111111111111111111111
//i = 57 : 1111111011111111111111111111111111111111111111111111111111111111
//i = 58 : 1111110111111111111111111111111111111111111111111111111111111111
//i = 59 : 1111101111111111111111111111111111111111111111111111111111111111
//i = 60 : 1111011111111111111111111111111111111111111111111111111111111111
//i = 61 : 1110111111111111111111111111111111111111111111111111111111111111
//i = 62 : 1101111111111111111111111111111111111111111111111111111111111111
//i = 63 : 1011111111111111111111111111111111111111111111111111111111111111
//i = 64 :  111111111111111111111111111111111111111111111111111111111111111

static long rotateRight (long i, int distance)

Returns the value obtained by rotating the two's complement binary representation of the specified long value right by the specified number of bits.

final long value = 1;
for (int i = 0; i < 65; i++) {
    final var ret = Long.rotateRight(value, i);
    System.out.printf("i = %2d : %s%n", i, Long.toBinaryString(ret));
}

// Result
// ↓
//i =  0 : 1
//i =  1 : 1000000000000000000000000000000000000000000000000000000000000000
//i =  2 : 100000000000000000000000000000000000000000000000000000000000000
//i =  3 : 10000000000000000000000000000000000000000000000000000000000000
//i =  4 : 1000000000000000000000000000000000000000000000000000000000000
//i =  5 : 100000000000000000000000000000000000000000000000000000000000
//i =  6 : 10000000000000000000000000000000000000000000000000000000000
//i =  7 : 1000000000000000000000000000000000000000000000000000000000
//i =  8 : 100000000000000000000000000000000000000000000000000000000
//i =  9 : 10000000000000000000000000000000000000000000000000000000
//i = 10 : 1000000000000000000000000000000000000000000000000000000
//i = 11 : 100000000000000000000000000000000000000000000000000000
//i = 12 : 10000000000000000000000000000000000000000000000000000
//i = 13 : 1000000000000000000000000000000000000000000000000000
//i = 14 : 100000000000000000000000000000000000000000000000000
//i = 15 : 10000000000000000000000000000000000000000000000000
//i = 16 : 1000000000000000000000000000000000000000000000000
//i = 17 : 100000000000000000000000000000000000000000000000
//i = 18 : 10000000000000000000000000000000000000000000000
//i = 19 : 1000000000000000000000000000000000000000000000
//i = 20 : 100000000000000000000000000000000000000000000
//i = 21 : 10000000000000000000000000000000000000000000
//i = 22 : 1000000000000000000000000000000000000000000
//i = 23 : 100000000000000000000000000000000000000000
//i = 24 : 10000000000000000000000000000000000000000
//i = 25 : 1000000000000000000000000000000000000000
//i = 26 : 100000000000000000000000000000000000000
//i = 27 : 10000000000000000000000000000000000000
//i = 28 : 1000000000000000000000000000000000000
//i = 29 : 100000000000000000000000000000000000
//i = 30 : 10000000000000000000000000000000000
//i = 31 : 1000000000000000000000000000000000
//i = 32 : 100000000000000000000000000000000
//i = 33 : 10000000000000000000000000000000
//i = 34 : 1000000000000000000000000000000
//i = 35 : 100000000000000000000000000000
//i = 36 : 10000000000000000000000000000
//i = 37 : 1000000000000000000000000000
//i = 38 : 100000000000000000000000000
//i = 39 : 10000000000000000000000000
//i = 40 : 1000000000000000000000000
//i = 41 : 100000000000000000000000
//i = 42 : 10000000000000000000000
//i = 43 : 1000000000000000000000
//i = 44 : 100000000000000000000
//i = 45 : 10000000000000000000
//i = 46 : 1000000000000000000
//i = 47 : 100000000000000000
//i = 48 : 10000000000000000
//i = 49 : 1000000000000000
//i = 50 : 100000000000000
//i = 51 : 10000000000000
//i = 52 : 1000000000000
//i = 53 : 100000000000
//i = 54 : 10000000000
//i = 55 : 1000000000
//i = 56 : 100000000
//i = 57 : 10000000
//i = 58 : 1000000
//i = 59 : 100000
//i = 60 : 10000
//i = 61 : 1000
//i = 62 : 100
//i = 63 : 10
//i = 64 : 1
final long value = Long.MAX_VALUE;
for (int i = 0; i < 65; i++) {
    final var ret = Long.rotateRight(value, i);
    System.out.printf("i = %2d : %64s%n", i, Long.toBinaryString(ret));
}

// Result
// ↓
//i =  0 :  111111111111111111111111111111111111111111111111111111111111111
//i =  1 : 1011111111111111111111111111111111111111111111111111111111111111
//i =  2 : 1101111111111111111111111111111111111111111111111111111111111111
//i =  3 : 1110111111111111111111111111111111111111111111111111111111111111
//i =  4 : 1111011111111111111111111111111111111111111111111111111111111111
//i =  5 : 1111101111111111111111111111111111111111111111111111111111111111
//i =  6 : 1111110111111111111111111111111111111111111111111111111111111111
//i =  7 : 1111111011111111111111111111111111111111111111111111111111111111
//i =  8 : 1111111101111111111111111111111111111111111111111111111111111111
//i =  9 : 1111111110111111111111111111111111111111111111111111111111111111
//i = 10 : 1111111111011111111111111111111111111111111111111111111111111111
//i = 11 : 1111111111101111111111111111111111111111111111111111111111111111
//i = 12 : 1111111111110111111111111111111111111111111111111111111111111111
//i = 13 : 1111111111111011111111111111111111111111111111111111111111111111
//i = 14 : 1111111111111101111111111111111111111111111111111111111111111111
//i = 15 : 1111111111111110111111111111111111111111111111111111111111111111
//i = 16 : 1111111111111111011111111111111111111111111111111111111111111111
//i = 17 : 1111111111111111101111111111111111111111111111111111111111111111
//i = 18 : 1111111111111111110111111111111111111111111111111111111111111111
//i = 19 : 1111111111111111111011111111111111111111111111111111111111111111
//i = 20 : 1111111111111111111101111111111111111111111111111111111111111111
//i = 21 : 1111111111111111111110111111111111111111111111111111111111111111
//i = 22 : 1111111111111111111111011111111111111111111111111111111111111111
//i = 23 : 1111111111111111111111101111111111111111111111111111111111111111
//i = 24 : 1111111111111111111111110111111111111111111111111111111111111111
//i = 25 : 1111111111111111111111111011111111111111111111111111111111111111
//i = 26 : 1111111111111111111111111101111111111111111111111111111111111111
//i = 27 : 1111111111111111111111111110111111111111111111111111111111111111
//i = 28 : 1111111111111111111111111111011111111111111111111111111111111111
//i = 29 : 1111111111111111111111111111101111111111111111111111111111111111
//i = 30 : 1111111111111111111111111111110111111111111111111111111111111111
//i = 31 : 1111111111111111111111111111111011111111111111111111111111111111
//i = 32 : 1111111111111111111111111111111101111111111111111111111111111111
//i = 33 : 1111111111111111111111111111111110111111111111111111111111111111
//i = 34 : 1111111111111111111111111111111111011111111111111111111111111111
//i = 35 : 1111111111111111111111111111111111101111111111111111111111111111
//i = 36 : 1111111111111111111111111111111111110111111111111111111111111111
//i = 37 : 1111111111111111111111111111111111111011111111111111111111111111
//i = 38 : 1111111111111111111111111111111111111101111111111111111111111111
//i = 39 : 1111111111111111111111111111111111111110111111111111111111111111
//i = 40 : 1111111111111111111111111111111111111111011111111111111111111111
//i = 41 : 1111111111111111111111111111111111111111101111111111111111111111
//i = 42 : 1111111111111111111111111111111111111111110111111111111111111111
//i = 43 : 1111111111111111111111111111111111111111111011111111111111111111
//i = 44 : 1111111111111111111111111111111111111111111101111111111111111111
//i = 45 : 1111111111111111111111111111111111111111111110111111111111111111
//i = 46 : 1111111111111111111111111111111111111111111111011111111111111111
//i = 47 : 1111111111111111111111111111111111111111111111101111111111111111
//i = 48 : 1111111111111111111111111111111111111111111111110111111111111111
//i = 49 : 1111111111111111111111111111111111111111111111111011111111111111
//i = 50 : 1111111111111111111111111111111111111111111111111101111111111111
//i = 51 : 1111111111111111111111111111111111111111111111111110111111111111
//i = 52 : 1111111111111111111111111111111111111111111111111111011111111111
//i = 53 : 1111111111111111111111111111111111111111111111111111101111111111
//i = 54 : 1111111111111111111111111111111111111111111111111111110111111111
//i = 55 : 1111111111111111111111111111111111111111111111111111111011111111
//i = 56 : 1111111111111111111111111111111111111111111111111111111101111111
//i = 57 : 1111111111111111111111111111111111111111111111111111111110111111
//i = 58 : 1111111111111111111111111111111111111111111111111111111111011111
//i = 59 : 1111111111111111111111111111111111111111111111111111111111101111
//i = 60 : 1111111111111111111111111111111111111111111111111111111111110111
//i = 61 : 1111111111111111111111111111111111111111111111111111111111111011
//i = 62 : 1111111111111111111111111111111111111111111111111111111111111101
//i = 63 : 1111111111111111111111111111111111111111111111111111111111111110
//i = 64 :  111111111111111111111111111111111111111111111111111111111111111

short shortValue ()

Returns the value of this Long as a short after a narrowing primitive conversion.

final var ret1 = Long.valueOf(0).shortValue();
System.out.println(ret1); // 0

final var ret2 = Long.valueOf(123).shortValue();
System.out.println(ret2); // 123

final var ret3 = Long.valueOf(-456).shortValue();
System.out.println(ret3); // -456
final var ret1 = Long.valueOf(Short.MAX_VALUE).shortValue();
System.out.println(ret1); // 32767

final var ret2 = Long.valueOf(Short.MIN_VALUE).shortValue();
System.out.println(ret2); // -32768
final var ret1 = Long.valueOf(32767).shortValue();
System.out.println(ret1); // 32767

final var ret3 = Long.valueOf(32768).shortValue();
System.out.println(ret3); // -32768

final var ret4 = Long.valueOf(32769).shortValue();
System.out.println(ret4); // -32767
final var ret1 = Long.valueOf(-32768).shortValue();
System.out.println(ret1); // -32768

final var ret3 = Long.valueOf(-32769).shortValue();
System.out.println(ret3); // 32767

final var ret4 = Long.valueOf(-32770).shortValue();
System.out.println(ret4); // 32766

static int signum (long i)

Returns the signum function of the specified long value.

System.out.println(Long.signum(100)); // 1
System.out.println(Long.signum(-100)); // -1

System.out.println(Long.signum(0)); // 0
System.out.println(Long.signum(-0)); // 0

System.out.println(Long.signum(Long.MAX_VALUE)); // 1
System.out.println(Long.signum(Long.MIN_VALUE)); // -1

static long sum (long a, long b)

Adds two long values together as per the + operator.

System.out.println(Long.sum(100, 200)); // 300
System.out.println(Long.sum(-300, 200)); // -100

System.out.println(Long.sum(Long.MAX_VALUE, Long.MIN_VALUE)); // -1
System.out.println(Long.sum(Long.MAX_VALUE, 1)); // -9223372036854775808
System.out.println(Long.sum(Long.MIN_VALUE, -1)); // 9223372036854775807

static String toBinaryString (long i)

Returns a string representation of the long argument as an unsigned integer in base 2.

System.out.println(Long.toBinaryString(0)); // "0"
System.out.println(Long.toBinaryString(1)); // "1"
System.out.println(Long.toBinaryString(2)); // "10"
System.out.println(Long.toBinaryString(3)); // "11"
System.out.println(Long.toBinaryString(4)); // "100"
System.out.println(Long.toBinaryString(5)); // "101"
System.out.println(Long.toBinaryString(6)); // "110"
System.out.println(Long.toBinaryString(7)); // "111"
System.out.println(Long.toBinaryString(8)); // "1000"

// "1111111111111111111111111111111111111111111111111111111111111111"
System.out.println(Long.toBinaryString(-1));

// "1111111111111111111111111111111111111111111111111111111111111110"
System.out.println(Long.toBinaryString(-2));

// "1111111111111111111111111111111111111111111111111111111111111101"
System.out.println(Long.toBinaryString(-3));

// "111111111111111111111111111111111111111111111111111111111111111"
System.out.println(Long.toBinaryString(Long.MAX_VALUE));

// "1000000000000000000000000000000000000000000000000000000000000000"
System.out.println(Long.toBinaryString(Long.MIN_VALUE));

static String toHexString (long i)

Returns a string representation of the long argument as an unsigned integer in base 16.

System.out.println(Long.toHexString(0)); // "0"
System.out.println(Long.toHexString(1)); // "1"
System.out.println(Long.toHexString(2)); // "2"
System.out.println(Long.toHexString(3)); // "3"
System.out.println(Long.toHexString(4)); // "4"
System.out.println(Long.toHexString(5)); // "5"
System.out.println(Long.toHexString(6)); // "6"
System.out.println(Long.toHexString(7)); // "7"
System.out.println(Long.toHexString(8)); // "8"
System.out.println(Long.toHexString(9)); // "9"
System.out.println(Long.toHexString(10)); // "a"
System.out.println(Long.toHexString(11)); // "b"
System.out.println(Long.toHexString(12)); // "c"
System.out.println(Long.toHexString(13)); // "d"
System.out.println(Long.toHexString(14)); // "e"
System.out.println(Long.toHexString(15)); // "f"
System.out.println(Long.toHexString(16)); // "10"
System.out.println(Long.toHexString(17)); // "11"

System.out.println(Long.toHexString(-1)); // "ffffffffffffffff"
System.out.println(Long.toHexString(-2)); // "fffffffffffffffe"
System.out.println(Long.toHexString(-3)); // "fffffffffffffffd"

System.out.println(Long.toHexString(Long.MAX_VALUE)); // "7fffffffffffffff"
System.out.println(Long.toHexString(Long.MIN_VALUE)); // "8000000000000000"

static String toOctalString (long i)

Returns a string representation of the long argument as an unsigned integer in base 8.

System.out.println(Long.toOctalString(0)); // "0"
System.out.println(Long.toOctalString(1)); // "1"
System.out.println(Long.toOctalString(2)); // "2"
System.out.println(Long.toOctalString(3)); // "3"
System.out.println(Long.toOctalString(4)); // "4"
System.out.println(Long.toOctalString(5)); // "5"
System.out.println(Long.toOctalString(6)); // "6"
System.out.println(Long.toOctalString(7)); // "7"
System.out.println(Long.toOctalString(8)); // "10"
System.out.println(Long.toOctalString(9)); // "11"
System.out.println(Long.toOctalString(10)); // "12"
System.out.println(Long.toOctalString(11)); // "13"

System.out.println(Long.toOctalString(-1)); // "1777777777777777777777"
System.out.println(Long.toOctalString(-2)); // "1777777777777777777776"
System.out.println(Long.toOctalString(-3)); // "1777777777777777777775"

System.out.println(Long.toOctalString(Long.MAX_VALUE)); // "777777777777777777777"
System.out.println(Long.toOctalString(Long.MIN_VALUE)); // "1000000000000000000000"

String toString ()

Returns a String object representing this Long's value.

final var ret1 = Long.valueOf(123).toString();
System.out.println(ret1); // "123"

final var ret2 = Long.valueOf(-456).toString();
System.out.println(ret2); // "-456"

static String toString (long i)

Returns a String object representing the specified long.

final var ret1 = Long.toString(123);
System.out.println(ret1); // "123"

final var ret2 = Long.toString(-456);
System.out.println(ret2); // "-456"

final var ret3 = Long.toString(Long.MAX_VALUE);
System.out.println(ret3); // "9223372036854775807"

final var ret4 = Long.toString(Long.MIN_VALUE);
System.out.println(ret4); // "-9223372036854775808"

static String toString (long i, int radix)

Returns a string representation of the first argument in the radix specified by the second argument.

final var ret1 = Long.toString(123, 10);
System.out.println(ret1); // "123"

final var ret2 = Long.toString(-456, 10);
System.out.println(ret2); // "-456"

final var ret3 = Long.toString(Long.MAX_VALUE, 10);
System.out.println(ret3); // "9223372036854775807"

final var ret4 = Long.toString(Long.MIN_VALUE, 10);
System.out.println(ret4); // "-9223372036854775808"
// Hexadecimal
final var ret1 = Long.toString(255, 16); // "ff"
System.out.println(ret1);

final var ret2 = Long.toString(-128, 16); // "-80"
System.out.println(ret2);

final var ret3 = Long.toString(Long.MAX_VALUE, 16);
System.out.println(ret3); // "7fffffffffffffff"

final var ret4 = Long.toString(Long.MIN_VALUE, 16);
System.out.println(ret4); // "-8000000000000000"

static String toUnsignedString (long i)

Returns a string representation of the argument as an unsigned decimal value.

System.out.println(Long.toUnsignedString(0)); // "0"
System.out.println(Long.toUnsignedString(123)); // "123"

System.out.println(Long.toUnsignedString(Long.MAX_VALUE)); // "9223372036854775807"
System.out.println(Long.toUnsignedString(Long.MIN_VALUE)); // "9223372036854775808"

System.out.println(Long.toUnsignedString(-1)); // "18446744073709551615"
System.out.println(Long.toUnsignedString(-2)); // "18446744073709551614"
System.out.println(Long.toUnsignedString(-3)); // "18446744073709551613"

static String toUnsignedString (long i, int radix)

Returns a string representation of the first argument as an unsigned integer value in the radix specified by the second argument.

System.out.println(Long.toUnsignedString(0, 10)); // "0"
System.out.println(Long.toUnsignedString(123, 10)); // "123"

System.out.println(Long.toUnsignedString(Long.MAX_VALUE, 10)); // "9223372036854775807"
System.out.println(Long.toUnsignedString(Long.MIN_VALUE, 10)); // "9223372036854775808"

System.out.println(Long.toUnsignedString(-1, 10)); // "18446744073709551615"
System.out.println(Long.toUnsignedString(-2, 10)); // "18446744073709551614"
System.out.println(Long.toUnsignedString(-3, 10)); // "18446744073709551613"
// Character.MAX_RADIX = 36
for (int i = 0; i < Character.MAX_RADIX + 1; i++) {
    //i =  0 : 0
    //i =  1 : 1
    //i =  2 : 2
    //i =  3 : 3
    //i =  4 : 4
    //i =  5 : 5
    //i =  6 : 6
    //i =  7 : 7
    //i =  8 : 8
    //i =  9 : 9
    //i = 10 : a
    //i = 11 : b
    //i = 12 : c
    //i = 13 : d
    //i = 14 : e
    //i = 15 : f
    //i = 16 : g
    //i = 17 : h
    //i = 18 : i
    //i = 19 : j
    //i = 20 : k
    //i = 21 : l
    //i = 22 : m
    //i = 23 : n
    //i = 24 : o
    //i = 25 : p
    //i = 26 : q
    //i = 27 : r
    //i = 28 : s
    //i = 29 : t
    //i = 30 : u
    //i = 31 : v
    //i = 32 : w
    //i = 33 : x
    //i = 34 : y
    //i = 35 : z
    //i = 36 : 10
    System.out.printf("i = %2d : %s%n", i, Long.toUnsignedString(i, Character.MAX_RADIX));
}

System.out.println(Long.toUnsignedString(0, Character.MAX_RADIX)); // "0"
System.out.println(Long.toUnsignedString(1234, Character.MAX_RADIX)); // "ya"

System.out.println(Long.toUnsignedString(-1, Character.MAX_RADIX)); // "3w5e11264sgsf"
System.out.println(Long.toUnsignedString(-2, Character.MAX_RADIX)); // "3w5e11264sgse"
System.out.println(Long.toUnsignedString(-3, Character.MAX_RADIX)); // "3w5e11264sgsd"

System.out.println(Long.toUnsignedString(Long.MAX_VALUE, Character.MAX_RADIX)); // "1y2p0ij32e8e7"
System.out.println(Long.toUnsignedString(Long.MIN_VALUE, Character.MAX_RADIX)); // "1y2p0ij32e8e8"

static Long valueOf (long l)

Returns a Long instance representing the specified long value.

System.out.println(Long.valueOf(0)); // 0
System.out.println(Long.valueOf(1)); // 1
System.out.println(Long.valueOf(2)); // 2
System.out.println(Long.valueOf(100)); // 100
System.out.println(Long.valueOf(9999999999L)); // 9999999999

System.out.println(Long.valueOf(-1)); // -1
System.out.println(Long.valueOf(-2)); // -2
System.out.println(Long.valueOf(-3)); // -3

System.out.println(Long.valueOf(Long.MAX_VALUE)); // 9223372036854775807
System.out.println(Long.valueOf(Long.MIN_VALUE)); // -9223372036854775808

static Long valueOf (String s)

Returns a Long object holding the value of the specified String.

System.out.println(Long.valueOf("0")); // 0
System.out.println(Long.valueOf("123")); // 123
System.out.println(Long.valueOf("+456")); // 456
System.out.println(Long.valueOf("-789")); // -789

System.out.println(Long.valueOf("9223372036854775807")); // 9223372036854775807
//Long.valueOf("9223372036854775808"); // NumberFormatException

System.out.println(Long.valueOf("-9223372036854775808")); // -9223372036854775808
//Long.valueOf("-9223372036854775809"); // NumberFormatException

static Long valueOf (String s, int radix)

Returns a Long object holding the value extracted from the specified String when parsed with the radix given by the second argument.

System.out.println(Long.valueOf("0", 10)); // 0
System.out.println(Long.valueOf("473", 10)); // 473
System.out.println(Long.valueOf("+42", 10)); // 42
System.out.println(Long.valueOf("-0", 10)); // 0
System.out.println(Long.valueOf("-FF", 16)); // -255
System.out.println(Long.valueOf("1100110", 2)); // 102
System.out.println(Long.valueOf("9223372036854775807", 10)); // 9223372036854775807
System.out.println(Long.valueOf("-9223372036854775808", 10)); // -9223372036854775808

//Long.valueOf("9223372036854775808", 10); // NumberFormatException
//Long.valueOf("99", 8); // NumberFormatException
//Long.valueOf("Hazelnut", 10); // NumberFormatException

System.out.println(Long.valueOf("Hazelnut", 36)); // 1356099454469

Related posts

To top of page