What is the name for storing / packing many boolean states into one number?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
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.
terminology bit
New contributor
user568458 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
 |Â
show 4 more comments
up vote
2
down vote
favorite
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.
terminology bit
New contributor
user568458 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Fastest ever driveby-downvote, in less time than it could take to actually read the question. New record.
– user568458
4 hours ago
In C#, there areenums
, and they can have aFlags
attribute. They could make your code far simpler.
– Bernhard Hiller
4 hours ago
Yeah and usually you could simply turn an ordered array of binary states into a base 2 number then store or transmit that as base 64 or whatever is best for the application. The sample code above is intended to clearly illustrate the concept, not be optimised code.
– user568458
4 hours ago
I suppose referring to this as 'enumeration' would be clear enough for most. But this seems like the sort of basic technique that probably has a long history and a proper name.
– user568458
4 hours ago
I'd call this "simulating bit fields". It is almost always a bad idea unless space efficiency is overwhelmingly important.
– Kilian Foth
4 hours ago
 |Â
show 4 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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.
terminology bit
New contributor
user568458 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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.
terminology bit
terminology bit
New contributor
user568458 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user568458 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 hours ago


Glorfindel
8282919
8282919
New contributor
user568458 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 4 hours ago


user568458
1167
1167
New contributor
user568458 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user568458 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
user568458 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Fastest ever driveby-downvote, in less time than it could take to actually read the question. New record.
– user568458
4 hours ago
In C#, there areenums
, and they can have aFlags
attribute. They could make your code far simpler.
– Bernhard Hiller
4 hours ago
Yeah and usually you could simply turn an ordered array of binary states into a base 2 number then store or transmit that as base 64 or whatever is best for the application. The sample code above is intended to clearly illustrate the concept, not be optimised code.
– user568458
4 hours ago
I suppose referring to this as 'enumeration' would be clear enough for most. But this seems like the sort of basic technique that probably has a long history and a proper name.
– user568458
4 hours ago
I'd call this "simulating bit fields". It is almost always a bad idea unless space efficiency is overwhelmingly important.
– Kilian Foth
4 hours ago
 |Â
show 4 more comments
Fastest ever driveby-downvote, in less time than it could take to actually read the question. New record.
– user568458
4 hours ago
In C#, there areenums
, and they can have aFlags
attribute. They could make your code far simpler.
– Bernhard Hiller
4 hours ago
Yeah and usually you could simply turn an ordered array of binary states into a base 2 number then store or transmit that as base 64 or whatever is best for the application. The sample code above is intended to clearly illustrate the concept, not be optimised code.
– user568458
4 hours ago
I suppose referring to this as 'enumeration' would be clear enough for most. But this seems like the sort of basic technique that probably has a long history and a proper name.
– user568458
4 hours ago
I'd call this "simulating bit fields". It is almost always a bad idea unless space efficiency is overwhelmingly important.
– Kilian Foth
4 hours ago
Fastest ever driveby-downvote, in less time than it could take to actually read the question. New record.
– user568458
4 hours ago
Fastest ever driveby-downvote, in less time than it could take to actually read the question. New record.
– user568458
4 hours ago
In C#, there are
enums
, and they can have a Flags
attribute. They could make your code far simpler.– Bernhard Hiller
4 hours ago
In C#, there are
enums
, and they can have a Flags
attribute. They could make your code far simpler.– Bernhard Hiller
4 hours ago
Yeah and usually you could simply turn an ordered array of binary states into a base 2 number then store or transmit that as base 64 or whatever is best for the application. The sample code above is intended to clearly illustrate the concept, not be optimised code.
– user568458
4 hours ago
Yeah and usually you could simply turn an ordered array of binary states into a base 2 number then store or transmit that as base 64 or whatever is best for the application. The sample code above is intended to clearly illustrate the concept, not be optimised code.
– user568458
4 hours ago
I suppose referring to this as 'enumeration' would be clear enough for most. But this seems like the sort of basic technique that probably has a long history and a proper name.
– user568458
4 hours ago
I suppose referring to this as 'enumeration' would be clear enough for most. But this seems like the sort of basic technique that probably has a long history and a proper name.
– user568458
4 hours ago
I'd call this "simulating bit fields". It is almost always a bad idea unless space efficiency is overwhelmingly important.
– Kilian Foth
4 hours ago
I'd call this "simulating bit fields". It is almost always a bad idea unless space efficiency is overwhelmingly important.
– Kilian Foth
4 hours ago
 |Â
show 4 more comments
1 Answer
1
active
oldest
votes
up vote
7
down vote
accepted
It's most commonly referred to as a bit field, and another term you'll often hear is bit masks, which are used to get or set individual bit values or the entire bit field at once.
Many programming languages have auxiliary structures to help with this. As @BernhardHiller notes in the comments, C# has enums with flags; Java has the BitSet class.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
It's most commonly referred to as a bit field, and another term you'll often hear is bit masks, which are used to get or set individual bit values or the entire bit field at once.
Many programming languages have auxiliary structures to help with this. As @BernhardHiller notes in the comments, C# has enums with flags; Java has the BitSet class.
add a comment |Â
up vote
7
down vote
accepted
It's most commonly referred to as a bit field, and another term you'll often hear is bit masks, which are used to get or set individual bit values or the entire bit field at once.
Many programming languages have auxiliary structures to help with this. As @BernhardHiller notes in the comments, C# has enums with flags; Java has the BitSet class.
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
It's most commonly referred to as a bit field, and another term you'll often hear is bit masks, which are used to get or set individual bit values or the entire bit field at once.
Many programming languages have auxiliary structures to help with this. As @BernhardHiller notes in the comments, C# has enums with flags; Java has the BitSet class.
It's most commonly referred to as a bit field, and another term you'll often hear is bit masks, which are used to get or set individual bit values or the entire bit field at once.
Many programming languages have auxiliary structures to help with this. As @BernhardHiller notes in the comments, C# has enums with flags; Java has the BitSet class.
answered 4 hours ago


Glorfindel
8282919
8282919
add a comment |Â
add a comment |Â
user568458 is a new contributor. Be nice, and check out our Code of Conduct.
user568458 is a new contributor. Be nice, and check out our Code of Conduct.
user568458 is a new contributor. Be nice, and check out our Code of Conduct.
user568458 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f380034%2fwhat-is-the-name-for-storing-packing-many-boolean-states-into-one-number%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Fastest ever driveby-downvote, in less time than it could take to actually read the question. New record.
– user568458
4 hours ago
In C#, there are
enums
, and they can have aFlags
attribute. They could make your code far simpler.– Bernhard Hiller
4 hours ago
Yeah and usually you could simply turn an ordered array of binary states into a base 2 number then store or transmit that as base 64 or whatever is best for the application. The sample code above is intended to clearly illustrate the concept, not be optimised code.
– user568458
4 hours ago
I suppose referring to this as 'enumeration' would be clear enough for most. But this seems like the sort of basic technique that probably has a long history and a proper name.
– user568458
4 hours ago
I'd call this "simulating bit fields". It is almost always a bad idea unless space efficiency is overwhelmingly important.
– Kilian Foth
4 hours ago