It's a sort of simple compression where you use one numeric variable to store many boolean / binary states, using doubling and the fact that every doubling number is 1 + the sum of all the previous ones.
I'm sure it must be an old, well-known technique, I'd like to know what it's called to refer to it properly. I've done several searches on every way I can think of to describe it, but found nothing beyond some blog articles where the article authors seem to have figured this out themselves and don't know what to call it, either (example 1, example 2).
For example, here's a very simple implementation intended to illustrate the concept:
packStatesIntoNumber () {
let num = 0
if (this.stateA) num += 1
if (this.stateB) num += 2
if (this.stateC) num += 4
if (this.stateD) num += 8
if (this.stateE) num += 16
if (this.stateF) num += 32
return num
}
unpackStatesFromNumber (num) {
assert(num < 64)
this.stateF = num >= 32; if (this.stateF) num -= 32
this.stateE = num >= 16; if (this.stateE) num -= 16
this.stateD = num >= 8; if (this.stateD) num -= 8
this.stateC = num >= 4; if (this.stateC) num -= 4
this.stateB = num >= 2; if (this.stateB) num -= 2
this.stateA = num >= 1; if (this.stateA) num -= 1
}
You could also use bitwise operators, base 2 number parsing, enums... There are many more efficient ways to implement it, I'm interested in the name of the approach more generally.
enums
, and they can have aFlags
attribute. They could make your code far simpler. – Bernhard Hiller Oct 15 '18 at 08:58state |= (0x1 << 1); state |= (0x1 << 2);
etc. Ultimately it amounts to the same, but uses bit arithmetic and hex representation instead. – Neil Oct 15 '18 at 10:33stateA=1&stateB=0&stateC=...
tostates=encodedValue
). Yeah, it'd be crazy to just do this internally. – user56reinstatemonica8 Oct 15 '18 at 11:04bool
is generally stored as a 32 bit integer internally. As such, packing can make the difference of a factor of 32. That's really a lot. I mean, we programmers are always ready to throw away half of our resources, but I'm generally reluctant to throw away 97% of them. Such waste factors can easily make the difference between being able to run important use cases and running out of memory. – cmaster - reinstate monica Oct 15 '18 at 14:19bool
is just a heritage of C. Before C99, the language did not have any boolean type, boolean values were just stored asint
. Add to that the implicit promotion of smaller integer types toint
, and any "bool" is 32 bits... – cmaster - reinstate monica Oct 15 '18 at 20:48Int32
and fourBoolean
will always be 24 bytes is "easier" than saying that it could be anywhere from 12 to 24 depending upon the order in which the members are stored. – supercat Oct 15 '18 at 21:18bool
. I fully agree that bit-fiddling is cumbersome and impacts performance. But storing onebool
per byte does not. From a performance perspective, a 32 bitbool
type just does not make sense, and a 1 bitbool
type doesn't either. The sensible compromise is the 8 bitbool
type, but I don't know which languages actually implement it. – cmaster - reinstate monica Oct 15 '18 at 21:39