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

Print this page

 ``` `````` 110 * '\u0039' and '\u0061' through 111 * '\u007A'. If {@code radix} is 112 * N, then the first N of these characters 113 * are used as radix-N digits in the order shown. Thus, 114 * the digits for hexadecimal (radix 16) are 115 * {@code 0123456789abcdef}. If uppercase letters are 116 * desired, the {@link java.lang.String#toUpperCase()} method may 117 * be called on the result: 118 * 119 *
120 * {@code Integer.toString(n, 16).toUpperCase()} 121 *
122 * 123 * @param i an integer to be converted to a string. 124 * @param radix the radix to use in the string representation. 125 * @return a string representation of the argument in the specified radix. 126 * @see java.lang.Character#MAX_RADIX 127 * @see java.lang.Character#MIN_RADIX 128 */ 129 public static String toString(int i, int radix) { 130 131 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 132 radix = 10; 133 134 /* Use the faster version */ 135 if (radix == 10) { 136 return toString(i); 137 } 138 139 char buf[] = new char[33]; 140 boolean negative = (i < 0); 141 int charPos = 32; 142 143 if (!negative) { 144 i = -i; 145 } 146 147 while (i <= -radix) { 148 buf[charPos--] = digits[-(i % radix)]; 149 i = i / radix; 150 } 151 buf[charPos] = digits[-i]; 152 153 if (negative) { 154 buf[--charPos] = '-'; 155 } 156 157 return new String(buf, charPos, (33 - charPos)); 158 } 159 160 /** 161 * Returns a string representation of the integer argument as an 162 * unsigned integer in base 16. 163 * 164 *

The unsigned integer value is the argument plus 232 165 * if the argument is negative; otherwise, it is equal to the 166 * argument. This value is converted to a string of ASCII digits 167 * in hexadecimal (base 16) with no extra leading 168 * {@code 0}s. If the unsigned magnitude is zero, it is 169 * represented by a single zero character {@code '0'} 170 * ('\u0030'); otherwise, the first character of 171 * the representation of the unsigned magnitude will not be the 172 * zero character. The following characters are used as 173 * hexadecimal digits: 174 * 175 *

176 * {@code 0123456789abcdef} 177 *
178 * 179 * These are the characters '\u0030' through 180 * '\u0039' and '\u0061' through 181 * '\u0066'. If uppercase letters are 182 * desired, the {@link java.lang.String#toUpperCase()} method may 183 * be called on the result: 184 * 185 *
186 * {@code Integer.toHexString(n).toUpperCase()} 187 *
188 * 189 * @param i an integer to be converted to a string. 190 * @return the string representation of the unsigned integer value 191 * represented by the argument in hexadecimal (base 16). 192 * @since JDK1.0.2 193 */ 194 public static String toHexString(int i) { 195 return toUnsignedString(i, 4); 196 } 197 198 /** 199 * Returns a string representation of the integer argument as an 200 * unsigned integer in base 8. 201 * 202 *

The unsigned integer value is the argument plus 232 203 * if the argument is negative; otherwise, it is equal to the 204 * argument. This value is converted to a string of ASCII digits 205 * in octal (base 8) with no extra leading {@code 0}s. 206 * 207 *

If the unsigned magnitude is zero, it is represented by a 208 * single zero character {@code '0'} 209 * ('\u0030'); otherwise, the first character of 210 * the representation of the unsigned magnitude will not be the 211 * zero character. The following characters are used as octal 212 * digits: 213 * 214 *

215 * {@code 01234567} 216 *
217 * 218 * These are the characters '\u0030' through 219 * '\u0037'. 220 * 221 * @param i an integer to be converted to a string. 222 * @return the string representation of the unsigned integer value 223 * represented by the argument in octal (base 8). 224 * @since JDK1.0.2 225 */ 226 public static String toOctalString(int i) { 227 return toUnsignedString(i, 3); 228 } 229 230 /** 231 * Returns a string representation of the integer argument as an 232 * unsigned integer in base 2. 233 * 234 *

The unsigned integer value is the argument plus 232 235 * if the argument is negative; otherwise it is equal to the 236 * argument. This value is converted to a string of ASCII digits 237 * in binary (base 2) with no extra leading {@code 0}s. 238 * If the unsigned magnitude is zero, it is represented by a 239 * single zero character {@code '0'} 240 * ('\u0030'); otherwise, the first character of 241 * the representation of the unsigned magnitude will not be the 242 * zero character. The characters {@code '0'} 243 * ('\u0030') and {@code '1'} 244 * ('\u0031') are used as binary digits. 245 * 246 * @param i an integer to be converted to a string. 247 * @return the string representation of the unsigned integer value 248 * represented by the argument in binary (base 2). 249 * @since JDK1.0.2 250 */ 251 public static String toBinaryString(int i) { 252 return toUnsignedString(i, 1); 253 } 254 255 /** 256 * Convert the integer to an unsigned number. 257 */ 258 private static String toUnsignedString(int i, int shift) { 259 char[] buf = new char[32]; 260 int charPos = 32; 261 int radix = 1 << shift; 262 int mask = radix - 1; 263 do { 264 buf[--charPos] = digits[i & mask]; 265 i >>>= shift; 266 } while (i != 0); 267 268 return new String(buf, charPos, (32 - charPos)); 269 } 270 271 272 final static char [] DigitTens = { 273 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 274 '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', 275 '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', 276 '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', 277 '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', 278 '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', `````` 317 /** 318 * Returns a {@code String} object representing the 319 * specified integer. The argument is converted to signed decimal 320 * representation and returned as a string, exactly as if the 321 * argument and radix 10 were given as arguments to the {@link 322 * #toString(int, int)} method. 323 * 324 * @param i an integer to be converted. 325 * @return a string representation of the argument in base 10. 326 */ 327 public static String toString(int i) { 328 if (i == Integer.MIN_VALUE) 329 return "-2147483648"; 330 int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); 331 char[] buf = new char[size]; 332 getChars(i, size, buf); 333 return new String(0, size, buf); 334 } 335 336 /** 337 * Places characters representing the integer i into the 338 * character array buf. The characters are placed into 339 * the buffer backwards starting with the least significant 340 * digit at the specified index (exclusive), and working 341 * backwards from there. 342 * 343 * Will fail if i == Integer.MIN_VALUE 344 */ 345 static void getChars(int i, int index, char[] buf) { 346 int q, r; 347 int charPos = index; 348 char sign = 0; 349 350 if (i < 0) { 351 sign = '-'; 352 i = -i; 353 } 354 355 // Generate two digits per iteration 356 while (i >= 65536) { `````` 511 * characters in the string must all be decimal digits, except 512 * that the first character may be an ASCII minus sign {@code '-'} 513 * ('\u002D') to indicate a negative value or an 514 * ASCII plus sign {@code '+'} ('\u002B') to 515 * indicate a positive value. The resulting integer value is 516 * returned, exactly as if the argument and the radix 10 were 517 * given as arguments to the {@link #parseInt(java.lang.String, 518 * int)} method. 519 * 520 * @param s a {@code String} containing the {@code int} 521 * representation to be parsed 522 * @return the integer value represented by the argument in decimal. 523 * @exception NumberFormatException if the string does not contain a 524 * parsable integer. 525 */ 526 public static int parseInt(String s) throws NumberFormatException { 527 return parseInt(s,10); 528 } 529 530 /** 531 * Returns an {@code Integer} object holding the value 532 * extracted from the specified {@code String} when parsed 533 * with the radix given by the second argument. The first argument 534 * is interpreted as representing a signed integer in the radix 535 * specified by the second argument, exactly as if the arguments 536 * were given to the {@link #parseInt(java.lang.String, int)} 537 * method. The result is an {@code Integer} object that 538 * represents the integer value specified by the string. 539 * 540 *

In other words, this method returns an {@code Integer} 541 * object equal to the value of: 542 * 543 *

544 * {@code new Integer(Integer.parseInt(s, radix))} 545 *
546 * 547 * @param s the string to be parsed. 548 * @param radix the radix to be used in interpreting {@code s} 549 * @return an {@code Integer} object holding the value 550 * represented by the string argument in the specified ``````1014 } 1015 1016 /** 1017 * Compares two {@code int} values numerically. 1018 * The value returned is identical to what would be returned by: 1019 *
1020      *    Integer.valueOf(x).compareTo(Integer.valueOf(y)) 1021      *
1022 * 1023 * @param x the first {@code int} to compare 1024 * @param y the second {@code int} to compare 1025 * @return the value {@code 0} if {@code x == y}; 1026 * a value less than {@code 0} if {@code x < y}; and 1027 * a value greater than {@code 0} if {@code x > y} 1028 * @since 1.7 1029 */ 1030 public static int compare(int x, int y) { 1031 return (x < y) ? -1 : ((x == y) ? 0 : 1); 1032 } 1033 1034 1035 // Bit twiddling 1036 1037 /** 1038 * The number of bits used to represent an {@code int} value in two's 1039 * complement binary form. 1040 * 1041 * @since 1.5 1042 */ 1043 public static final int SIZE = 32; 1044 1045 /** 1046 * Returns an {@code int} value with at most a single one-bit, in the 1047 * position of the highest-order ("leftmost") one-bit in the specified 1048 * {@code int} value. Returns zero if the specified value has no 1049 * one-bits in its two's complement binary representation, that is, if it 1050 * is equal to zero. 1051 * 1052 * @return an {@code int} value with a single one-bit, in the position 1053 * of the highest-order one-bit in the specified value, or zero if ``` ``` `````` 110 * '\u0039' and '\u0061' through 111 * '\u007A'. If {@code radix} is 112 * N, then the first N of these characters 113 * are used as radix-N digits in the order shown. Thus, 114 * the digits for hexadecimal (radix 16) are 115 * {@code 0123456789abcdef}. If uppercase letters are 116 * desired, the {@link java.lang.String#toUpperCase()} method may 117 * be called on the result: 118 * 119 *
120 * {@code Integer.toString(n, 16).toUpperCase()} 121 *
122 * 123 * @param i an integer to be converted to a string. 124 * @param radix the radix to use in the string representation. 125 * @return a string representation of the argument in the specified radix. 126 * @see java.lang.Character#MAX_RADIX 127 * @see java.lang.Character#MIN_RADIX 128 */ 129 public static String toString(int i, int radix) { 130 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 131 radix = 10; 132 133 /* Use the faster version */ 134 if (radix == 10) { 135 return toString(i); 136 } 137 138 char buf[] = new char[33]; 139 boolean negative = (i < 0); 140 int charPos = 32; 141 142 if (!negative) { 143 i = -i; 144 } 145 146 while (i <= -radix) { 147 buf[charPos--] = digits[-(i % radix)]; 148 i = i / radix; 149 } 150 buf[charPos] = digits[-i]; 151 152 if (negative) { 153 buf[--charPos] = '-'; 154 } 155 156 return new String(buf, charPos, (33 - charPos)); 157 } 158 159 /** 160 * Returns an unsigned string representation of the first argument 161 * in the radix specified by the second argument. 162 * 163 *

If the radix is smaller than {@code Character.MIN_RADIX} 164 * or larger than {@code Character.MAX_RADIX}, then the radix 165 * {@code 10} is used instead. 166 * 167 *

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

If the magnitude is zero, it is represented by a single zero 171 * character {@code '0'} ('\u0030'); otherwise, 172 * the first character of the representation of the magnitude will 173 * not be the zero character. 174 * 175 *

The characters used as digits and the behavior of radixes 176 * is the same as {@link #toString(int, int) toString}. 177 * 178 * @param i an integer to be converted to an unsigned string. 179 * @param radix the radix to use in the string representation. 180 * @return an unsigned string representation of the argument in the specified radix. 181 * @see #toString(int, int) 182 * @since 1.7 183 */ 184 public static String toUnsignedString(int i, int radix) { 185 return Long.toString(toUnsignedLong(i), radix); 186 } 187 188 /** 189 * Returns a string representation of the integer argument as an 190 * unsigned integer in base 16. 191 * 192 *

The unsigned integer value is the argument plus 232 193 * if the argument is negative; otherwise, it is equal to the 194 * argument. This value is converted to a string of ASCII digits 195 * in hexadecimal (base 16) with no extra leading 196 * {@code 0}s. If the unsigned magnitude is zero, it is 197 * represented by a single zero character {@code '0'} 198 * ('\u0030'); otherwise, the first character of 199 * the representation of the unsigned magnitude will not be the 200 * zero character. The following characters are used as 201 * hexadecimal digits: 202 * 203 *

204 * {@code 0123456789abcdef} 205 *
206 * 207 * These are the characters '\u0030' through 208 * '\u0039' and '\u0061' through 209 * '\u0066'. If uppercase letters are 210 * desired, the {@link java.lang.String#toUpperCase()} method may 211 * be called on the result: 212 * 213 *
214 * {@code Integer.toHexString(n).toUpperCase()} 215 *
216 * 217 * @param i an integer to be converted to a string. 218 * @return the string representation of the unsigned integer value 219 * represented by the argument in hexadecimal (base 16). 220 * @since JDK1.0.2 221 */ 222 public static String toHexString(int i) { 223 return toUnsignedString0(i, 4); 224 } 225 226 /** 227 * Returns a string representation of the integer argument as an 228 * unsigned integer in base 8. 229 * 230 *

The unsigned integer value is the argument plus 232 231 * if the argument is negative; otherwise, it is equal to the 232 * argument. This value is converted to a string of ASCII digits 233 * in octal (base 8) with no extra leading {@code 0}s. 234 * 235 *

If the unsigned magnitude is zero, it is represented by a 236 * single zero character {@code '0'} 237 * ('\u0030'); otherwise, the first character of 238 * the representation of the unsigned magnitude will not be the 239 * zero character. The following characters are used as octal 240 * digits: 241 * 242 *

243 * {@code 01234567} 244 *
245 * 246 * These are the characters '\u0030' through 247 * '\u0037'. 248 * 249 * @param i an integer to be converted to a string. 250 * @return the string representation of the unsigned integer value 251 * represented by the argument in octal (base 8). 252 * @since JDK1.0.2 253 */ 254 public static String toOctalString(int i) { 255 return toUnsignedString0(i, 3); 256 } 257 258 /** 259 * Returns a string representation of the integer argument as an 260 * unsigned integer in base 2. 261 * 262 *

The unsigned integer value is the argument plus 232 263 * if the argument is negative; otherwise it is equal to the 264 * argument. This value is converted to a string of ASCII digits 265 * in binary (base 2) with no extra leading {@code 0}s. 266 * If the unsigned magnitude is zero, it is represented by a 267 * single zero character {@code '0'} 268 * ('\u0030'); otherwise, the first character of 269 * the representation of the unsigned magnitude will not be the 270 * zero character. The characters {@code '0'} 271 * ('\u0030') and {@code '1'} 272 * ('\u0031') are used as binary digits. 273 * 274 * @param i an integer to be converted to a string. 275 * @return the string representation of the unsigned integer value 276 * represented by the argument in binary (base 2). 277 * @since JDK1.0.2 278 */ 279 public static String toBinaryString(int i) { 280 return toUnsignedString0(i, 1); 281 } 282 283 /** 284 * Convert the integer to an unsigned number. 285 */ 286 private static String toUnsignedString0(int i, int shift) { 287 char[] buf = new char[32]; 288 int charPos = 32; 289 int radix = 1 << shift; 290 int mask = radix - 1; 291 do { 292 buf[--charPos] = digits[i & mask]; 293 i >>>= shift; 294 } while (i != 0); 295 296 return new String(buf, charPos, (32 - charPos)); 297 } 298 299 300 final static char [] DigitTens = { 301 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 302 '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', 303 '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', 304 '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', 305 '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', 306 '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', `````` 345 /** 346 * Returns a {@code String} object representing the 347 * specified integer. The argument is converted to signed decimal 348 * representation and returned as a string, exactly as if the 349 * argument and radix 10 were given as arguments to the {@link 350 * #toString(int, int)} method. 351 * 352 * @param i an integer to be converted. 353 * @return a string representation of the argument in base 10. 354 */ 355 public static String toString(int i) { 356 if (i == Integer.MIN_VALUE) 357 return "-2147483648"; 358 int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); 359 char[] buf = new char[size]; 360 getChars(i, size, buf); 361 return new String(0, size, buf); 362 } 363 364 /** 365 * Returns an unsigned string representation of the argument. 366 * 367 * The argument is converted to unsigned decimal representation 368 * and returned as a string exactly as if the argument and radix 369 * 10 were given as arguments to the {@link #toUnsignedString(int, 370 * int)} method. 371 * 372 * @param i an integer to be converted to an unsigned string. 373 * @return an unsigned string representation of the argument. 374 * @see #toUnsignedString(int, int) 375 * @since 1.7 376 */ 377 public static String toUnsignedString(int i) { 378 return Long.toString(toUnsignedLong(i)); 379 } 380 381 /** 382 * Places characters representing the integer i into the 383 * character array buf. The characters are placed into 384 * the buffer backwards starting with the least significant 385 * digit at the specified index (exclusive), and working 386 * backwards from there. 387 * 388 * Will fail if i == Integer.MIN_VALUE 389 */ 390 static void getChars(int i, int index, char[] buf) { 391 int q, r; 392 int charPos = index; 393 char sign = 0; 394 395 if (i < 0) { 396 sign = '-'; 397 i = -i; 398 } 399 400 // Generate two digits per iteration 401 while (i >= 65536) { `````` 556 * characters in the string must all be decimal digits, except 557 * that the first character may be an ASCII minus sign {@code '-'} 558 * ('\u002D') to indicate a negative value or an 559 * ASCII plus sign {@code '+'} ('\u002B') to 560 * indicate a positive value. The resulting integer value is 561 * returned, exactly as if the argument and the radix 10 were 562 * given as arguments to the {@link #parseInt(java.lang.String, 563 * int)} method. 564 * 565 * @param s a {@code String} containing the {@code int} 566 * representation to be parsed 567 * @return the integer value represented by the argument in decimal. 568 * @exception NumberFormatException if the string does not contain a 569 * parsable integer. 570 */ 571 public static int parseInt(String s) throws NumberFormatException { 572 return parseInt(s,10); 573 } 574 575 /** 576 * Parses the string argument as an unsigned integer in the radix 577 * specified by the second argument. 578 * 579 * The characters in the string must all be digits of the 580 * specified radix (as determined by whether {@link 581 * java.lang.Character#digit(char, int)} returns a nonnegative 582 * value), except that the first character may be an ASCII plus 583 * sign {@code '+'} ('\u002B'). The resulting 584 * integer value is returned. 585 * 586 *

An exception of type {@code NumberFormatException} is 587 * thrown if any of the following situations occurs: 588 *

589 *
• The first argument is {@code null} or is a string of 590 * length zero. 591 * 592 *
• The radix is either smaller than 593 * {@link java.lang.Character#MIN_RADIX} or 594 * larger than {@link java.lang.Character#MAX_RADIX}. 595 * 596 *
• Any character of the string is not a digit of the specified 597 * radix, except that the first character may be a plus sign 598 * {@code '+'} ('\u002B') provided that the 599 * string is longer than length 1. 600 * 601 *
• The value represented by the string is larger than the 602 * largest unsigned {@code int}. 603 * 604 *
605 * 606 * 607 * @param s the {@code String} containing the unsigned integer 608 * representation to be parsed 609 * @param radix the radix to be used while parsing {@code s}. 610 * @return the integer represented by the string argument in the 611 * specified radix. 612 * @throws NumberFormatException if the {@code String} 613 * does not contain a parsable {@code int}. 614 * @since 1.7 615 */ 616 public static int parseUnsignedInt(String s, int radix) 617 throws NumberFormatException { 618 if (s == null) { 619 throw new NumberFormatException("null"); 620 } 621 622 int len = s.length(); 623 if (len > 0) { 624 char firstChar = s.charAt(0); 625 if (firstChar == '-') { 626 throw new 627 NumberFormatException(String.format("Illegal leading minus sign " + 628 "on unsigned string %s.", s)); 629 } else { 630 long ell = Long.parseLong(s, radix); 631 if ((ell & 0xffffffff00000000L) == 0) { 632 return (int) ell; 633 } else { 634 throw new 635 NumberFormatException(String.format("String value %s exceeds " + 636 "range of unsigned int.", s)); 637 } 638 } 639 } else { 640 throw NumberFormatException.forInputString(s); 641 } 642 } 643 644 /** 645 * Parses the string argument as an unsigned decimal integer. The 646 * characters in the string must all be decimal digits, except 647 * that the first character may be an an ASCII plus sign {@code 648 * '+'} ('\u002B'). The resulting integer value 649 * is returned, exactly as if the argument and the radix 10 were 650 * given as arguments to the {@link 651 * #parseUnsignedInt(java.lang.String, int)} method. 652 * 653 * @param s a {@code String} containing the unsigned {@code int} 654 * representation to be parsed 655 * @return the unsigned integer value represented by the argument in decimal. 656 * @throws NumberFormatException if the string does not contain a 657 * parsable unsigned integer. 658 * @since 1.7 659 */ 660 public static int parseUnsignedInt(String s) throws NumberFormatException { 661 return parseUnsignedInt(s, 10); 662 } 663 664 /** 665 * Returns an {@code Integer} object holding the value 666 * extracted from the specified {@code String} when parsed 667 * with the radix given by the second argument. The first argument 668 * is interpreted as representing a signed integer in the radix 669 * specified by the second argument, exactly as if the arguments 670 * were given to the {@link #parseInt(java.lang.String, int)} 671 * method. The result is an {@code Integer} object that 672 * represents the integer value specified by the string. 673 * 674 *

In other words, this method returns an {@code Integer} 675 * object equal to the value of: 676 * 677 *

678 * {@code new Integer(Integer.parseInt(s, radix))} 679 *
680 * 681 * @param s the string to be parsed. 682 * @param radix the radix to be used in interpreting {@code s} 683 * @return an {@code Integer} object holding the value 684 * represented by the string argument in the specified ``````1148 } 1149 1150 /** 1151 * Compares two {@code int} values numerically. 1152 * The value returned is identical to what would be returned by: 1153 *
1154      *    Integer.valueOf(x).compareTo(Integer.valueOf(y)) 1155      *
1156 * 1157 * @param x the first {@code int} to compare 1158 * @param y the second {@code int} to compare 1159 * @return the value {@code 0} if {@code x == y}; 1160 * a value less than {@code 0} if {@code x < y}; and 1161 * a value greater than {@code 0} if {@code x > y} 1162 * @since 1.7 1163 */ 1164 public static int compare(int x, int y) { 1165 return (x < y) ? -1 : ((x == y) ? 0 : 1); 1166 } 1167 1168 /** 1169 * Compares two {@code int} values numerically treating the values 1170 * as unsigned. 1171 * 1172 * @param x the first {@code int} to compare 1173 * @param y the second {@code int} to compare 1174 * @return the value {@code 0} if {@code x == y}; a value less 1175 * than {@code 0} if {@code x < y} as unsigned values; and 1176 * a value greater than {@code 0} if {@code x > y} as 1177 * unsigned values 1178 * @since 1.7 1179 */ 1180 public static int compareUnsigned(int x, int y) { 1181 return Long.compare(toUnsignedLong(x), toUnsignedLong(y)); 1182 } 1183 1184 /** 1185 * Converts the argument to a {@code long} by an unsigned 1186 * conversion. In an unsigned conversion to a {@code long}, the 1187 * high-order 32 bits of the {@code long} are zero and the 1188 * low-order 32 bits are equal to the bits of the integer 1189 * argument. 1190 * 1191 * @return the argument converted to {@code long} by an unsigned 1192 * conversion 1193 * @param x the value to convert to an unsigned {@code long} 1194 * @since 1.7 1195 */ 1196 public static long toUnsignedLong(int x) { 1197 return ((long) x) & 0xffffffffL; 1198 } 1199 1200 /** 1201 * Returns the unsigned quotient of dividing the first argument by 1202 * the second where each argument is interpreted as an unsigned 1203 * value. 1204 * 1205 * In other words, return the unsigned value of {@code 1206 * (dividend / divisor)}. 1207 * 1208 * @return the unsigned quotient of the first argument divided by 1209 * the second argument 1210 * @param dividend the value to be divided 1211 * @param divisor the value doing the dividing 1212 * @since 1.7 1213 */ 1214 public static int divideUnsigned(int dividend, int divisor) { 1215 return (int)(toUnsignedLong(dividend)/toUnsignedLong(divisor)); 1216 } 1217 1218 /** 1219 * Returns the unsigned remainder from dividing the first argument by 1220 * the second where each argument is interpreted as an unsigned 1221 * value. 1222 * 1223 * In other words, return the unsigned value of {@code 1224 * (dividend % divisor)}. 1225 * 1226 * @return the unsigned remainder of the first argument divided by 1227 * the second argument 1228 * @param dividend the value to be divided 1229 * @param divisor the value doing the dividing 1230 * @since 1.7 1231 */ 1232 public static int remainderUnsigned(int dividend, int divisor) { 1233 return (int)(toUnsignedLong(dividend)%toUnsignedLong(divisor)); 1234 } 1235 1236 1237 // Bit twiddling 1238 1239 /** 1240 * The number of bits used to represent an {@code int} value in two's 1241 * complement binary form. 1242 * 1243 * @since 1.5 1244 */ 1245 public static final int SIZE = 32; 1246 1247 /** 1248 * Returns an {@code int} value with at most a single one-bit, in the 1249 * position of the highest-order ("leftmost") one-bit in the specified 1250 * {@code int} value. Returns zero if the specified value has no 1251 * one-bits in its two's complement binary representation, that is, if it 1252 * is equal to zero. 1253 * 1254 * @return an {@code int} value with a single one-bit, in the position 1255 * of the highest-order one-bit in the specified value, or zero if ```