1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| 757 public static int rotateLeft(int i, int distance) {
758 if (distance == 0) {
759 return i;
760 }
761 /*
762 * According to JLS3, 15.19, the right operand of a shift is always
763 * implicitly masked with 0x1F, which the negation of 'distance' is
764 * taking advantage of.
765 */
766 return ((i << distance) | (i >>> (-distance)));
767 }
768
769 /**
770 * Rotates the bits of the specified integer to the right by the specified
771 * number of bits.
772 *
773 * @param i
774 * the integer value to rotate right.
775 * @param distance
776 * the number of bits to rotate.
777 * @return the rotated value.
778 * @since 1.5
779 */
780 public static int rotateRight(int i, int distance) {
781 if (distance == 0) {
782 return i;
783 }
784 /*
785 * According to JLS3, 15.19, the right operand of a shift is always
786 * implicitly masked with 0x1F, which the negation of 'distance' is
787 * taking advantage of.
788 */
789 return ((i >>> distance) | (i << (-distance)));
790 } |
Partager