{"id":183462,"date":"2013-06-16T14:25:02","date_gmt":"2013-06-16T10:25:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=183462"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=183462","title":{"rendered":"<span class=\"post_title\">\u041f\u0440\u0430\u043a\u0442\u0438\u043a\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f XOR \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438<\/span>"},"content":{"rendered":"<div class=\"content html_format\"> \t\t\t\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e \u0431\u0438\u0442\u043e\u0432\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 XOR (\u0438\u0441\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0435\u0435 \u0418\u041b\u0418) \u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0435\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043d\u0430 JAVA.<\/p>\n<p>  \u0418 \u0442\u0430\u043a, XOR \u2013 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u00ab\u0438\u0441\u0442\u0438\u043d\u0430\u00bb \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u00ab\u0438\u0441\u0442\u0438\u043d\u0430\u00bb.<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/9af\/1de\/e09\/9af1dee09d4d36ff0b15bdb4aae19e3b.png\" alt=\"image\"\/><\/p>\n<p>  <a name=\"habracut\"><\/a><\/p>\n<p>  XOR \u043e\u0431\u043b\u0430\u0434\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438:<\/p>\n<p>  <i>a XOR 0 = a<br \/>  a XOR a = 0<br \/>  a XOR b = b XOR a<br \/>  (a XOR b) XOR b = a<\/i><\/p>\n<p>  \u0412 \u044f\u0437\u044b\u043a\u0435 JAVA (\u0430 \u0442\u0430\u043a\u0436\u0435 \u0432 \u0421, \u0421++, C#, Ruby, PHP, JavaScript) \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c \u00ab^\u00bb.<\/p>\n<h4>\u041e\u0431\u043c\u0435\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439<\/h4>\n<p>  \u0421 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 XOR \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043e\u0434\u043d\u043e\u0442\u0438\u043f\u043d\u044b\u0445 \u043f\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439:   <\/p>\n<pre><code class=\"java\">\t\tint x = 5, y = 7;   \t\tx = x^y; \/\/ x == 2 \t\ty = x^y; \/\/ y == 5 \t\tx = x^y; \/\/ x == 7 <\/code><\/pre>\n<p>  \u0438\u043b\u0438 \u0432 \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438:  <\/p>\n<pre><code class=\"java\">\t\ty ^= (x ^= y); \t\tx ^= y; <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0440\u0435\u0432\u0435\u0440\u0441 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438:  <\/p>\n<pre><code class=\"java\">\t  public static final String reverseWithXOR(String string) { \t        char[] array = string.toCharArray(); \t        int length = array.length; \t        int half = (int) Math.floor(array.length \/ 2); \t        for (int i = 0; i &lt; half; i++) { \t            array[i] ^= array[length - i - 1]; \t            array[length - i - 1] ^= array[i]; \t            array[i] ^= array[length - i - 1];\t \t        } \t        return String.valueOf(array); \t    }\t\t   <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/dc5\/cbd\/5e7\/dc5cbd5e7b8d19e4c15a6b48f118480a.png\"\/><\/p>\n<h4>\u0428\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/h4>\n<p>  \u0428\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 XOR \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e:<br \/>  <i>(a XOR k) XOR k = a<\/i><br \/>  \u0433\u0434\u0435 <i>k<\/i> \u2013 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u0432 \u0440\u043e\u043b\u0438 \u043a\u043b\u044e\u0447\u0430<\/p>\n<p>  \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a\u0438:  <\/p>\n<pre><code class=\"java\">\tpublic static byte[] encode(String pText, String pKey) { \t\tbyte[] txt = pText.getBytes(); \t\tbyte[] key = pKey.getBytes(); \t\tbyte[] res = new byte[pText.length()];  \t\tfor (int i = 0; i &lt; txt.length; i++) { \t\t\tres[i] = (byte) (txt[i] ^ key[i % key.length]); \t\t}  \t\treturn res; \t} <\/code><\/pre>\n<p>  \u0438 \u0434\u0435\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f:  <\/p>\n<pre><code class=\"java\">\tpublic static String decode(byte[] pText, String pKey) { \t\tbyte[] res = new byte[pText.length]; \t\tbyte[] key = pKey.getBytes();  \t\tfor (int i = 0; i &lt; pText.length; i++) { \t\t\tres[i] = (byte) (pText[i] ^ key[i % key.length]); \t\t}  \t\treturn new String(res); \t} <\/code><\/pre>\n<p>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0437\u0430\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u201c\u0421\u044a\u0435\u0448\u044c \u0435\u0449\u0451 \u044d\u0442\u0438\u0445 \u043c\u044f\u0433\u043a\u0438\u0445 \u0444\u0440\u0430\u043d\u0446\u0443\u0437\u0441\u043a\u0438\u0445 \u0431\u0443\u043b\u043e\u043a, \u0434\u0430 \u0432\u044b\u043f\u0435\u0439 \u0447\u0430\u044e.\u201d \u0418 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043b\u044e\u0447\u0430 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0441\u043b\u043e\u0432\u043e \u201c\u0445\u0430\u0431\u0440\u0430\u201d:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/6c7\/cc5\/8dc\/6c7cc58dc6b43307dad4a24a76ae0bfd.png\"\/><\/p>\n<p>  \u0423\u0437\u043a\u0438\u043c \u043c\u0435\u0441\u0442\u043e\u043c \u0442\u0430\u043a\u043e\u0433\u043e \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e, \u0447\u0442\u043e \u0437\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u0441 \u043b\u0435\u0433\u043a\u043e\u0441\u0442\u044c\u044e \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043a\u043b\u044e\u0447 \u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0440\u0430\u0437\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0435\u0441\u044c \u0442\u0435\u043a\u0441\u0442. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0447\u0438\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435 \u043e\u043d \u0440\u0435\u0434\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435, \u0445\u043e\u0442\u044f \u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442 \u043a\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<br \/>  \u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u0447\u0442\u043e \u0432 \u0441\u0432\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0434\u0430\u043d\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f Microsoft \u0434\u043b\u044f \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 Office 95.<\/p>\n<h4>\u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b XORShift<\/h4>\n<p>  \u0412 2003 \u0433\u043e\u0434\u0443 <a href=\"http:\/\/en.wikipedia.org\/wiki\/George_Marsaglia\">\u0414\u0436\u043e\u0440\u0434\u0436 \u041c\u0430\u0440\u0441\u0430\u0433\u043b\u0438\u044f<\/a> \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043b \u043c\u0438\u0440\u0443 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c XOR \u2013 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Xorshift\">XORShift<\/a>.<\/p>\n<p>  \u041e\u0434\u043d\u0430 \u0438\u0437 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0435\u0433\u043e \u0440\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439:  <\/p>\n<pre><code class=\"java\">class XORShift {  \tprivate long rnd;  \tpublic XORShift(long rnd) { \t\tthis.rnd = rnd; \t}  \tpublic long getRandom() { \t\tthis.rnd ^= (this.rnd &lt;&lt; 21);  \t\tthis.rnd ^= (this.rnd &gt;&gt;&gt; 35);  \t\tthis.rnd ^= (this.rnd &lt;&lt; 4);  \t\treturn this.rnd; \t} } <\/code><\/pre>\n<p>  \u0422\u0443\u0442 \u201c\u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435\u201c \u0447\u0438\u0441\u043b\u0430 21, 35 \u0438 4 \u043f\u043e\u0434\u043e\u0431\u0440\u0430\u043d\u044b \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043b\u0443\u0447\u0448\u0435\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 (\u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0438\u043e\u0434 \u0440\u0430\u0432\u0435\u043d 2<sup>64<\/sup>-1).<br \/>  \u041f\u0440\u043e\u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0432 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0447\u0438\u0441\u043b\u043e\u043c 1111111111 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0442\u0430\u043a\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u043f\u0435\u0440\u0432\u044b\u0445 10 \u0447\u0438\u0441\u0435\u043b:<\/p>\n<p>  39462749392662495<br \/>  4596835458788324745<br \/>  -7932128052244037525<br \/>  -2502212788642280052<br \/>  3288035714308340525<br \/>  -8561046377475020727<br \/>  -812160615072319265<br \/>  -3869866974339671508<br \/>  -7329504029400927428<br \/>  3890915262874757420<\/p>\n<p>  \u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u0441\u044c\u0431\u0430 \u0442\u0435\u043c, \u0443 \u043a\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u0435 \u043a\u0440\u0430\u0441\u0438\u0432\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f XOR, \u043d\u0435 \u0432\u043e\u0448\u0435\u0434\u0448\u0438\u0435 \u0432 \u0441\u0442\u0430\u0442\u044c\u044e, \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u043d\u0438\u0445. \t\t\t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/183462\/\"> http:\/\/habrahabr.ru\/post\/183462\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\"> \t\t\t\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e \u0431\u0438\u0442\u043e\u0432\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 XOR (\u0438\u0441\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0435\u0435 \u0418\u041b\u0418) \u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0435\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043d\u0430 JAVA.<\/p>\n<p>  \u0418 \u0442\u0430\u043a, XOR \u2013 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u00ab\u0438\u0441\u0442\u0438\u043d\u0430\u00bb \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u00ab\u0438\u0441\u0442\u0438\u043d\u0430\u00bb.<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/9af\/1de\/e09\/9af1dee09d4d36ff0b15bdb4aae19e3b.png\" alt=\"image\"\/><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-183462","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/183462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=183462"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/183462\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=183462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=183462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=183462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}