How does this print “hello world”?

后端 未结 9 1371
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:09

    Adding some value to above answers. Following groovy script prints intermediate values.

    String getBits(long l) {
    return Long.toBinaryString(l).padLeft(8,'0');
    }
    
    for (long l = 4946144450195624l; l > 0; l >>= 5){
        println ''
        print String.valueOf(l).toString().padLeft(16,'0')
        print '|'+ getBits((l & 31 ))
        print '|'+ getBits(((l & 31 | 64)))
        print '|'+ getBits(((l & 31 | 64)  % 95))
        print '|'+ getBits(((l & 31 | 64)  % 95 + 32))
    
        print '|';
        System.out.print((char) (((l & 31 | 64) % 95) + 32));
    }
    

    Here it is

    4946144450195624|00001000|01001000|01001000|01101000|h
    0154567014068613|00000101|01000101|01000101|01100101|e
    0004830219189644|00001100|01001100|01001100|01101100|l
    0000150944349676|00001100|01001100|01001100|01101100|l
    0000004717010927|00001111|01001111|01001111|01101111|o
    0000000147406591|00011111|01011111|00000000|00100000| 
    0000000004606455|00010111|01010111|01010111|01110111|w
    0000000000143951|00001111|01001111|01001111|01101111|o
    0000000000004498|00010010|01010010|01010010|01110010|r
    0000000000000140|00001100|01001100|01001100|01101100|l
    0000000000000004|00000100|01000100|01000100|01100100|d
    

提交回复
热议问题