What does “”“ if((counter & (1 < 0): ”“” do?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-2















I am trying to print all subsets of an array. I came across this if statement in on of the solutions.
What does this if condition do?



if((counter & (1 << j)) > 0):










share|improve this question























  • How do we know without knowing what is counter, i and j ? << is bitwise left shift operator doc.

    – Austin
    Nov 25 '18 at 5:43











  • It checks if bit number j in counter is set.

    – Michael Butscher
    Nov 25 '18 at 5:49











  • This was the program I was referrring to:geeksforgeeks.org/power-set

    – I am L
    Nov 25 '18 at 6:33


















-2















I am trying to print all subsets of an array. I came across this if statement in on of the solutions.
What does this if condition do?



if((counter & (1 << j)) > 0):










share|improve this question























  • How do we know without knowing what is counter, i and j ? << is bitwise left shift operator doc.

    – Austin
    Nov 25 '18 at 5:43











  • It checks if bit number j in counter is set.

    – Michael Butscher
    Nov 25 '18 at 5:49











  • This was the program I was referrring to:geeksforgeeks.org/power-set

    – I am L
    Nov 25 '18 at 6:33














-2












-2








-2








I am trying to print all subsets of an array. I came across this if statement in on of the solutions.
What does this if condition do?



if((counter & (1 << j)) > 0):










share|improve this question














I am trying to print all subsets of an array. I came across this if statement in on of the solutions.
What does this if condition do?



if((counter & (1 << j)) > 0):







python python-3.x if-statement data-structures set






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 5:35









I am LI am L

1219




1219













  • How do we know without knowing what is counter, i and j ? << is bitwise left shift operator doc.

    – Austin
    Nov 25 '18 at 5:43











  • It checks if bit number j in counter is set.

    – Michael Butscher
    Nov 25 '18 at 5:49











  • This was the program I was referrring to:geeksforgeeks.org/power-set

    – I am L
    Nov 25 '18 at 6:33



















  • How do we know without knowing what is counter, i and j ? << is bitwise left shift operator doc.

    – Austin
    Nov 25 '18 at 5:43











  • It checks if bit number j in counter is set.

    – Michael Butscher
    Nov 25 '18 at 5:49











  • This was the program I was referrring to:geeksforgeeks.org/power-set

    – I am L
    Nov 25 '18 at 6:33

















How do we know without knowing what is counter, i and j ? << is bitwise left shift operator doc.

– Austin
Nov 25 '18 at 5:43





How do we know without knowing what is counter, i and j ? << is bitwise left shift operator doc.

– Austin
Nov 25 '18 at 5:43













It checks if bit number j in counter is set.

– Michael Butscher
Nov 25 '18 at 5:49





It checks if bit number j in counter is set.

– Michael Butscher
Nov 25 '18 at 5:49













This was the program I was referrring to:geeksforgeeks.org/power-set

– I am L
Nov 25 '18 at 6:33





This was the program I was referrring to:geeksforgeeks.org/power-set

– I am L
Nov 25 '18 at 6:33












2 Answers
2






active

oldest

votes


















1














Your statement:



if ( (counter & (1 << j)) > 0 ):


It's some bitwise operations. Let's break it down:





  • (1 << j) generates the number 0b1 and shifts it left by j places - j must be an integer. This is akin to saying 2**j, or 2 to the jth power, but doing it with the bit-shifting operator << makes it clear we're doing bitwise operations.


  • counter & (1 << j) takes the result of that last operation, and does a bitwise and with the variable counter. It seems that j is a specifier for a bitmask - it tells which bit is important to check in in counter. Since whatever (1 << j) produces will only have one 1 in its binary representation, the expression counter & (1 << j) will always produce either a power of 2, or 0.


  • > 0 checks if the number that was produced was 0.


All in all, this is a fairly involved way to check if the jth bit from the right in counter is equal to 1 or 0. Without seeing the rest of your code, it's impossible to tell what the line does in context, but hopefully this gives you enough of a hint to whatever you're trying to figure out.






share|improve this answer
























  • geeksforgeeks.org/power-set

    – I am L
    Nov 25 '18 at 6:31











  • This was the program I was referrring to:geeksforgeeks.org/power-set

    – I am L
    Nov 25 '18 at 6:32





















1














The & operator is a bitwise operator. More information here.



Basically, if we consider counter as a binary:



counter = 0b0010
1 << j -> with j = 0


j will be the position that you want to evaluate with the 1.



Therefore, in this case the IF statement will not be executed because the AND operator will return 0.



But with:



counter = 0b0010
1 << j -> with j = 1


The IF statement will be executed because the AND operator will return 1.



To understand a little more, you can play with the following piece of code and change the values of counter and j:



counter = 0b0100
j = 2

if((counter & (1 << j)) > 0):
print("True")
else:
print("False")





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%2f53464944%2fwhat-does-ifcounter-1-j-0-do%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Your statement:



    if ( (counter & (1 << j)) > 0 ):


    It's some bitwise operations. Let's break it down:





    • (1 << j) generates the number 0b1 and shifts it left by j places - j must be an integer. This is akin to saying 2**j, or 2 to the jth power, but doing it with the bit-shifting operator << makes it clear we're doing bitwise operations.


    • counter & (1 << j) takes the result of that last operation, and does a bitwise and with the variable counter. It seems that j is a specifier for a bitmask - it tells which bit is important to check in in counter. Since whatever (1 << j) produces will only have one 1 in its binary representation, the expression counter & (1 << j) will always produce either a power of 2, or 0.


    • > 0 checks if the number that was produced was 0.


    All in all, this is a fairly involved way to check if the jth bit from the right in counter is equal to 1 or 0. Without seeing the rest of your code, it's impossible to tell what the line does in context, but hopefully this gives you enough of a hint to whatever you're trying to figure out.






    share|improve this answer
























    • geeksforgeeks.org/power-set

      – I am L
      Nov 25 '18 at 6:31











    • This was the program I was referrring to:geeksforgeeks.org/power-set

      – I am L
      Nov 25 '18 at 6:32


















    1














    Your statement:



    if ( (counter & (1 << j)) > 0 ):


    It's some bitwise operations. Let's break it down:





    • (1 << j) generates the number 0b1 and shifts it left by j places - j must be an integer. This is akin to saying 2**j, or 2 to the jth power, but doing it with the bit-shifting operator << makes it clear we're doing bitwise operations.


    • counter & (1 << j) takes the result of that last operation, and does a bitwise and with the variable counter. It seems that j is a specifier for a bitmask - it tells which bit is important to check in in counter. Since whatever (1 << j) produces will only have one 1 in its binary representation, the expression counter & (1 << j) will always produce either a power of 2, or 0.


    • > 0 checks if the number that was produced was 0.


    All in all, this is a fairly involved way to check if the jth bit from the right in counter is equal to 1 or 0. Without seeing the rest of your code, it's impossible to tell what the line does in context, but hopefully this gives you enough of a hint to whatever you're trying to figure out.






    share|improve this answer
























    • geeksforgeeks.org/power-set

      – I am L
      Nov 25 '18 at 6:31











    • This was the program I was referrring to:geeksforgeeks.org/power-set

      – I am L
      Nov 25 '18 at 6:32
















    1












    1








    1







    Your statement:



    if ( (counter & (1 << j)) > 0 ):


    It's some bitwise operations. Let's break it down:





    • (1 << j) generates the number 0b1 and shifts it left by j places - j must be an integer. This is akin to saying 2**j, or 2 to the jth power, but doing it with the bit-shifting operator << makes it clear we're doing bitwise operations.


    • counter & (1 << j) takes the result of that last operation, and does a bitwise and with the variable counter. It seems that j is a specifier for a bitmask - it tells which bit is important to check in in counter. Since whatever (1 << j) produces will only have one 1 in its binary representation, the expression counter & (1 << j) will always produce either a power of 2, or 0.


    • > 0 checks if the number that was produced was 0.


    All in all, this is a fairly involved way to check if the jth bit from the right in counter is equal to 1 or 0. Without seeing the rest of your code, it's impossible to tell what the line does in context, but hopefully this gives you enough of a hint to whatever you're trying to figure out.






    share|improve this answer













    Your statement:



    if ( (counter & (1 << j)) > 0 ):


    It's some bitwise operations. Let's break it down:





    • (1 << j) generates the number 0b1 and shifts it left by j places - j must be an integer. This is akin to saying 2**j, or 2 to the jth power, but doing it with the bit-shifting operator << makes it clear we're doing bitwise operations.


    • counter & (1 << j) takes the result of that last operation, and does a bitwise and with the variable counter. It seems that j is a specifier for a bitmask - it tells which bit is important to check in in counter. Since whatever (1 << j) produces will only have one 1 in its binary representation, the expression counter & (1 << j) will always produce either a power of 2, or 0.


    • > 0 checks if the number that was produced was 0.


    All in all, this is a fairly involved way to check if the jth bit from the right in counter is equal to 1 or 0. Without seeing the rest of your code, it's impossible to tell what the line does in context, but hopefully this gives you enough of a hint to whatever you're trying to figure out.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 25 '18 at 5:52









    Green Cloak GuyGreen Cloak Guy

    3,4201721




    3,4201721













    • geeksforgeeks.org/power-set

      – I am L
      Nov 25 '18 at 6:31











    • This was the program I was referrring to:geeksforgeeks.org/power-set

      – I am L
      Nov 25 '18 at 6:32





















    • geeksforgeeks.org/power-set

      – I am L
      Nov 25 '18 at 6:31











    • This was the program I was referrring to:geeksforgeeks.org/power-set

      – I am L
      Nov 25 '18 at 6:32



















    geeksforgeeks.org/power-set

    – I am L
    Nov 25 '18 at 6:31





    geeksforgeeks.org/power-set

    – I am L
    Nov 25 '18 at 6:31













    This was the program I was referrring to:geeksforgeeks.org/power-set

    – I am L
    Nov 25 '18 at 6:32







    This was the program I was referrring to:geeksforgeeks.org/power-set

    – I am L
    Nov 25 '18 at 6:32















    1














    The & operator is a bitwise operator. More information here.



    Basically, if we consider counter as a binary:



    counter = 0b0010
    1 << j -> with j = 0


    j will be the position that you want to evaluate with the 1.



    Therefore, in this case the IF statement will not be executed because the AND operator will return 0.



    But with:



    counter = 0b0010
    1 << j -> with j = 1


    The IF statement will be executed because the AND operator will return 1.



    To understand a little more, you can play with the following piece of code and change the values of counter and j:



    counter = 0b0100
    j = 2

    if((counter & (1 << j)) > 0):
    print("True")
    else:
    print("False")





    share|improve this answer




























      1














      The & operator is a bitwise operator. More information here.



      Basically, if we consider counter as a binary:



      counter = 0b0010
      1 << j -> with j = 0


      j will be the position that you want to evaluate with the 1.



      Therefore, in this case the IF statement will not be executed because the AND operator will return 0.



      But with:



      counter = 0b0010
      1 << j -> with j = 1


      The IF statement will be executed because the AND operator will return 1.



      To understand a little more, you can play with the following piece of code and change the values of counter and j:



      counter = 0b0100
      j = 2

      if((counter & (1 << j)) > 0):
      print("True")
      else:
      print("False")





      share|improve this answer


























        1












        1








        1







        The & operator is a bitwise operator. More information here.



        Basically, if we consider counter as a binary:



        counter = 0b0010
        1 << j -> with j = 0


        j will be the position that you want to evaluate with the 1.



        Therefore, in this case the IF statement will not be executed because the AND operator will return 0.



        But with:



        counter = 0b0010
        1 << j -> with j = 1


        The IF statement will be executed because the AND operator will return 1.



        To understand a little more, you can play with the following piece of code and change the values of counter and j:



        counter = 0b0100
        j = 2

        if((counter & (1 << j)) > 0):
        print("True")
        else:
        print("False")





        share|improve this answer













        The & operator is a bitwise operator. More information here.



        Basically, if we consider counter as a binary:



        counter = 0b0010
        1 << j -> with j = 0


        j will be the position that you want to evaluate with the 1.



        Therefore, in this case the IF statement will not be executed because the AND operator will return 0.



        But with:



        counter = 0b0010
        1 << j -> with j = 1


        The IF statement will be executed because the AND operator will return 1.



        To understand a little more, you can play with the following piece of code and change the values of counter and j:



        counter = 0b0100
        j = 2

        if((counter & (1 << j)) > 0):
        print("True")
        else:
        print("False")






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 6:05









        Manuel ReyesManuel Reyes

        886




        886






























            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%2f53464944%2fwhat-does-ifcounter-1-j-0-do%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()