I discovered this oddity:
for (long l = 4946144450195624l; l > 0; l >>= 5)
System.out.print((char) (((l & 31 | 64) % 95) + 32));
Output:
hello world
How does this work?
I discovered this oddity:
for (long l = 4946144450195624l; l > 0; l >>= 5)
System.out.print((char) (((l & 31 | 64) % 95) + 32));
Output:
hello world
How does this work?
You've encoded characters as 5-bit values and packed 11 of them into a 64 bit long.
(packedValues >> 5*i) & 31
is the i-th encoded value with a range 0-31.The hard part, as you say, is encoding the space. The lower case english letters occupy the contiguous range 97-122 in Unicode (and ascii, and most other encodings), but the space is 32.
To overcome this, you used some arithmetic.
((x+64)%95)+32
is almost the same asx + 96
(note how bitwise OR is equivalent to addition, in this case), but when x=31, we get32
.out.println((char) (((l & 31 | 64) % 95) + 32 / 1002439 * 1002439));
To make it caps :3
Without an
Oracle
tag, it was difficult to see this question. Active bounty brought me here. I wish the question had other relevant technology tags too :-(I mostly work with
Oracle database
, so I would use someOracle
knowledge to interpret and explain :-)Let's convert the number
4946144450195624
intobinary
. For that I use a smallfunction
called dec2bin i.e. decimal-to-binary.Let's use the function to get the binary value -
Now the catch is the
5-bit
conversion. Start grouping from right to left with 5 digits in each group. We get :-We would be finally left with just 3 digits int he end at the right. Because, we had total 53 digits in the binary conversion.
hello world
total has 11 characters(including space), so we need to add 2 bits to the last group where we were left with just 3 bits after grouping.So, now we have :-
Now, we need to convert it to 7-bit ascii value. For the characters it is easy, we need to just set the 6th and 7th bit. Add
11
to each 5-bit group above to the left.That gives :-
Let's interpret the binary values, I will use
binary to decimal conversion function
.Let's look at each binary value -
Let's look at what characters they are :-
So, what do we get in the output?
d l r o w ⌂ o l l e h
That is hello⌂world in reverse. The only issue is the space. And the reason is well explained by @higuaro in his answer. I honestly couldn't interpret the space issue myself at first attempt, until I saw the explanation given in his answer.