< prev index next >

src/java.base/share/classes/java/lang/Integer.java

Print this page
rev 49267 : [mq]: 8199843-Optimize-Integer-Long-highestOneBit


1557      * complement binary form.
1558      *
1559      * @since 1.8
1560      */
1561     public static final int BYTES = SIZE / Byte.SIZE;
1562 
1563     /**
1564      * Returns an {@code int} value with at most a single one-bit, in the
1565      * position of the highest-order ("leftmost") one-bit in the specified
1566      * {@code int} value.  Returns zero if the specified value has no
1567      * one-bits in its two's complement binary representation, that is, if it
1568      * is equal to zero.
1569      *
1570      * @param i the value whose highest one bit is to be computed
1571      * @return an {@code int} value with a single one-bit, in the position
1572      *     of the highest-order one-bit in the specified value, or zero if
1573      *     the specified value is itself equal to zero.
1574      * @since 1.5
1575      */
1576     public static int highestOneBit(int i) {
1577         // HD, Figure 3-1
1578         i |= (i >>  1);
1579         i |= (i >>  2);
1580         i |= (i >>  4);
1581         i |= (i >>  8);
1582         i |= (i >> 16);
1583         return i - (i >>> 1);
1584     }
1585 
1586     /**
1587      * Returns an {@code int} value with at most a single one-bit, in the
1588      * position of the lowest-order ("rightmost") one-bit in the specified
1589      * {@code int} value.  Returns zero if the specified value has no
1590      * one-bits in its two's complement binary representation, that is, if it
1591      * is equal to zero.
1592      *
1593      * @param i the value whose lowest one bit is to be computed
1594      * @return an {@code int} value with a single one-bit, in the position
1595      *     of the lowest-order one-bit in the specified value, or zero if
1596      *     the specified value is itself equal to zero.
1597      * @since 1.5
1598      */
1599     public static int lowestOneBit(int i) {
1600         // HD, Section 2-1
1601         return i & -i;
1602     }
1603 




1557      * complement binary form.
1558      *
1559      * @since 1.8
1560      */
1561     public static final int BYTES = SIZE / Byte.SIZE;
1562 
1563     /**
1564      * Returns an {@code int} value with at most a single one-bit, in the
1565      * position of the highest-order ("leftmost") one-bit in the specified
1566      * {@code int} value.  Returns zero if the specified value has no
1567      * one-bits in its two's complement binary representation, that is, if it
1568      * is equal to zero.
1569      *
1570      * @param i the value whose highest one bit is to be computed
1571      * @return an {@code int} value with a single one-bit, in the position
1572      *     of the highest-order one-bit in the specified value, or zero if
1573      *     the specified value is itself equal to zero.
1574      * @since 1.5
1575      */
1576     public static int highestOneBit(int i) {
1577         return i == 0 ? 0 : MIN_VALUE >>> numberOfLeadingZeros(i);






1578     }
1579 
1580     /**
1581      * Returns an {@code int} value with at most a single one-bit, in the
1582      * position of the lowest-order ("rightmost") one-bit in the specified
1583      * {@code int} value.  Returns zero if the specified value has no
1584      * one-bits in its two's complement binary representation, that is, if it
1585      * is equal to zero.
1586      *
1587      * @param i the value whose lowest one bit is to be computed
1588      * @return an {@code int} value with a single one-bit, in the position
1589      *     of the lowest-order one-bit in the specified value, or zero if
1590      *     the specified value is itself equal to zero.
1591      * @since 1.5
1592      */
1593     public static int lowestOneBit(int i) {
1594         // HD, Section 2-1
1595         return i & -i;
1596     }
1597 


< prev index next >