Integer bitcount java как использовать
Перейти к содержимому

Integer bitcount java как использовать

  • автор:

Java.lang.Integer.bitCount() Method

The java.lang.Integer.bitCount() method returns the number of one-bits in the two’s complement binary representation of the specified int value i. This is sometimes referred to as the population count.

Declaration

Following is the declaration for java.lang.Integer.bitCount() method

Parameters

i − This is the int value.

Return Value

This method returns the number of one-bits in the two’s complement binary representation of the specified int value.

Class Integer

In addition, this class provides several methods for converting an int to a String and a String to an int , as well as other constants and methods useful when dealing with an int .

This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.

Implementation note: The implementations of the «bit twiddling» methods (such as highestOneBit and numberOfTrailingZeros ) are based on material from Henry S. Warren, Jr.’s Hacker’s Delight, (Addison Wesley, 2002).

Field Summary

Constructor Summary

Method Summary

Methods declared in class java.lang.Object

Field Details

MIN_VALUE

MAX_VALUE

BYTES

Constructor Details

Integer

Integer

Method Details

toString

If the radix is smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX , then the radix 10 is used instead.

If the first argument is negative, the first element of the result is the ASCII minus character ‘-‘ ( ‘\u002D’ ). If the first argument is not negative, no sign character appears in the result.

The remaining characters of the result represent the magnitude of the first argument. If the magnitude is zero, it is represented by a single zero character ‘0’ ( ‘\u0030’ ); otherwise, the first character of the representation of the magnitude will not be the zero character. The following ASCII characters are used as digits:

toUnsignedString

If the radix is smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX , then the radix 10 is used instead.

Note that since the first argument is treated as an unsigned value, no leading sign character is printed.

If the magnitude is zero, it is represented by a single zero character ‘0’ ( ‘\u0030’ ); otherwise, the first character of the representation of the magnitude will not be the zero character.

The behavior of radixes and the characters used as digits are the same as toString .

toHexString

The unsigned integer value is the argument plus 2 32 if the argument is negative; otherwise, it is equal to the argument. This value is converted to a string of ASCII digits in hexadecimal (base 16) with no extra leading 0 s.

The value of the argument can be recovered from the returned string s by calling Integer.parseUnsignedInt(s, 16) .

If the unsigned magnitude is zero, it is represented by a single zero character ‘0’ ( ‘\u0030’ ); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The following characters are used as hexadecimal digits:

toOctalString

The unsigned integer value is the argument plus 2 32 if the argument is negative; otherwise, it is equal to the argument. This value is converted to a string of ASCII digits in octal (base 8) with no extra leading 0 s.

The value of the argument can be recovered from the returned string s by calling Integer.parseUnsignedInt(s, 8) .

If the unsigned magnitude is zero, it is represented by a single zero character ‘0’ ( ‘\u0030’ ); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The following characters are used as octal digits:

toBinaryString

The unsigned integer value is the argument plus 2 32 if the argument is negative; otherwise it is equal to the argument. This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0 s.

The value of the argument can be recovered from the returned string s by calling Integer.parseUnsignedInt(s, 2) .

If the unsigned magnitude is zero, it is represented by a single zero character ‘0’ ( ‘\u0030’ ); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The characters ‘0’ ( ‘\u0030’ ) and ‘1’ ( ‘\u0031’ ) are used as binary digits.

toString

toUnsignedString

parseInt

  • The first argument is null or is a string of length zero.
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX .
  • Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign ‘-‘ ( ‘\u002D’ ) or plus sign ‘+’ ( ‘\u002B’ ) provided that the string is longer than length 1.
  • The value represented by the string is not a value of type int .

parseInt

The method does not take steps to guard against the CharSequence being mutated while parsing.

parseInt

parseUnsignedInt

  • The first argument is null or is a string of length zero.
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX .
  • Any character of the string is not a digit of the specified radix, except that the first character may be a plus sign ‘+’ ( ‘\u002B’ ) provided that the string is longer than length 1.
  • The value represented by the string is larger than the largest unsigned int , 2 32 -1.

parseUnsignedInt

The method does not take steps to guard against the CharSequence being mutated while parsing.

parseUnsignedInt

valueOf

In other words, this method returns an Integer object equal to the value of:

valueOf

In other words, this method returns an Integer object equal to the value of:

valueOf

byteValue

shortValue

intValue

longValue

floatValue

doubleValue

toString

hashCode

hashCode

equals

getInteger

The first argument is treated as the name of a system property. System properties are accessible through the System.getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value using the grammar supported by decode and an Integer object representing this value is returned.

If there is no property with the specified name, if the specified name is empty or null , or if the property does not have the correct numeric format, then null is returned.

In other words, this method returns an Integer object equal to the value of:

getInteger

The first argument is treated as the name of a system property. System properties are accessible through the System.getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value using the grammar supported by decode and an Integer object representing this value is returned.

The second argument is the default value. An Integer object that represents the value of the second argument is returned if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null .

In other words, this method returns an Integer object equal to the value of:

getInteger

  • If the property value begins with the two ASCII characters 0x or the ASCII character # , not followed by a minus sign, then the rest of it is parsed as a hexadecimal integer exactly as by the method valueOf(java.lang.String, int) with radix 16.
  • If the property value begins with the ASCII character 0 followed by another character, it is parsed as an octal integer exactly as by the method valueOf(java.lang.String, int) with radix 8.
  • Otherwise, the property value is parsed as a decimal integer exactly as by the method valueOf(java.lang.String, int) with radix 10.

The second argument is the default value. The default value is returned if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null .

What is the Integer.bitCount method in Java?

Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.

The bitCount static method of the Integer class can be used to find the number of 1 bits in the two’s complement binary representation of the given int value.

Syntax

The bitCount method takes the int value as an argument and returns an integer value that represents the number of 1 s present in the two’s complement binary representation of the argument.

Integer.bitCount() method in Java with Examples

The bitCount() is a static method of the Integer class that counts the number of 1 bit in the two’s complement binary representation of the integer. It was introduced in Java 1.5, and this post will discuss the bitCount() method in detail.

  • Method declaration – public static int bitCount(int i)
  • What does it do? It will count the number of 1’s in the two’s complement binary representation of the number passed in the argument
  • What does it return? It will return an int value denoting the number of 1’s in the two’s complement binary representation

What is the two’s complement of a number? We can easily count the number of 1 bit for a positive number, but what about negative numbers? How to calculate the bit count for negative numbers? For that, we first have to understand the 2’s complement.

This StackOverflow answer has explained 2’s complement quite well. We recommend reading it to get the basic idea of 2’s complement.

But here is a small conclusion on how we can calculate the 2’s complement of a number.

  • Let’s say we have to calculate the 2’s complement of -6.
  • First, write the binary representation of its corresponding positive integer +6.
  • The binary representation of 6 is 110.
  • Invert 0’s and 1’s. So, the new binary representation would be 001.
  • Now, add 1 to it.
  • After adding 1, we will get 010.

So, the 2’s complement of -6 would be 010. This is when we have only considered 3 bits. But if we have considered 32 bits, then 2’s complement of -6 would be 11111111111111111111111111111010.

Let’s first calculate the bit count for positive numbers

Let’s take an integer value = 6. The binary representation of 6 is 110, so the count of 1 bit should be 2.

Output –

Now, let’s calculate the bit count for the negative numbers

Let’s take the negative integer number = -6. The 2’s complement binary representation of -6 is 11111111111111111111111111111010. Thus the bit count for -6 would be 30.

Output –

Time Complexity of the bitCount() method

The time complexity of the Integer bitCount() method is O(1) as it performs only constant operations on the input. Below is the internal implementation of the bitCount() method –

Q- What if we passed a null Integer value to bitCount() argument?

It will throw a NullPointerException as illustrated by the below program.

Output –

Please visit this link to learn more about the Integer wrapper class of java and its other functions or methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected].

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *