Long.bitCount() in Java

Long.bitCount(): This method is available in java.lang.Long class of Java.

Syntax:

int java.lang.Long.bitCount(long i)

This method takes one argument of type long as its parameter. This method returns the number of one-bits in the two's complement binary representation of the specified long value.

Parameters: One parameter is required for this method.

i: the value whose bits are to be counted.

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

For Example:

long i = 17

Long.bitCount(i) = > It returns 2.

Approach

Java

public class LongBitcount {
    public static void main(String[] args) {

        long i = 17;
        System.out.println(Long.bitCount(i));

    }
}

Output:

2


Some methods of Long

Long.parseLong(CharSequence s, int beginIndex, int endIndex, int radix)It parses the CharSequence argument as a signed long in the specified radix, beginning at the beginIndex and extending to endIndex - 1.

Long.parseUnsignedLong(String s)It parses the string argument as an unsigned decimal long.

Long.parseUnsignedLong(String s, int radix)It parses the string argument as an unsigned long in the radix specified by the second argument.

Long.parseUnsignedLong(CharSequence s, int beginIndex, int endIndex, int radix)It parses the CharSequence argument as an unsigned long in the specified radix, beginning at the beginIndex and extending to endIndex - 1.

Long.remainderUnsigned()It returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.

Long.reverse(): It returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified long value.

Long.reverseBytes()It returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified long value.

Long.rotateLeft() It returns the value obtained by rotating the two's complement binary representation of the specified long value left by the specified number of bits.

Long.rotateRight()It returns the value obtained by rotating the two's complement binary representation of the specified long value right by the specified number of bits.

Long.signum()It returns the signum function of the specified long value.

Long.SIZEThe number of bits used to represent a long value in two complements binary form (i.e 64).

Long.sum() It adds two long values together as per the + operator.

Long.toBinaryString()It returns a string representation of the long argument as an unsigned integer in base 2.

Long.toHexString()It returns a string representation of the long argument as an unsigned integer in base 16.

Long.toOctalString()It returns a string representation of the long argument as an unsigned integer in base 8.

Long.toString(long i)It returns a String object representing the specified long. 

Long.toString(long i, int radix)It returns a string representation of the first argument in the radix specified by the second argument.

Long.toUnsignedString()It returns a string representation of the argument as an unsigned decimal value.

Long.valueOf(long l): It returns a Long instance representing the specified long value. 

Long.valueOf(String s): It returns a Long object holding the value of the specified String.

Long.valueOf(String s, int radix)It returns a Long object holding the value extracted from the specified String when parsed with the radix given by the second argument.

Long.bitCount()It returns the number of one-bits in the two's complement binary representation of the specified long value.

Long.BYTESThe number of bytes is used to represent a long value in two complements binary forms (i.e 8).

Long.compare()It compares two long values numerically.

Long.compareUnsigned()It compares two long values numerically treating the values as unsigned.

Long.decode()It decodes a String into a Long. Accepts decimal, hexadecimal, and octal numbers.

Long.divideUnsigned()It returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.

Long.getLong(String nm)It determines the long value of the system property with the specified name.

Long.getLong(String nm, long val)It determines the long value of the system property with the specified name.

Long.getLong(String nm, Long val)It returns the long value of the system property with the specified name. The first argument is treated as the name of a system property.

Long.hashCode()It returns a hash code for a long value.

Long.highestOneBit()It 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.

Long.lowestOneBit()It 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.

Long.max()It returns the greater of two long values.

Long.MAX_VALUEA constant holding the maximum value long can have, 2^63-1.

Long.min() It returns the smaller of two long values.

Long.MIN_VALUEA constant holding the minimum value long can have, -2^63.

Long.numberOfLeadingZeros()It 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.

Long.numberOfTrailingZeros() It 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.

Long.parseLong(String s)It parses the string argument as a signed decimal long.

Long.parseLong(String s, int radix)It parses the string argument as a signed long in the radix specified by the second argument.

No comments:

Post a Comment