1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Java 实现十进制数转换为二进制

Java 实现十进制数转换为二进制

时间:2022-07-22 08:01:42

相关推荐

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-6if (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;}

Kotlin 开发者社区

国内第一Kotlin 开发者社区公众号,主要分享、交流 Kotlin 编程语言、Spring Boot、Android、React.js/Node.js、函数式编程、编程思想等相关主题。

越是喧嚣的世界,越需要宁静的思考。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。