Live data from Hacker News

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

github.com

91–100 of 149 posts

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

#91
post #55

What's the practical use for this?

Saving space in data structures that use buckets such as hash tables. I actually discovered this in a paper on hash tables that kind of just mentioned that it was a thing but I had a harder time finding out exactly how it was done.

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

#92

Earlier quoted context omitted.

Yes, the universe is expanded.

I really like this mechanism. It seems like it should be the optimal way to store a set of size k. Out of curiosity, do you know of an efficient way to calculate this without sorting first? Or even better, without knowing k ahead of time? If you can sort your input first it's trivial, but even though it seems like a super efficient way to store a set of numbers I don't know how to use it to solve, for example, the ot…

I don't know of a way of doing this without sorting or knowing k ahead of time, sorry.

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

#93
post #81

Why is this first line true: "This works because there are 3876 possible unique values for a set of 4 4 bit values" Each 4 bit number has 16 possible values. And you can order them in the set in 4! = 24 ways. So I thought you would get 24 * 16 = 384. Can someone explain?

They're taking the multiset [0].

So it's \binom{2^4 + 4 - 1}{4} = 3876

In other words, there are 3876 multisets of cardinality 4 with elements taken from the set containing all 4 bit values.

[0] https://en.wikipedia.org/wiki/Multiset

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

#94
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.

Is there a specific topic of study that taught you those formulas?

https://en.wikipedia.org/wiki/Information_theory

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

#95
post #55

What's the practical use for this?

Saving space in data structures that use buckets such as hash tables. I actually discovered this in a paper on hash tables that kind of just mentioned that it was a thing but I had a harder time finding out exactly how it was done.

Any chance you can remember which paper?

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

#97
For completeness: The number of possible sets that include 4 numbers from the range [0..15] is (16+4-1) choose 4, or 3876.

Let's assume that our set is (a, b, c, d). It is ordered, therefore a Consider all possible numbers that may be in our set, laid out in order.

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

For each element in the set, we mark its position in this order by placing a marker to the left of the corresponding number.

For our example, we first mark element 2:

0 1 m 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Then 4:

0 1 m 2 3 m 4 5 6 7 8 9 10 11 12 13 14 15

Then, 4 again:

0 1 m 2 3 m m 4 5 6 7 8 9 10 11 12 13 14 15

And finally, 10:

0 1 m 2 3 m m 4 5 6 7 8 9 m 10 11 12 13 14 15

Now, out of these 16+4 places, only 19 of them could be a valid placement for a marker, as a marker should always be placed to the left of a number (or said differently, "15" will always be in the end.)

Therefore, there are 19 choose 4 ways to pick the locations of the markers.

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

#99
post #44

how are you getting 3876 unique values for a set of 4 4 bit values (16, 16, 16, 16) ?

I don't understand as well...

(n + k - 1) choose k

n = number of unique 4-bit values

k = size of multiset

(2^4 + 4 - 1) choose 4 = 3876

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

#100
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...

Does this implementation allow duplicates? Typically a set is defined as an unordered collection with no duplicate items.
Post reply on HN