Advertisement
Guest User

Bitcount

a guest
Oct 25th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1.    /**
  2.      * Returns the number of one-bits in the two's complement binary
  3.      * representation of the specified {@code long} value.  This function is
  4.      * sometimes referred to as the <i>population count</i>.
  5.      *
  6.      * @param i the value whose bits are to be counted
  7.      * @return the number of one-bits in the two's complement binary
  8.      *     representation of the specified {@code long} value.
  9.      * @since 1.5
  10.      */
  11.      public static int bitCount(long i) {
  12.         // HD, Figure 5-14
  13.         i = i - ((i >>> 1) & 0x5555555555555555L);
  14.         i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L);
  15.         i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
  16.         i = i + (i >>> 8);
  17.         i = i + (i >>> 16);
  18.         i = i + (i >>> 32);
  19.         return (int)i & 0x7f;
  20.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement