How does this print “hello world”?

后端 未结 9 1330
Happy的楠姐
Happy的楠姐 2021-01-29 17:33

I discovered this oddity:

for (long l = 4946144450195624l; l > 0; l >>= 5)
    System.out.print((char) (((l & 31 | 64) % 95) + 32));
相关标签:
9条回答
  • 2021-01-29 18:17

    Interesting!

    Standard ASCII characters which are visible are in range of 32 to 127.

    That's why you see 32, and 95 (127 - 32) there.

    In fact each character is mapped to 5 bits here, (you can find what is 5 bit combination for each character), and then all bits are concatenated to form a large number.

    Positive longs are 63 bit numbers, large enough to hold encrypted form of 12 characters. So it is large enough to hold Hello word, but for larger texts you shall use larger numbers, or even a BigInteger.


    In an application we wanted to transfer visible English Characters, Persian Characters and Symbols via SMS. As you see there are 32 (number of Persian chars) + 95 (number of English characters and standard visible symbols) = 127 possible values, which can be represented with 7 bits.

    We converted each UTF-8 (16 bit) character to 7 bits, and gain more than 56% compression ratio. So we could send texts with twice length in the same number of SMSs. (It is somehow the same thing happened here).

    0 讨论(0)
  • 2021-01-29 18:18

    out.println((char) (((l & 31 | 64) % 95) + 32 / 1002439 * 1002439));

    To make it caps :3

    0 讨论(0)
  • 2021-01-29 18:20

    It prints "hello world" for a similar reason this does:

    for (int k=1587463874; k>0; k>>=3)
         System.out.print((char) (100 + Math.pow(2,2*(((k&7^1)-1)>>3 + 1) + (k&7&3)) + 10*((k&7)>>2) + (((k&7)-7)>>3) + 1 - ((-(k&7^5)>>3) + 1)*80));
    

    but for a somewhat different reason than this:

    for (int k=2011378; k>0; k>>=2)
        System.out.print((char) (110 + Math.pow(2,2*(((k^1)-1)>>21 + 1) + (k&3)) - ((k&8192)/8192 + 7.9*(-(k^1964)>>21) - .1*(-((k&35)^35)>>21) + .3*(-((k&120)^120)>>21) + (-((k|7)^7)>>21) + 9.1)*10));
    
    0 讨论(0)
提交回复
热议问题