Live data from Hacker News

Show HN: How to store a set of four 5-bit values in one 16-bit value

github.com

31–40 of 149 posts

Re: Show HN: How to store a set of four 5-bit values in one 16-bit value

#31

This is quite related to the problem of sorting a million 32 bit integers using only 2M of RAM (and no disk). It can be done.

If I only have 8 bytes of ram and a program counter I can do it... For (I=0; I Shoddy runtime, but hey...

Yes, this is a problem that requires careful specification. You only get streaming access to your input, not random.

Re: Show HN: How to store a set of four 5-bit values in one 16-bit value

#32
post #6

Earlier quoted context omitted.

To answer the obvious next question: Four 5-bit values in order have 20 bits of entropy, so cannot be stored in 16 bits. Four 5-bit values without order have 20 - log2(4!) =~ 20 - 4.59 = 15.41 bits of entropy (corresponding to log(2^20/4!) possible configurations), and thus can fit in 16 bits of data if you're clever about it.

Your math is wrong. If two of the numbers happen to be the same, order no longer matters for those numbers, so your log2(4!) needs to be larger...

Right. So the true answer is

    log2((32*31*30*29)/(4*3*2) + (32*31*30)/2 + (32*31) + (32*31)/2) = 15.675

 which is still smaller than 16.

Re: Show HN: How to store a set of four 5-bit values in one 16-bit value

#33

Earlier quoted context omitted.

If I only have 8 bytes of ram and a program counter I can do it... For (I=0; I Shoddy runtime, but hey...

Yes, this is a problem that requires careful specification. You only get streaming access to your input, not random.

Couldn't you just do that with radix sort or am I missing something?

Re: Show HN: How to store a set of four 5-bit values in one 16-bit value

#34

Earlier quoted context omitted.

Yes, this is a problem that requires careful specification. You only get streaming access to your input, not random.

Couldn't you just do that with radix sort or am I missing something?

Yes, you're missing something. Radix sort, like most sorting algorithms, requires storing the intermediate results in memory. This problem seems impossible at first blush because storing 1 million 4-byte integers would seem to require 4M of RAM, and only 2M is available.

Re: Show HN: How to store a set of four 5-bit values in one 16-bit value

#36

That is one big-ass makefile though. I realize it has some extra niceties but I have to ask, did you ever try make main without any makefile at all? If you haven't done so, delete the makefile now (you have it in version control anyway) and give it a try.

Is there a way to make it work with just make? I'm way too used to just typing make at this point and it's not too much work to just copy the same makefile everywhere.

Re: Show HN: How to store a set of four 5-bit values in one 16-bit value

#37
It is possible to do this in O(1) memory, for arbitrary sized collections, efficiently.

The first trick is to make a function that can calculate the nth set with k elements from some universe U (in this case U = {0..2^5-1} without order (no duplicates) directly. This is done using the https://en.wikipedia.org/wiki/Combinatorial_number_system. This is very efficient.

Then, to encode duplicates you use the stars and bars trick.

Re: Show HN: How to store a set of four 5-bit values in one 16-bit value

#38

This is quite related to the problem of sorting a million 32 bit integers using only 2M of RAM (and no disk). It can be done.

A list of deltas should do it. In the worst case, the deltas would use log2[2^32/1000000] * 1000000 bits, so about 1.5 MB. Plus some space because of base 128 encoding (it increases size up to 37/32, rounded up per byte).

I got a worst case of exactly 2 MB (1.907 MiB) (all deltas being 4294, so the list is 0, 4294, 8588...), but maybe it's possible to get better than that.

It would be uber slow though, probably n^2.

Re: Show HN: How to store a set of four 5-bit values in one 16-bit value

#39
post #35

This is quite related to the problem of sorting a million 32 bit integers using only 2M of RAM (and no disk). It can be done.

So what is the trick?

It's the opening problem from Programming Pearls: http://www.fusu.us/2013/06/bitmap-sort.html

(edit: better link)

Post reply on HN