How to strip off left side of binary number in Python?












0















I got this binary number 101111111111000



I need to strip off the 8 most significant bits and have 11111000 at the end.



I tried to make 101111111111000 << 8, but this results in 10111111111100000000000, it hasn't the same effect as >> which strips the lower bits. So how can this be done? The final result MUST BE binary type.










share|improve this question


















  • 2





    What do you mean by "binary type"? Python has no such type. Would an integer suffice?

    – Rory Daulton
    Nov 25 '18 at 22:39











  • "it hasn't the same effect as >> which strips the lower bits" is because >> does not "strip", but it shifts the bits. So it does have the same effect as <<, in the other direction.

    – usr2564301
    Nov 25 '18 at 22:40











  • Integers are binary yeah, but I'am working in the binary level, that's why i'am approaching it with binaries.

    – Rodrigo Formighieri
    Nov 25 '18 at 22:41






  • 1





    @Alnitak It would be more like I need the 8 lower significant bits of any binary number, doesnt matter the word length.

    – Rodrigo Formighieri
    Nov 25 '18 at 22:52






  • 1





    @RodrigoFormighieri that's a completely different (but far easier) question to answer. Your question would have made more sense if you had described what you wanted to retain rather than what you want to throw away.

    – Alnitak
    Nov 25 '18 at 23:00
















0















I got this binary number 101111111111000



I need to strip off the 8 most significant bits and have 11111000 at the end.



I tried to make 101111111111000 << 8, but this results in 10111111111100000000000, it hasn't the same effect as >> which strips the lower bits. So how can this be done? The final result MUST BE binary type.










share|improve this question


















  • 2





    What do you mean by "binary type"? Python has no such type. Would an integer suffice?

    – Rory Daulton
    Nov 25 '18 at 22:39











  • "it hasn't the same effect as >> which strips the lower bits" is because >> does not "strip", but it shifts the bits. So it does have the same effect as <<, in the other direction.

    – usr2564301
    Nov 25 '18 at 22:40











  • Integers are binary yeah, but I'am working in the binary level, that's why i'am approaching it with binaries.

    – Rodrigo Formighieri
    Nov 25 '18 at 22:41






  • 1





    @Alnitak It would be more like I need the 8 lower significant bits of any binary number, doesnt matter the word length.

    – Rodrigo Formighieri
    Nov 25 '18 at 22:52






  • 1





    @RodrigoFormighieri that's a completely different (but far easier) question to answer. Your question would have made more sense if you had described what you wanted to retain rather than what you want to throw away.

    – Alnitak
    Nov 25 '18 at 23:00














0












0








0








I got this binary number 101111111111000



I need to strip off the 8 most significant bits and have 11111000 at the end.



I tried to make 101111111111000 << 8, but this results in 10111111111100000000000, it hasn't the same effect as >> which strips the lower bits. So how can this be done? The final result MUST BE binary type.










share|improve this question














I got this binary number 101111111111000



I need to strip off the 8 most significant bits and have 11111000 at the end.



I tried to make 101111111111000 << 8, but this results in 10111111111100000000000, it hasn't the same effect as >> which strips the lower bits. So how can this be done? The final result MUST BE binary type.







python binary






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 22:35









Rodrigo FormighieriRodrigo Formighieri

1109




1109








  • 2





    What do you mean by "binary type"? Python has no such type. Would an integer suffice?

    – Rory Daulton
    Nov 25 '18 at 22:39











  • "it hasn't the same effect as >> which strips the lower bits" is because >> does not "strip", but it shifts the bits. So it does have the same effect as <<, in the other direction.

    – usr2564301
    Nov 25 '18 at 22:40











  • Integers are binary yeah, but I'am working in the binary level, that's why i'am approaching it with binaries.

    – Rodrigo Formighieri
    Nov 25 '18 at 22:41






  • 1





    @Alnitak It would be more like I need the 8 lower significant bits of any binary number, doesnt matter the word length.

    – Rodrigo Formighieri
    Nov 25 '18 at 22:52






  • 1





    @RodrigoFormighieri that's a completely different (but far easier) question to answer. Your question would have made more sense if you had described what you wanted to retain rather than what you want to throw away.

    – Alnitak
    Nov 25 '18 at 23:00














  • 2





    What do you mean by "binary type"? Python has no such type. Would an integer suffice?

    – Rory Daulton
    Nov 25 '18 at 22:39











  • "it hasn't the same effect as >> which strips the lower bits" is because >> does not "strip", but it shifts the bits. So it does have the same effect as <<, in the other direction.

    – usr2564301
    Nov 25 '18 at 22:40











  • Integers are binary yeah, but I'am working in the binary level, that's why i'am approaching it with binaries.

    – Rodrigo Formighieri
    Nov 25 '18 at 22:41






  • 1





    @Alnitak It would be more like I need the 8 lower significant bits of any binary number, doesnt matter the word length.

    – Rodrigo Formighieri
    Nov 25 '18 at 22:52






  • 1





    @RodrigoFormighieri that's a completely different (but far easier) question to answer. Your question would have made more sense if you had described what you wanted to retain rather than what you want to throw away.

    – Alnitak
    Nov 25 '18 at 23:00








2




2





What do you mean by "binary type"? Python has no such type. Would an integer suffice?

– Rory Daulton
Nov 25 '18 at 22:39





What do you mean by "binary type"? Python has no such type. Would an integer suffice?

– Rory Daulton
Nov 25 '18 at 22:39













"it hasn't the same effect as >> which strips the lower bits" is because >> does not "strip", but it shifts the bits. So it does have the same effect as <<, in the other direction.

– usr2564301
Nov 25 '18 at 22:40





"it hasn't the same effect as >> which strips the lower bits" is because >> does not "strip", but it shifts the bits. So it does have the same effect as <<, in the other direction.

– usr2564301
Nov 25 '18 at 22:40













Integers are binary yeah, but I'am working in the binary level, that's why i'am approaching it with binaries.

– Rodrigo Formighieri
Nov 25 '18 at 22:41





Integers are binary yeah, but I'am working in the binary level, that's why i'am approaching it with binaries.

– Rodrigo Formighieri
Nov 25 '18 at 22:41




1




1





@Alnitak It would be more like I need the 8 lower significant bits of any binary number, doesnt matter the word length.

– Rodrigo Formighieri
Nov 25 '18 at 22:52





@Alnitak It would be more like I need the 8 lower significant bits of any binary number, doesnt matter the word length.

– Rodrigo Formighieri
Nov 25 '18 at 22:52




1




1





@RodrigoFormighieri that's a completely different (but far easier) question to answer. Your question would have made more sense if you had described what you wanted to retain rather than what you want to throw away.

– Alnitak
Nov 25 '18 at 23:00





@RodrigoFormighieri that's a completely different (but far easier) question to answer. Your question would have made more sense if you had described what you wanted to retain rather than what you want to throw away.

– Alnitak
Nov 25 '18 at 23:00












3 Answers
3






active

oldest

votes


















1














You can get the rightmost 8 bits of an integer n using bitwise-and:



n&255


255 is the sum of the values of the smallest 8 bits.



So, starting with 101111111111000, which is 24568 decimal, n&255 gives 11111000, which is 248 decimal.






share|improve this answer


























  • this is only consistent with the OP's question if the input data had 16 bits to start with.

    – Alnitak
    Nov 25 '18 at 22:44











  • Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.

    – khelwood
    Nov 25 '18 at 22:49



















1














another general way to do this is:



n = int('101111111111000', 2) # 24568

n & (1 << int.bit_length(n) - 8) - 1


gives 120 (or 1111000)






share|improve this answer
























  • Also works! Thanks

    – Rodrigo Formighieri
    Nov 26 '18 at 12:43



















0














To achieve this for a number x with n digits, one can use this



x&(2**(len(bin(x))-2-8)-1)



-2 to strip 0b, -8 to strip leftmost



Simply said it ands your number with just enough 1s that the 8 leftmost bits are set to 0.






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%2f53472702%2fhow-to-strip-off-left-side-of-binary-number-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can get the rightmost 8 bits of an integer n using bitwise-and:



    n&255


    255 is the sum of the values of the smallest 8 bits.



    So, starting with 101111111111000, which is 24568 decimal, n&255 gives 11111000, which is 248 decimal.






    share|improve this answer


























    • this is only consistent with the OP's question if the input data had 16 bits to start with.

      – Alnitak
      Nov 25 '18 at 22:44











    • Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.

      – khelwood
      Nov 25 '18 at 22:49
















    1














    You can get the rightmost 8 bits of an integer n using bitwise-and:



    n&255


    255 is the sum of the values of the smallest 8 bits.



    So, starting with 101111111111000, which is 24568 decimal, n&255 gives 11111000, which is 248 decimal.






    share|improve this answer


























    • this is only consistent with the OP's question if the input data had 16 bits to start with.

      – Alnitak
      Nov 25 '18 at 22:44











    • Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.

      – khelwood
      Nov 25 '18 at 22:49














    1












    1








    1







    You can get the rightmost 8 bits of an integer n using bitwise-and:



    n&255


    255 is the sum of the values of the smallest 8 bits.



    So, starting with 101111111111000, which is 24568 decimal, n&255 gives 11111000, which is 248 decimal.






    share|improve this answer















    You can get the rightmost 8 bits of an integer n using bitwise-and:



    n&255


    255 is the sum of the values of the smallest 8 bits.



    So, starting with 101111111111000, which is 24568 decimal, n&255 gives 11111000, which is 248 decimal.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 25 '18 at 22:44

























    answered Nov 25 '18 at 22:38









    khelwoodkhelwood

    31.9k74465




    31.9k74465













    • this is only consistent with the OP's question if the input data had 16 bits to start with.

      – Alnitak
      Nov 25 '18 at 22:44











    • Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.

      – khelwood
      Nov 25 '18 at 22:49



















    • this is only consistent with the OP's question if the input data had 16 bits to start with.

      – Alnitak
      Nov 25 '18 at 22:44











    • Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.

      – khelwood
      Nov 25 '18 at 22:49

















    this is only consistent with the OP's question if the input data had 16 bits to start with.

    – Alnitak
    Nov 25 '18 at 22:44





    this is only consistent with the OP's question if the input data had 16 bits to start with.

    – Alnitak
    Nov 25 '18 at 22:44













    Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.

    – khelwood
    Nov 25 '18 at 22:49





    Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.

    – khelwood
    Nov 25 '18 at 22:49













    1














    another general way to do this is:



    n = int('101111111111000', 2) # 24568

    n & (1 << int.bit_length(n) - 8) - 1


    gives 120 (or 1111000)






    share|improve this answer
























    • Also works! Thanks

      – Rodrigo Formighieri
      Nov 26 '18 at 12:43
















    1














    another general way to do this is:



    n = int('101111111111000', 2) # 24568

    n & (1 << int.bit_length(n) - 8) - 1


    gives 120 (or 1111000)






    share|improve this answer
























    • Also works! Thanks

      – Rodrigo Formighieri
      Nov 26 '18 at 12:43














    1












    1








    1







    another general way to do this is:



    n = int('101111111111000', 2) # 24568

    n & (1 << int.bit_length(n) - 8) - 1


    gives 120 (or 1111000)






    share|improve this answer













    another general way to do this is:



    n = int('101111111111000', 2) # 24568

    n & (1 << int.bit_length(n) - 8) - 1


    gives 120 (or 1111000)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 25 '18 at 23:18









    Sam MasonSam Mason

    3,35811331




    3,35811331













    • Also works! Thanks

      – Rodrigo Formighieri
      Nov 26 '18 at 12:43



















    • Also works! Thanks

      – Rodrigo Formighieri
      Nov 26 '18 at 12:43

















    Also works! Thanks

    – Rodrigo Formighieri
    Nov 26 '18 at 12:43





    Also works! Thanks

    – Rodrigo Formighieri
    Nov 26 '18 at 12:43











    0














    To achieve this for a number x with n digits, one can use this



    x&(2**(len(bin(x))-2-8)-1)



    -2 to strip 0b, -8 to strip leftmost



    Simply said it ands your number with just enough 1s that the 8 leftmost bits are set to 0.






    share|improve this answer




























      0














      To achieve this for a number x with n digits, one can use this



      x&(2**(len(bin(x))-2-8)-1)



      -2 to strip 0b, -8 to strip leftmost



      Simply said it ands your number with just enough 1s that the 8 leftmost bits are set to 0.






      share|improve this answer


























        0












        0








        0







        To achieve this for a number x with n digits, one can use this



        x&(2**(len(bin(x))-2-8)-1)



        -2 to strip 0b, -8 to strip leftmost



        Simply said it ands your number with just enough 1s that the 8 leftmost bits are set to 0.






        share|improve this answer













        To achieve this for a number x with n digits, one can use this



        x&(2**(len(bin(x))-2-8)-1)



        -2 to strip 0b, -8 to strip leftmost



        Simply said it ands your number with just enough 1s that the 8 leftmost bits are set to 0.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 23:05









        Jonathan RJonathan R

        1,182512




        1,182512






























            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%2f53472702%2fhow-to-strip-off-left-side-of-binary-number-in-python%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







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen