Grabbing 'n' binary bits from end of unsigned int in C? Bit masking?












-1















I'm pretty new to C and I seem to be messing up my bitmasking. From what I understand, it's a way to grab or create something of a subset from a binary value.



Say I want to grab the last 8 bits of an unsigned int value, containing 00001111000011110000111100001111.



How would I use AND/OR to grab those last 8?










share|improve this question


















  • 1





    what have you tried so far ? where are your C code attemtps ?

    – LoneWanderer
    Nov 19 '18 at 17:43






  • 1





    Possible duplicate of Get last n bits of binary

    – Govind Parmar
    Nov 19 '18 at 17:52
















-1















I'm pretty new to C and I seem to be messing up my bitmasking. From what I understand, it's a way to grab or create something of a subset from a binary value.



Say I want to grab the last 8 bits of an unsigned int value, containing 00001111000011110000111100001111.



How would I use AND/OR to grab those last 8?










share|improve this question


















  • 1





    what have you tried so far ? where are your C code attemtps ?

    – LoneWanderer
    Nov 19 '18 at 17:43






  • 1





    Possible duplicate of Get last n bits of binary

    – Govind Parmar
    Nov 19 '18 at 17:52














-1












-1








-1








I'm pretty new to C and I seem to be messing up my bitmasking. From what I understand, it's a way to grab or create something of a subset from a binary value.



Say I want to grab the last 8 bits of an unsigned int value, containing 00001111000011110000111100001111.



How would I use AND/OR to grab those last 8?










share|improve this question














I'm pretty new to C and I seem to be messing up my bitmasking. From what I understand, it's a way to grab or create something of a subset from a binary value.



Say I want to grab the last 8 bits of an unsigned int value, containing 00001111000011110000111100001111.



How would I use AND/OR to grab those last 8?







c binary hex bit bitmask






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 17:37









RadiantBytesRadiantBytes

44




44








  • 1





    what have you tried so far ? where are your C code attemtps ?

    – LoneWanderer
    Nov 19 '18 at 17:43






  • 1





    Possible duplicate of Get last n bits of binary

    – Govind Parmar
    Nov 19 '18 at 17:52














  • 1





    what have you tried so far ? where are your C code attemtps ?

    – LoneWanderer
    Nov 19 '18 at 17:43






  • 1





    Possible duplicate of Get last n bits of binary

    – Govind Parmar
    Nov 19 '18 at 17:52








1




1





what have you tried so far ? where are your C code attemtps ?

– LoneWanderer
Nov 19 '18 at 17:43





what have you tried so far ? where are your C code attemtps ?

– LoneWanderer
Nov 19 '18 at 17:43




1




1





Possible duplicate of Get last n bits of binary

– Govind Parmar
Nov 19 '18 at 17:52





Possible duplicate of Get last n bits of binary

– Govind Parmar
Nov 19 '18 at 17:52












4 Answers
4






active

oldest

votes


















2














Here's a more general solution to generate the mask based on how many bits you're interested int.



unsigned int last_n_bits(unsigned int value, int n)
{
unsigned int mask = -1;
if (n < sizeof(unsigned) * CHAR_BIT)
mask = ((1<<n)-1);
return value & mask;
}





share|improve this answer


























  • you should assert that n < sizeof(unsigned) * CHAR_BIT

    – Swordfish
    Nov 19 '18 at 17:55











  • and then you've got a problem getting all bits.

    – Swordfish
    Nov 19 '18 at 18:03











  • @Swordfish good valid points.

    – cleblanc
    Nov 19 '18 at 18:11











  • The width of unsigned might technically be less than sizeof(unsigned)*CHAR_BIT as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.

    – PSkocik
    Nov 19 '18 at 18:28



















0














You can use the BINARY AND operator for do a binary mask:



unsigned char last_eight_bits = my_number & 0b11111111





share|improve this answer



















  • 2





    No need for the bit operation if the target is a char (or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.

    – Swordfish
    Nov 19 '18 at 17:45





















0














Mask-out all bits > 0xff:



value & 0xffu





share|improve this answer
























  • Or create a mask using (1 << 8) - 1 where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).

    – Maarten Bodewes
    Nov 19 '18 at 17:50



















0















Say I want to grab the last 8 bits of an unsigned int value, containing
00001111000011110000111100001111.



How would I use AND/OR to grab those last 8?




In this case, you would use AND. The following code will grab the 8 least significant bits:



unsigned int number = 0x0F0F0F0Fu;
unsigned int mask = 0x000000FFu; // set all bits to "1" which you want to grab
unsigned int result = number & mask; // result will be 0x0000000F


The &-Operator is used for an AND-Operation with each bit:



    00001111000011110000111100001111
AND 00000000000000000000000011111111
------------------------------------
00000000000000000000000000001111


Be aware that "0 AND X = 0" and that "1 AND X = X". You can do further investigation at: https://en.wikipedia.org/wiki/Boolean_algebra.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379951%2fgrabbing-n-binary-bits-from-end-of-unsigned-int-in-c-bit-masking%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Here's a more general solution to generate the mask based on how many bits you're interested int.



    unsigned int last_n_bits(unsigned int value, int n)
    {
    unsigned int mask = -1;
    if (n < sizeof(unsigned) * CHAR_BIT)
    mask = ((1<<n)-1);
    return value & mask;
    }





    share|improve this answer


























    • you should assert that n < sizeof(unsigned) * CHAR_BIT

      – Swordfish
      Nov 19 '18 at 17:55











    • and then you've got a problem getting all bits.

      – Swordfish
      Nov 19 '18 at 18:03











    • @Swordfish good valid points.

      – cleblanc
      Nov 19 '18 at 18:11











    • The width of unsigned might technically be less than sizeof(unsigned)*CHAR_BIT as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.

      – PSkocik
      Nov 19 '18 at 18:28
















    2














    Here's a more general solution to generate the mask based on how many bits you're interested int.



    unsigned int last_n_bits(unsigned int value, int n)
    {
    unsigned int mask = -1;
    if (n < sizeof(unsigned) * CHAR_BIT)
    mask = ((1<<n)-1);
    return value & mask;
    }





    share|improve this answer


























    • you should assert that n < sizeof(unsigned) * CHAR_BIT

      – Swordfish
      Nov 19 '18 at 17:55











    • and then you've got a problem getting all bits.

      – Swordfish
      Nov 19 '18 at 18:03











    • @Swordfish good valid points.

      – cleblanc
      Nov 19 '18 at 18:11











    • The width of unsigned might technically be less than sizeof(unsigned)*CHAR_BIT as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.

      – PSkocik
      Nov 19 '18 at 18:28














    2












    2








    2







    Here's a more general solution to generate the mask based on how many bits you're interested int.



    unsigned int last_n_bits(unsigned int value, int n)
    {
    unsigned int mask = -1;
    if (n < sizeof(unsigned) * CHAR_BIT)
    mask = ((1<<n)-1);
    return value & mask;
    }





    share|improve this answer















    Here's a more general solution to generate the mask based on how many bits you're interested int.



    unsigned int last_n_bits(unsigned int value, int n)
    {
    unsigned int mask = -1;
    if (n < sizeof(unsigned) * CHAR_BIT)
    mask = ((1<<n)-1);
    return value & mask;
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 19 '18 at 18:09

























    answered Nov 19 '18 at 17:50









    cleblanccleblanc

    3,4611913




    3,4611913













    • you should assert that n < sizeof(unsigned) * CHAR_BIT

      – Swordfish
      Nov 19 '18 at 17:55











    • and then you've got a problem getting all bits.

      – Swordfish
      Nov 19 '18 at 18:03











    • @Swordfish good valid points.

      – cleblanc
      Nov 19 '18 at 18:11











    • The width of unsigned might technically be less than sizeof(unsigned)*CHAR_BIT as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.

      – PSkocik
      Nov 19 '18 at 18:28



















    • you should assert that n < sizeof(unsigned) * CHAR_BIT

      – Swordfish
      Nov 19 '18 at 17:55











    • and then you've got a problem getting all bits.

      – Swordfish
      Nov 19 '18 at 18:03











    • @Swordfish good valid points.

      – cleblanc
      Nov 19 '18 at 18:11











    • The width of unsigned might technically be less than sizeof(unsigned)*CHAR_BIT as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.

      – PSkocik
      Nov 19 '18 at 18:28

















    you should assert that n < sizeof(unsigned) * CHAR_BIT

    – Swordfish
    Nov 19 '18 at 17:55





    you should assert that n < sizeof(unsigned) * CHAR_BIT

    – Swordfish
    Nov 19 '18 at 17:55













    and then you've got a problem getting all bits.

    – Swordfish
    Nov 19 '18 at 18:03





    and then you've got a problem getting all bits.

    – Swordfish
    Nov 19 '18 at 18:03













    @Swordfish good valid points.

    – cleblanc
    Nov 19 '18 at 18:11





    @Swordfish good valid points.

    – cleblanc
    Nov 19 '18 at 18:11













    The width of unsigned might technically be less than sizeof(unsigned)*CHAR_BIT as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.

    – PSkocik
    Nov 19 '18 at 18:28





    The width of unsigned might technically be less than sizeof(unsigned)*CHAR_BIT as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.

    – PSkocik
    Nov 19 '18 at 18:28













    0














    You can use the BINARY AND operator for do a binary mask:



    unsigned char last_eight_bits = my_number & 0b11111111





    share|improve this answer



















    • 2





      No need for the bit operation if the target is a char (or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.

      – Swordfish
      Nov 19 '18 at 17:45


















    0














    You can use the BINARY AND operator for do a binary mask:



    unsigned char last_eight_bits = my_number & 0b11111111





    share|improve this answer



















    • 2





      No need for the bit operation if the target is a char (or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.

      – Swordfish
      Nov 19 '18 at 17:45
















    0












    0








    0







    You can use the BINARY AND operator for do a binary mask:



    unsigned char last_eight_bits = my_number & 0b11111111





    share|improve this answer













    You can use the BINARY AND operator for do a binary mask:



    unsigned char last_eight_bits = my_number & 0b11111111






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 '18 at 17:43









    iEldeniElden

    684517




    684517








    • 2





      No need for the bit operation if the target is a char (or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.

      – Swordfish
      Nov 19 '18 at 17:45
















    • 2





      No need for the bit operation if the target is a char (or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.

      – Swordfish
      Nov 19 '18 at 17:45










    2




    2





    No need for the bit operation if the target is a char (or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.

    – Swordfish
    Nov 19 '18 at 17:45







    No need for the bit operation if the target is a char (or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.

    – Swordfish
    Nov 19 '18 at 17:45













    0














    Mask-out all bits > 0xff:



    value & 0xffu





    share|improve this answer
























    • Or create a mask using (1 << 8) - 1 where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).

      – Maarten Bodewes
      Nov 19 '18 at 17:50
















    0














    Mask-out all bits > 0xff:



    value & 0xffu





    share|improve this answer
























    • Or create a mask using (1 << 8) - 1 where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).

      – Maarten Bodewes
      Nov 19 '18 at 17:50














    0












    0








    0







    Mask-out all bits > 0xff:



    value & 0xffu





    share|improve this answer













    Mask-out all bits > 0xff:



    value & 0xffu






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 '18 at 17:44









    SwordfishSwordfish

    9,52711436




    9,52711436













    • Or create a mask using (1 << 8) - 1 where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).

      – Maarten Bodewes
      Nov 19 '18 at 17:50



















    • Or create a mask using (1 << 8) - 1 where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).

      – Maarten Bodewes
      Nov 19 '18 at 17:50

















    Or create a mask using (1 << 8) - 1 where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).

    – Maarten Bodewes
    Nov 19 '18 at 17:50





    Or create a mask using (1 << 8) - 1 where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).

    – Maarten Bodewes
    Nov 19 '18 at 17:50











    0















    Say I want to grab the last 8 bits of an unsigned int value, containing
    00001111000011110000111100001111.



    How would I use AND/OR to grab those last 8?




    In this case, you would use AND. The following code will grab the 8 least significant bits:



    unsigned int number = 0x0F0F0F0Fu;
    unsigned int mask = 0x000000FFu; // set all bits to "1" which you want to grab
    unsigned int result = number & mask; // result will be 0x0000000F


    The &-Operator is used for an AND-Operation with each bit:



        00001111000011110000111100001111
    AND 00000000000000000000000011111111
    ------------------------------------
    00000000000000000000000000001111


    Be aware that "0 AND X = 0" and that "1 AND X = X". You can do further investigation at: https://en.wikipedia.org/wiki/Boolean_algebra.






    share|improve this answer




























      0















      Say I want to grab the last 8 bits of an unsigned int value, containing
      00001111000011110000111100001111.



      How would I use AND/OR to grab those last 8?




      In this case, you would use AND. The following code will grab the 8 least significant bits:



      unsigned int number = 0x0F0F0F0Fu;
      unsigned int mask = 0x000000FFu; // set all bits to "1" which you want to grab
      unsigned int result = number & mask; // result will be 0x0000000F


      The &-Operator is used for an AND-Operation with each bit:



          00001111000011110000111100001111
      AND 00000000000000000000000011111111
      ------------------------------------
      00000000000000000000000000001111


      Be aware that "0 AND X = 0" and that "1 AND X = X". You can do further investigation at: https://en.wikipedia.org/wiki/Boolean_algebra.






      share|improve this answer


























        0












        0








        0








        Say I want to grab the last 8 bits of an unsigned int value, containing
        00001111000011110000111100001111.



        How would I use AND/OR to grab those last 8?




        In this case, you would use AND. The following code will grab the 8 least significant bits:



        unsigned int number = 0x0F0F0F0Fu;
        unsigned int mask = 0x000000FFu; // set all bits to "1" which you want to grab
        unsigned int result = number & mask; // result will be 0x0000000F


        The &-Operator is used for an AND-Operation with each bit:



            00001111000011110000111100001111
        AND 00000000000000000000000011111111
        ------------------------------------
        00000000000000000000000000001111


        Be aware that "0 AND X = 0" and that "1 AND X = X". You can do further investigation at: https://en.wikipedia.org/wiki/Boolean_algebra.






        share|improve this answer














        Say I want to grab the last 8 bits of an unsigned int value, containing
        00001111000011110000111100001111.



        How would I use AND/OR to grab those last 8?




        In this case, you would use AND. The following code will grab the 8 least significant bits:



        unsigned int number = 0x0F0F0F0Fu;
        unsigned int mask = 0x000000FFu; // set all bits to "1" which you want to grab
        unsigned int result = number & mask; // result will be 0x0000000F


        The &-Operator is used for an AND-Operation with each bit:



            00001111000011110000111100001111
        AND 00000000000000000000000011111111
        ------------------------------------
        00000000000000000000000000001111


        Be aware that "0 AND X = 0" and that "1 AND X = X". You can do further investigation at: https://en.wikipedia.org/wiki/Boolean_algebra.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 18:18









        GoldenArrows777GoldenArrows777

        311




        311






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379951%2fgrabbing-n-binary-bits-from-end-of-unsigned-int-in-c-bit-masking%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()