TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How to save multiple numbers into one byte

13 pointsby seycombiover 8 years ago

13 comments

new299over 8 years ago
I&#x27;m a bit shocked to see this on the front page. That&#x27;s possibly a comment on me as much as anything.<p>But this is just really simple bit packing. Is this something many engineers (maybe frontend&#x2F;JS) don&#x27;t know about now?
评论 #13093595 未加载
评论 #13093566 未加载
评论 #13093802 未加载
评论 #13093787 未加载
评论 #13093740 未加载
bitcharmerover 8 years ago
This is simple binary packing approach to storing small-type data in wider types. Delta compression is a natural next step from here. I once published a blog post on that topic: <a href="http:&#x2F;&#x2F;bitcharmer.blogspot.co.uk&#x2F;2013&#x2F;12&#x2F;how-to-serialise-array-of-doubles-with.html" rel="nofollow">http:&#x2F;&#x2F;bitcharmer.blogspot.co.uk&#x2F;2013&#x2F;12&#x2F;how-to-serialise-ar...</a><p>Mind you, the reference implementation from the blog is really poor in terms of code quality and performance because I was not allowed to open-source what I actually developed for my client, but the ideas still stand. Delta compression can make a huge difference in some cases; because cpu cycles are cheap and bitwise operations are very fast it has the potential to bring serious benefits in some scenarios, ie. lower latency of transmitting (numeric) data over networks.
BurningFrogover 8 years ago
Since 3⁵ (243) is less than 256, you can actually fit 5 &quot;trinary&quot; numbers into a byte.<p>The code gets more complex and slow, but not really by much.
评论 #13093874 未加载
评论 #13093869 未加载
评论 #13093583 未加载
gefhover 8 years ago
Alternatively, just store it naively and gzip. Or use a sparse array. Or, best, don&#x27;t do anything until storage size is an actual, measured bottleneck
pjscottover 8 years ago
See also bitfields, which make the compiler generate the shifting and masking code for you:<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bit_field" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bit_field</a>
评论 #13093409 未加载
Walkmanover 8 years ago
Either I don&#x27;t understand something or the Resulting bytes after c should be &#x27;11100100&#x27; and after d should be &#x27;11100110&#x27;.
评论 #13094778 未加载
smaddoxover 8 years ago
And if you need to store values that are not powers of 2, you can use the following kind of encoding&#x2F;decoding. In python3:<p><pre><code> from math import * def number_to_trits(n): # little-endian encoding # (i.e. smallest precision trit first) trits = [] v = n for i in range(1,9): v, d = divmod(v, 3**i) trits.append(d) return trits print(number_to_trits(8)) # [2, 2, 0, 0, 0, 0, 0, 0] def trits_to_number(trits): n = 0 for i, trit in enumerate(trits): n += trit*3**i return n print(trits_to_number([2,2])) # 8</code></pre>
mamispover 8 years ago
I also really liked this page on bitpacking, which also offers a technique to implement them using only math operations.<p><a href="http:&#x2F;&#x2F;number-none.com&#x2F;product&#x2F;Packing%20Integers&#x2F;index.html" rel="nofollow">http:&#x2F;&#x2F;number-none.com&#x2F;product&#x2F;Packing%20Integers&#x2F;index.html</a><p>I recently implemented a bitpacker in a game VM that didn&#x27;t have bitwise operations, and the math-based version worked perfectly.
strikingover 8 years ago
Isn&#x27;t it possible to use SQLite or something for something like this? That would greatly simplify most of these tasks, making your dataset reachable through a fast declarative language.<p>As many games of Go as 100000 is, it&#x27;s not so big you couldn&#x27;t use a regular, untuned database.
评论 #13093737 未加载
bill45over 8 years ago
This method is only optimal if your numbers have ranges that fit nicely into powers of two. See here for optimal bit packing: <a href="https:&#x2F;&#x2F;codeplea.com&#x2F;optimal-bit-packing" rel="nofollow">https:&#x2F;&#x2F;codeplea.com&#x2F;optimal-bit-packing</a>
deutroniumover 8 years ago
Neat :)<p>I wrote some very slow code to set&#x2F;get the Nth bit in an array of bytes.<p>I was planning on using it for Chess bitboards - <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bitboard" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bitboard</a>, but it could be used for say 8 booleans to the byte.<p>unsigned char getbit(unsigned char * bits,unsigned long n){<p><pre><code> return (bits[n&#x2F;8] &amp; (unsigned char)pow(2,n%8)) &gt;&gt; n%8; </code></pre> }<p>void setbit(unsigned char * bits,unsigned long n, unsigned char val){<p><pre><code> bits[n&#x2F;8] = (bits[n&#x2F;8] &amp; ~(unsigned char)pow(2,n%8)) | ((unsigned char)pow(2,n%8) * val); }</code></pre>
评论 #13093465 未加载
amingilaniover 8 years ago
Great read, but you should really move the motivation up to the top before you start the explanation.
ChicagoDaveover 8 years ago
&quot;a lot&quot; is two words.
评论 #13093606 未加载