{"id":1513,"date":"2022-03-31T23:34:41","date_gmt":"2022-03-31T15:34:41","guid":{"rendered":"http:\/\/diuut.com\/?p=1513"},"modified":"2022-03-31T23:46:55","modified_gmt":"2022-03-31T15:46:55","slug":"%e5%8d%81%e8%bf%9b%e5%88%b6%e8%bd%ac%e4%ba%8c%e8%bf%9b%e5%88%b6%e6%96%b9%e6%b3%95%e8%a7%a3%e6%9e%90%ef%bc%88inteager-tobinarystring%ef%bc%89","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=1513","title":{"rendered":"\u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u65b9\u6cd5\u89e3\u6790\uff08Inteager.toBinaryString\uff09"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>\u9700\u6c42\uff1a<\/p><cite>\u5341\u8fdb\u5236\u6570\u8f6c\u4e8c\u8fdb\u5236\u6570<\/cite><\/blockquote>\n\n\n\n<p>\u8be5\u65b9\u6cd5\u662fInteager\u4e2d\u9644\u5e26\u7684\u65b9\u6cd5\uff0c\u73b0\u5c06\u5176\u8c03\u7528\u63d0\u53d6\u51fa\u6765\u65b9\u4fbf\u53c2\u8003\uff0c\u9644\u5e26\u4e86\u539f\u6ce8\u89e3\u3002\uff08\u7b49\u4e8e\u6ca1\u89e3\u6790<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage xyz.diuut;\n\n\n\/**\n * @Author Diuut\n * @Date 2022\/3\/31  21:39\n *\/\npublic class toBinaryString {\n    public static void main(String&#91;] args) {\n        int num=257;\n        System.out.println(&quot;num1:&quot;+num);\n        System.out.println(&quot;num2:&quot;+toBinaryString(num));\n        System.out.println(&quot;num3:&quot;+Integer.parseInt(toBinaryString(num),2));\/\/\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\n        \/\/num:257\n        \/\/num:100000001\n        \/\/num3:257\n    }\n\n    \/**\n     * Returns a string representation of the integer argument as an\n     * unsigned integer in base&amp;nbsp;2.\n     *\n     * &lt;p&gt;The unsigned integer value is the argument plus 2&lt;sup&gt;32&lt;\/sup&gt;\n     * if the argument is negative; otherwise it is equal to the\n     * argument.  This value is converted to a string of ASCII digits\n     * in binary (base&amp;nbsp;2) with no extra leading {@code 0}s.\n     *\n     * &lt;p&gt;The value of the argument can be recovered from the returned\n     * string {@code s} by calling {@link\n     * Integer#parseUnsignedInt(String, int)\n     * Integer.parseUnsignedInt(s, 2)}.\n     *\n     * &lt;p&gt;If the unsigned magnitude is zero, it is represented by a\n     * single zero character {@code '0'} ({@code '\\u005Cu0030'});\n     * otherwise, the first character of the representation of the\n     * unsigned magnitude will not be the zero character. The\n     * characters {@code '0'} ({@code '\\u005Cu0030'}) and {@code\n     * '1'} ({@code '\\u005Cu0031'}) are used as binary digits.\n     *\n     * @param   i   an integer to be converted to a string.\n     * @return  the string representation of the unsigned integer value\n     *          represented by the argument in binary (base&amp;nbsp;2).\n     * @since   JDK1.0.2\n     *\/\n    public static String toBinaryString(int i) {\n        return toUnsignedString0(i, 1);\n    }\n    \/**\n     * The number of bits used to represent an {@code int} value in two's\n     * complement binary form.\n     *\n     * @since 1.5\n     *\/\n    private static final int SIZE = 32;\n\n    \/**\n     * Returns the greater of two {@code int} values. That is, the\n     * result is the argument closer to the value of\n     * {@link Integer#MAX_VALUE}. If the arguments have the same value,\n     * the result is that same value.\n     *\n     * @param   a   an argument.\n     * @param   b   another argument.\n     * @return  the larger of {@code a} and {@code b}.\n     *\/\n    private static int max(int a, int b) {\n        return (a &gt;= b) ? a : b;\n    }\n\n    \/**\n     * Convert the integer to an unsigned number.\n     *\/\n    private static String toUnsignedString0(int val, int shift) {\n        \/\/ assert shift &gt; 0 &amp;&amp; shift &lt;=5 : &quot;Illegal shift value&quot;;\n        int mag = SIZE - numberOfLeadingZeros(val);\n        int chars = max(((mag + (shift - 1)) \/ shift), 1);\n        char&#91;] buf = new char&#91;chars];\n\n        formatUnsignedInt(val, shift, buf, 0, chars);\n\n        \/\/ Use special constructor which takes over &quot;buf&quot;.\n        return new String(buf);\n    }\n    \/**\n     * Format a long (treated as unsigned) into a character buffer.\n     * @param val the unsigned int to format\n     * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)\n     * @param buf the character buffer to write to\n     * @param offset the offset in the destination buffer to start at\n     * @param len the number of characters to write\n     * @return the lowest character  location used\n     *\/\n    private static int formatUnsignedInt(int val, int shift, char&#91;] buf, int offset, int len) {\n        int charPos = len;\n        int radix = 1 &lt;&lt; shift;\n        int mask = radix - 1;\n        do {\n            buf&#91;offset + --charPos] = digits&#91;val &amp; mask];\n            val &gt;&gt;&gt;= shift;\n        } while (val != 0 &amp;&amp; charPos &gt; 0);\n\n        return charPos;\n    }\n\n    \/**\n     * All possible chars for representing a number as a String\n     * \u80fd\u8868\u793a\u6210\u6570\u5b57\u7684\u6240\u6709\u5b57\u7b26\u4e32\u96c6\u5408\n     *\/\n    static final char&#91;] digits = {\n            '0', '1', '2', '3', '4', '5',\n            '6', '7', '8', '9', 'a', 'b',\n            'c', 'd', 'e', 'f', 'g', 'h',\n            'i', 'j', 'k', 'l', 'm', 'n',\n            'o', 'p', 'q', 'r', 's', 't',\n            'u', 'v', 'w', 'x', 'y', 'z'\n    };\n    \/**\n     * Returns the number of zero bits preceding the highest-order\n     * (&quot;leftmost&quot;) one-bit in the two's complement binary representation\n     * of the specified {@code int} value.  Returns 32 if the\n     * specified value has no one-bits in its two's complement representation,\n     * in other words if it is equal to zero.\n     *\n     * &lt;p&gt;Note that this method is closely related to the logarithm base 2.\n     * For all positive {@code int} values x:\n     * &lt;ul&gt;\n     * &lt;li&gt;floor(log&lt;sub&gt;2&lt;\/sub&gt;(x)) = {@code 31 - numberOfLeadingZeros(x)}\n     * &lt;li&gt;ceil(log&lt;sub&gt;2&lt;\/sub&gt;(x)) = {@code 32 - numberOfLeadingZeros(x - 1)}\n     * &lt;\/ul&gt;\n     *\n     * @param i the value whose number of leading zeros is to be computed\n     * @return the number of zero bits preceding the highest-order\n     *     (&quot;leftmost&quot;) one-bit in the two's complement binary representation\n     *     of the specified {@code int} value, or 32 if the value\n     *     is equal to zero.\n     * @since 1.5\n     *\/\n    public static int numberOfLeadingZeros(int i) {\n        \/\/ HD, Figure 5-6\n        if (i == 0) {\n            return 32;\n        }\n        int n = 1;\n        if (i &gt;&gt;&gt; 16 == 0) {\n            n += 16;\n            i &lt;&lt;= 16;\n        }\n        if (i &gt;&gt;&gt; 24 == 0) {\n            n += 8;\n            i &lt;&lt;= 8;\n        }\n        if (i &gt;&gt;&gt; 28 == 0) {\n            n += 4;\n            i &lt;&lt;= 4;\n        }\n        if (i &gt;&gt;&gt; 30 == 0) {\n            n += 2;\n            i &lt;&lt;= 2;\n        }\n        n -= i &gt;&gt;&gt; 31;\n        return n;\n    }\n}\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\uff1a \u5341\u8fdb\u5236\u6570\u8f6c\u4e8c\u8fdb\u5236\u6570 \u8be5\u65b9\u6cd5\u662fInteager\u4e2d\u9644\u5e26\u7684\u65b9\u6cd5\uff0c\u73b0\u5c06\u5176\u8c03\u7528\u63d0\u53d6\u51fa\u6765\u65b9\u4fbf\u53c2\u8003\uff0c\u9644\u5e26\u4e86\u539f\u6ce8\u89e3\u3002\uff08<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=1513\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">\u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u65b9\u6cd5\u89e3\u6790\uff08Inteager.toBinaryString\uff09<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":657,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[37,49],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1513"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1513"}],"version-history":[{"count":4,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1513\/revisions"}],"predecessor-version":[{"id":1518,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1513\/revisions\/1518"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/657"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}