What is the name for storing / packing many boolean states into one number?

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question









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 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











  • 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














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.










share|improve this question









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 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











  • 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












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.










share|improve this question









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






share|improve this question









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.











share|improve this question









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.









share|improve this question




share|improve this question








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 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











  • 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











  • 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











  • 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










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.






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "131"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    user568458 is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer
























      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.






      share|improve this answer






















        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 4 hours ago









        Glorfindel

        8282919




        8282919




















            user568458 is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            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.













             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery