您的位置 首页 > 腾讯云社区

Java 实现十进制数转换为二进制---一个会写诗的程序员

简单实现 public static String toBinary(int n) { StringBuilder sb = new StringBuilder(); for (int i = 31; i >= 0; i--) { // 将最高位的数移至最低位(移31位), 再 & 1 操作,由于1在内存中除了最低位是1,其余31位都是零,然后把这个数按十进制输出;再移次高位,做相同的操作,直到最后一位 sb.append(n >>> i & 1); } return sb.toString(); }JDK 实现Integer.toBinaryString(n)

源代码:

/** * Returns a string representation of the integer argument as an * unsigned integer in base&nbsp;2. * * <p>The unsigned integer value is the argument plus 2<sup>32</sup> * 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&nbsp;2) with no extra leading {@code 0}s. * * <p>The value of the argument can be recovered from the returned * string {@code s} by calling {@link * Integer#parseUnsignedInt(String, int) * Integer.parseUnsignedInt(s, 2)}. * * <p>If the unsigned magnitude is zero, it is represented by a * single zero character {@code '0'} ({@code 'u005Cu0030'}); * otherwise, the first character of the representation of the * unsigned magnitude will not be the zero character. The * characters {@code '0'} ({@code 'u005Cu0030'}) and {@code * '1'} ({@code 'u005Cu0031'}) are used as binary digits. * * @param i an integer to be converted to a string. * @return the string representation of the unsigned integer value * represented by the argument in binary (base&nbsp;2). * @see #parseUnsignedInt(String, int) * @see #toUnsignedString(int, int) * @since JDK1.0.2 */ public static String toBinaryString(int i) { return toUnsignedString0(i, 1); } /** * Convert the integer to an unsigned number. */ private static String toUnsignedString0(int val, int shift) { // assert shift > 0 && shift <=5 : "Illegal shift value"; int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val); int chars = Math.max(((mag + (shift - 1)) / shift), 1); char[] buf = new char[chars]; formatUnsignedInt(val, shift, buf, 0, chars); // Use special constructor which takes over "buf". return new String(buf, true); } /** * Returns the number of zero bits preceding the highest-order * ("leftmost") one-bit in the two's complement binary representation * of the specified {@code int} value. Returns 32 if the * specified value has no one-bits in its two's complement representation, * in other words if it is equal to zero. * * <p>Note that this method is closely related to the logarithm base 2. * For all positive {@code int} values x: * <ul> * <li>floor(log<sub>2</sub>(x)) = {@code 31 - numberOfLeadingZeros(x)} * <li>ceil(log<sub>2</sub>(x)) = {@code 32 - numberOfLeadingZeros(x - 1)} * </ul> * * @param i the value whose number of leading zeros is to be computed * @return the number of zero bits preceding the highest-order * ("leftmost") one-bit in the two's complement binary representation * of the specified {@code int} value, or 32 if the value * is equal to zero. * @since 1.5 */ public static int numberOfLeadingZeros(int i) { // HD, Figure 5-6 if (i == 0) return 32; int n = 1; if (i >>> 16 == 0) { n += 16; i <<= 16; } if (i >>> 24 == 0) { n += 8; i <<= 8; } if (i >>> 28 == 0) { n += 4; i <<= 4; } if (i >>> 30 == 0) { n += 2; i <<= 2; } n -= i >>> 31; return n; } /** * Format a long (treated as unsigned) into a character buffer. * @param val the unsigned int to format * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) * @param buf the character buffer to write to * @param offset the offset in the destination buffer to start at * @param len the number of characters to write * @return the lowest character location used */ static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) { int charPos = len; int radix = 1 << shift; int mask = radix - 1; do { buf[offset + --charPos] = Integer.digits[val & mask]; val >>>= shift; } while (val != 0 && charPos > 0); return charPos; } ---来自腾讯云社区的---一个会写诗的程序员

关于作者: 瞎采新闻

这里可以显示个人介绍!这里可以显示个人介绍!

热门文章

留言与评论(共有 0 条评论)
   
验证码: