not understanding decimal to binary with bitwise operators in c++ [on hold]












-1















i don't understand this code



void function(int a){
if(a){
function(a>>1);
cout<<(a&1);
}
}


can somebody explain this function to me step by step?










share|improve this question













put on hold as too broad by Sneftel, Toby Speight, Chathuranga Chandrasekara, Mark Rotteveel, cbuchart Mar 5 at 10:40


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 2





    For negative a, the value of a >> 1 is implementation-defined.

    – Alper
    Jan 2 at 9:23






  • 1





    You need to google a few things. "Left and right shift", "Bitwise AND and OR" and "Recursion".

    – Nitin Pawar
    Jan 2 at 9:25













  • You need to target what you do not understand in this function: The bitwise operations (<< and &)? The recursion of function? Something else?

    – Holt
    Jan 2 at 9:29
















-1















i don't understand this code



void function(int a){
if(a){
function(a>>1);
cout<<(a&1);
}
}


can somebody explain this function to me step by step?










share|improve this question













put on hold as too broad by Sneftel, Toby Speight, Chathuranga Chandrasekara, Mark Rotteveel, cbuchart Mar 5 at 10:40


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 2





    For negative a, the value of a >> 1 is implementation-defined.

    – Alper
    Jan 2 at 9:23






  • 1





    You need to google a few things. "Left and right shift", "Bitwise AND and OR" and "Recursion".

    – Nitin Pawar
    Jan 2 at 9:25













  • You need to target what you do not understand in this function: The bitwise operations (<< and &)? The recursion of function? Something else?

    – Holt
    Jan 2 at 9:29














-1












-1








-1








i don't understand this code



void function(int a){
if(a){
function(a>>1);
cout<<(a&1);
}
}


can somebody explain this function to me step by step?










share|improve this question














i don't understand this code



void function(int a){
if(a){
function(a>>1);
cout<<(a&1);
}
}


can somebody explain this function to me step by step?







c++ function bitwise-operators






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 9:20









amirhosein arabhajiamirhosein arabhaji

144




144




put on hold as too broad by Sneftel, Toby Speight, Chathuranga Chandrasekara, Mark Rotteveel, cbuchart Mar 5 at 10:40


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









put on hold as too broad by Sneftel, Toby Speight, Chathuranga Chandrasekara, Mark Rotteveel, cbuchart Mar 5 at 10:40


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2





    For negative a, the value of a >> 1 is implementation-defined.

    – Alper
    Jan 2 at 9:23






  • 1





    You need to google a few things. "Left and right shift", "Bitwise AND and OR" and "Recursion".

    – Nitin Pawar
    Jan 2 at 9:25













  • You need to target what you do not understand in this function: The bitwise operations (<< and &)? The recursion of function? Something else?

    – Holt
    Jan 2 at 9:29














  • 2





    For negative a, the value of a >> 1 is implementation-defined.

    – Alper
    Jan 2 at 9:23






  • 1





    You need to google a few things. "Left and right shift", "Bitwise AND and OR" and "Recursion".

    – Nitin Pawar
    Jan 2 at 9:25













  • You need to target what you do not understand in this function: The bitwise operations (<< and &)? The recursion of function? Something else?

    – Holt
    Jan 2 at 9:29








2




2





For negative a, the value of a >> 1 is implementation-defined.

– Alper
Jan 2 at 9:23





For negative a, the value of a >> 1 is implementation-defined.

– Alper
Jan 2 at 9:23




1




1





You need to google a few things. "Left and right shift", "Bitwise AND and OR" and "Recursion".

– Nitin Pawar
Jan 2 at 9:25







You need to google a few things. "Left and right shift", "Bitwise AND and OR" and "Recursion".

– Nitin Pawar
Jan 2 at 9:25















You need to target what you do not understand in this function: The bitwise operations (<< and &)? The recursion of function? Something else?

– Holt
Jan 2 at 9:29





You need to target what you do not understand in this function: The bitwise operations (<< and &)? The recursion of function? Something else?

– Holt
Jan 2 at 9:29












3 Answers
3






active

oldest

votes


















2














// Step 1: You call a function defined as `void function(int a)`
// which accepts one parameter of type int and returns a void.
void function(int a)
{
// Step 2: This function checks if a is zero or some other value.
// If it is zero, nothing happens and the function returns.
// If it is non-zero, we proceed inside the if block.
if(a){
// Step 3: we do two things here.
// First we right-shift whatever value is stored in a.
// Right-shifting means if we have e.g. a=8, which is 1000 in binary,
// then rightshifting 8 would be 0100 or 4. Basically it is division by 2.
// This continues until we have 0000.

// Secondly, this function will call itself then. This is called recursion.
// So the "void function(4)-> void function(2) -> void function(1)" calls will run.
function(a>>1);

// Here we will bitwise AND with 1.
// So, 0001 & 0001 = 1
// 0010 & 0001 = 0
// 0100 & 0001 = 0
// 1000 & 0001 = 0
// And this is what you will see on the terminal as output: 1000
// The 1 gets printed first, because the inner most call in the recursion
// finishes first and the call stack unwraps back up.
cout<<(a&1);
}
}





share|improve this answer


























  • 'First we right-shift' - that's perhaps ambivalent; if had to rewrite the function solely based on this explanation, I'd do a >>= 1; f(a); std::cout << (a&1).

    – Aconcagua
    Jan 2 at 9:48








  • 1





    @OP, To avoid the problems, mentioned for the case of negative numbers, I would change if(a) by if (a>0).

    – Joey Mallone
    Jan 2 at 9:49













  • @Aconcagua, Are you referring to "6.2.2 Evaluation Order [expr.evaluation]"? I always thought that the operation would be performed first, before the function call continues. Is it not so? If not, then apparently, I have learned something new today.

    – Joey Mallone
    Jan 2 at 9:56











  • Your understanding is fine, the point is: your wording could be misunderstood. Sure the value first is shifted right, then passed to the function - to be more precise: the result of the shift is passed - wheras the operand a remains unchanged. 'First we right-shift' I'd understand, in contrast to, as modifying the operand as well, just as in f(a >>= 1) (note the additional =).

    – Aconcagua
    Jan 2 at 10:07








  • 1





    Apart from: It is totally legal to right-shift 0 (no UB at all)! 0 >> 1 just remains 0...

    – Aconcagua
    Jan 2 at 10:13



















1














Recursive function



void function(int a){
if(a){ // if a is not 0, continue calling recursively
function(a>>1); // right shift a one step i.e. divide by 2, then call ourselves
cout<<(a&1); // bitwise & with a i.e print if a is odd or even.
}
}


Example



function(4);
a = 4, a>>1 == 2
function(2);
a = 2, a>>1 == 0
function(0); will end recursion
cout << (2&1) will print 0 since 0x10 & 0x01 == 0x00
cout << (4&1) will print 0 since 0x20 & 0x01 == 0x00





share|improve this answer































    1














    void function(int a)    // function declaration
    {
    if(a) // a != 0?
    {
    function(a>>1); // recursive call to function with a shifted right one bit
    cout<<(a&1); // print out least significant bit of a
    }
    }


    This function prints out a binary representation of a number given, e. g. a initially 13:



    f(13)            // 13 == 0b1101
    {
    f(6) // 0b1101 >> 1 == 0b0110
    {
    f(3) // 0b0110 >> 1 == 0b0011
    {
    f(1) // etc
    {
    f(0)
    { } // does nothing
    std::cout << (1 & 1); // 1, most significant bit of original 13
    }
    std::cout << (3 & 1); // 1
    }
    std::cout << (6 & 1); // 0
    }
    std::cout << (13 & 1); // 1
    }

    1101 in total


    Signature is dangerous, though; wheras it is implementation defined if the shift is logic (always 0 shifted in at the left) or arithmetic (shift in current sign bit again), usually (question is C, but applies for C++ as well) arithmetic shift is implemented for signed values.



    So you would end up in an endless recursion if you pass a negative value to the function. Better:



    void function(unsigned int a);
    // ^^^^^^^^


    For unsigned values, logic and arithmetic shift are the same...



    Side note: if you pass 0 initially to the function, you don't get any output at all (either variant).






    share|improve this answer
































      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      // Step 1: You call a function defined as `void function(int a)`
      // which accepts one parameter of type int and returns a void.
      void function(int a)
      {
      // Step 2: This function checks if a is zero or some other value.
      // If it is zero, nothing happens and the function returns.
      // If it is non-zero, we proceed inside the if block.
      if(a){
      // Step 3: we do two things here.
      // First we right-shift whatever value is stored in a.
      // Right-shifting means if we have e.g. a=8, which is 1000 in binary,
      // then rightshifting 8 would be 0100 or 4. Basically it is division by 2.
      // This continues until we have 0000.

      // Secondly, this function will call itself then. This is called recursion.
      // So the "void function(4)-> void function(2) -> void function(1)" calls will run.
      function(a>>1);

      // Here we will bitwise AND with 1.
      // So, 0001 & 0001 = 1
      // 0010 & 0001 = 0
      // 0100 & 0001 = 0
      // 1000 & 0001 = 0
      // And this is what you will see on the terminal as output: 1000
      // The 1 gets printed first, because the inner most call in the recursion
      // finishes first and the call stack unwraps back up.
      cout<<(a&1);
      }
      }





      share|improve this answer


























      • 'First we right-shift' - that's perhaps ambivalent; if had to rewrite the function solely based on this explanation, I'd do a >>= 1; f(a); std::cout << (a&1).

        – Aconcagua
        Jan 2 at 9:48








      • 1





        @OP, To avoid the problems, mentioned for the case of negative numbers, I would change if(a) by if (a>0).

        – Joey Mallone
        Jan 2 at 9:49













      • @Aconcagua, Are you referring to "6.2.2 Evaluation Order [expr.evaluation]"? I always thought that the operation would be performed first, before the function call continues. Is it not so? If not, then apparently, I have learned something new today.

        – Joey Mallone
        Jan 2 at 9:56











      • Your understanding is fine, the point is: your wording could be misunderstood. Sure the value first is shifted right, then passed to the function - to be more precise: the result of the shift is passed - wheras the operand a remains unchanged. 'First we right-shift' I'd understand, in contrast to, as modifying the operand as well, just as in f(a >>= 1) (note the additional =).

        – Aconcagua
        Jan 2 at 10:07








      • 1





        Apart from: It is totally legal to right-shift 0 (no UB at all)! 0 >> 1 just remains 0...

        – Aconcagua
        Jan 2 at 10:13
















      2














      // Step 1: You call a function defined as `void function(int a)`
      // which accepts one parameter of type int and returns a void.
      void function(int a)
      {
      // Step 2: This function checks if a is zero or some other value.
      // If it is zero, nothing happens and the function returns.
      // If it is non-zero, we proceed inside the if block.
      if(a){
      // Step 3: we do two things here.
      // First we right-shift whatever value is stored in a.
      // Right-shifting means if we have e.g. a=8, which is 1000 in binary,
      // then rightshifting 8 would be 0100 or 4. Basically it is division by 2.
      // This continues until we have 0000.

      // Secondly, this function will call itself then. This is called recursion.
      // So the "void function(4)-> void function(2) -> void function(1)" calls will run.
      function(a>>1);

      // Here we will bitwise AND with 1.
      // So, 0001 & 0001 = 1
      // 0010 & 0001 = 0
      // 0100 & 0001 = 0
      // 1000 & 0001 = 0
      // And this is what you will see on the terminal as output: 1000
      // The 1 gets printed first, because the inner most call in the recursion
      // finishes first and the call stack unwraps back up.
      cout<<(a&1);
      }
      }





      share|improve this answer


























      • 'First we right-shift' - that's perhaps ambivalent; if had to rewrite the function solely based on this explanation, I'd do a >>= 1; f(a); std::cout << (a&1).

        – Aconcagua
        Jan 2 at 9:48








      • 1





        @OP, To avoid the problems, mentioned for the case of negative numbers, I would change if(a) by if (a>0).

        – Joey Mallone
        Jan 2 at 9:49













      • @Aconcagua, Are you referring to "6.2.2 Evaluation Order [expr.evaluation]"? I always thought that the operation would be performed first, before the function call continues. Is it not so? If not, then apparently, I have learned something new today.

        – Joey Mallone
        Jan 2 at 9:56











      • Your understanding is fine, the point is: your wording could be misunderstood. Sure the value first is shifted right, then passed to the function - to be more precise: the result of the shift is passed - wheras the operand a remains unchanged. 'First we right-shift' I'd understand, in contrast to, as modifying the operand as well, just as in f(a >>= 1) (note the additional =).

        – Aconcagua
        Jan 2 at 10:07








      • 1





        Apart from: It is totally legal to right-shift 0 (no UB at all)! 0 >> 1 just remains 0...

        – Aconcagua
        Jan 2 at 10:13














      2












      2








      2







      // Step 1: You call a function defined as `void function(int a)`
      // which accepts one parameter of type int and returns a void.
      void function(int a)
      {
      // Step 2: This function checks if a is zero or some other value.
      // If it is zero, nothing happens and the function returns.
      // If it is non-zero, we proceed inside the if block.
      if(a){
      // Step 3: we do two things here.
      // First we right-shift whatever value is stored in a.
      // Right-shifting means if we have e.g. a=8, which is 1000 in binary,
      // then rightshifting 8 would be 0100 or 4. Basically it is division by 2.
      // This continues until we have 0000.

      // Secondly, this function will call itself then. This is called recursion.
      // So the "void function(4)-> void function(2) -> void function(1)" calls will run.
      function(a>>1);

      // Here we will bitwise AND with 1.
      // So, 0001 & 0001 = 1
      // 0010 & 0001 = 0
      // 0100 & 0001 = 0
      // 1000 & 0001 = 0
      // And this is what you will see on the terminal as output: 1000
      // The 1 gets printed first, because the inner most call in the recursion
      // finishes first and the call stack unwraps back up.
      cout<<(a&1);
      }
      }





      share|improve this answer















      // Step 1: You call a function defined as `void function(int a)`
      // which accepts one parameter of type int and returns a void.
      void function(int a)
      {
      // Step 2: This function checks if a is zero or some other value.
      // If it is zero, nothing happens and the function returns.
      // If it is non-zero, we proceed inside the if block.
      if(a){
      // Step 3: we do two things here.
      // First we right-shift whatever value is stored in a.
      // Right-shifting means if we have e.g. a=8, which is 1000 in binary,
      // then rightshifting 8 would be 0100 or 4. Basically it is division by 2.
      // This continues until we have 0000.

      // Secondly, this function will call itself then. This is called recursion.
      // So the "void function(4)-> void function(2) -> void function(1)" calls will run.
      function(a>>1);

      // Here we will bitwise AND with 1.
      // So, 0001 & 0001 = 1
      // 0010 & 0001 = 0
      // 0100 & 0001 = 0
      // 1000 & 0001 = 0
      // And this is what you will see on the terminal as output: 1000
      // The 1 gets printed first, because the inner most call in the recursion
      // finishes first and the call stack unwraps back up.
      cout<<(a&1);
      }
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 2 at 10:16

























      answered Jan 2 at 9:41









      Joey MalloneJoey Mallone

      2,24441833




      2,24441833













      • 'First we right-shift' - that's perhaps ambivalent; if had to rewrite the function solely based on this explanation, I'd do a >>= 1; f(a); std::cout << (a&1).

        – Aconcagua
        Jan 2 at 9:48








      • 1





        @OP, To avoid the problems, mentioned for the case of negative numbers, I would change if(a) by if (a>0).

        – Joey Mallone
        Jan 2 at 9:49













      • @Aconcagua, Are you referring to "6.2.2 Evaluation Order [expr.evaluation]"? I always thought that the operation would be performed first, before the function call continues. Is it not so? If not, then apparently, I have learned something new today.

        – Joey Mallone
        Jan 2 at 9:56











      • Your understanding is fine, the point is: your wording could be misunderstood. Sure the value first is shifted right, then passed to the function - to be more precise: the result of the shift is passed - wheras the operand a remains unchanged. 'First we right-shift' I'd understand, in contrast to, as modifying the operand as well, just as in f(a >>= 1) (note the additional =).

        – Aconcagua
        Jan 2 at 10:07








      • 1





        Apart from: It is totally legal to right-shift 0 (no UB at all)! 0 >> 1 just remains 0...

        – Aconcagua
        Jan 2 at 10:13



















      • 'First we right-shift' - that's perhaps ambivalent; if had to rewrite the function solely based on this explanation, I'd do a >>= 1; f(a); std::cout << (a&1).

        – Aconcagua
        Jan 2 at 9:48








      • 1





        @OP, To avoid the problems, mentioned for the case of negative numbers, I would change if(a) by if (a>0).

        – Joey Mallone
        Jan 2 at 9:49













      • @Aconcagua, Are you referring to "6.2.2 Evaluation Order [expr.evaluation]"? I always thought that the operation would be performed first, before the function call continues. Is it not so? If not, then apparently, I have learned something new today.

        – Joey Mallone
        Jan 2 at 9:56











      • Your understanding is fine, the point is: your wording could be misunderstood. Sure the value first is shifted right, then passed to the function - to be more precise: the result of the shift is passed - wheras the operand a remains unchanged. 'First we right-shift' I'd understand, in contrast to, as modifying the operand as well, just as in f(a >>= 1) (note the additional =).

        – Aconcagua
        Jan 2 at 10:07








      • 1





        Apart from: It is totally legal to right-shift 0 (no UB at all)! 0 >> 1 just remains 0...

        – Aconcagua
        Jan 2 at 10:13

















      'First we right-shift' - that's perhaps ambivalent; if had to rewrite the function solely based on this explanation, I'd do a >>= 1; f(a); std::cout << (a&1).

      – Aconcagua
      Jan 2 at 9:48







      'First we right-shift' - that's perhaps ambivalent; if had to rewrite the function solely based on this explanation, I'd do a >>= 1; f(a); std::cout << (a&1).

      – Aconcagua
      Jan 2 at 9:48






      1




      1





      @OP, To avoid the problems, mentioned for the case of negative numbers, I would change if(a) by if (a>0).

      – Joey Mallone
      Jan 2 at 9:49







      @OP, To avoid the problems, mentioned for the case of negative numbers, I would change if(a) by if (a>0).

      – Joey Mallone
      Jan 2 at 9:49















      @Aconcagua, Are you referring to "6.2.2 Evaluation Order [expr.evaluation]"? I always thought that the operation would be performed first, before the function call continues. Is it not so? If not, then apparently, I have learned something new today.

      – Joey Mallone
      Jan 2 at 9:56





      @Aconcagua, Are you referring to "6.2.2 Evaluation Order [expr.evaluation]"? I always thought that the operation would be performed first, before the function call continues. Is it not so? If not, then apparently, I have learned something new today.

      – Joey Mallone
      Jan 2 at 9:56













      Your understanding is fine, the point is: your wording could be misunderstood. Sure the value first is shifted right, then passed to the function - to be more precise: the result of the shift is passed - wheras the operand a remains unchanged. 'First we right-shift' I'd understand, in contrast to, as modifying the operand as well, just as in f(a >>= 1) (note the additional =).

      – Aconcagua
      Jan 2 at 10:07







      Your understanding is fine, the point is: your wording could be misunderstood. Sure the value first is shifted right, then passed to the function - to be more precise: the result of the shift is passed - wheras the operand a remains unchanged. 'First we right-shift' I'd understand, in contrast to, as modifying the operand as well, just as in f(a >>= 1) (note the additional =).

      – Aconcagua
      Jan 2 at 10:07






      1




      1





      Apart from: It is totally legal to right-shift 0 (no UB at all)! 0 >> 1 just remains 0...

      – Aconcagua
      Jan 2 at 10:13





      Apart from: It is totally legal to right-shift 0 (no UB at all)! 0 >> 1 just remains 0...

      – Aconcagua
      Jan 2 at 10:13













      1














      Recursive function



      void function(int a){
      if(a){ // if a is not 0, continue calling recursively
      function(a>>1); // right shift a one step i.e. divide by 2, then call ourselves
      cout<<(a&1); // bitwise & with a i.e print if a is odd or even.
      }
      }


      Example



      function(4);
      a = 4, a>>1 == 2
      function(2);
      a = 2, a>>1 == 0
      function(0); will end recursion
      cout << (2&1) will print 0 since 0x10 & 0x01 == 0x00
      cout << (4&1) will print 0 since 0x20 & 0x01 == 0x00





      share|improve this answer




























        1














        Recursive function



        void function(int a){
        if(a){ // if a is not 0, continue calling recursively
        function(a>>1); // right shift a one step i.e. divide by 2, then call ourselves
        cout<<(a&1); // bitwise & with a i.e print if a is odd or even.
        }
        }


        Example



        function(4);
        a = 4, a>>1 == 2
        function(2);
        a = 2, a>>1 == 0
        function(0); will end recursion
        cout << (2&1) will print 0 since 0x10 & 0x01 == 0x00
        cout << (4&1) will print 0 since 0x20 & 0x01 == 0x00





        share|improve this answer


























          1












          1








          1







          Recursive function



          void function(int a){
          if(a){ // if a is not 0, continue calling recursively
          function(a>>1); // right shift a one step i.e. divide by 2, then call ourselves
          cout<<(a&1); // bitwise & with a i.e print if a is odd or even.
          }
          }


          Example



          function(4);
          a = 4, a>>1 == 2
          function(2);
          a = 2, a>>1 == 0
          function(0); will end recursion
          cout << (2&1) will print 0 since 0x10 & 0x01 == 0x00
          cout << (4&1) will print 0 since 0x20 & 0x01 == 0x00





          share|improve this answer













          Recursive function



          void function(int a){
          if(a){ // if a is not 0, continue calling recursively
          function(a>>1); // right shift a one step i.e. divide by 2, then call ourselves
          cout<<(a&1); // bitwise & with a i.e print if a is odd or even.
          }
          }


          Example



          function(4);
          a = 4, a>>1 == 2
          function(2);
          a = 2, a>>1 == 0
          function(0); will end recursion
          cout << (2&1) will print 0 since 0x10 & 0x01 == 0x00
          cout << (4&1) will print 0 since 0x20 & 0x01 == 0x00






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 9:30









          AndersAnders

          30.4k64872




          30.4k64872























              1














              void function(int a)    // function declaration
              {
              if(a) // a != 0?
              {
              function(a>>1); // recursive call to function with a shifted right one bit
              cout<<(a&1); // print out least significant bit of a
              }
              }


              This function prints out a binary representation of a number given, e. g. a initially 13:



              f(13)            // 13 == 0b1101
              {
              f(6) // 0b1101 >> 1 == 0b0110
              {
              f(3) // 0b0110 >> 1 == 0b0011
              {
              f(1) // etc
              {
              f(0)
              { } // does nothing
              std::cout << (1 & 1); // 1, most significant bit of original 13
              }
              std::cout << (3 & 1); // 1
              }
              std::cout << (6 & 1); // 0
              }
              std::cout << (13 & 1); // 1
              }

              1101 in total


              Signature is dangerous, though; wheras it is implementation defined if the shift is logic (always 0 shifted in at the left) or arithmetic (shift in current sign bit again), usually (question is C, but applies for C++ as well) arithmetic shift is implemented for signed values.



              So you would end up in an endless recursion if you pass a negative value to the function. Better:



              void function(unsigned int a);
              // ^^^^^^^^


              For unsigned values, logic and arithmetic shift are the same...



              Side note: if you pass 0 initially to the function, you don't get any output at all (either variant).






              share|improve this answer






























                1














                void function(int a)    // function declaration
                {
                if(a) // a != 0?
                {
                function(a>>1); // recursive call to function with a shifted right one bit
                cout<<(a&1); // print out least significant bit of a
                }
                }


                This function prints out a binary representation of a number given, e. g. a initially 13:



                f(13)            // 13 == 0b1101
                {
                f(6) // 0b1101 >> 1 == 0b0110
                {
                f(3) // 0b0110 >> 1 == 0b0011
                {
                f(1) // etc
                {
                f(0)
                { } // does nothing
                std::cout << (1 & 1); // 1, most significant bit of original 13
                }
                std::cout << (3 & 1); // 1
                }
                std::cout << (6 & 1); // 0
                }
                std::cout << (13 & 1); // 1
                }

                1101 in total


                Signature is dangerous, though; wheras it is implementation defined if the shift is logic (always 0 shifted in at the left) or arithmetic (shift in current sign bit again), usually (question is C, but applies for C++ as well) arithmetic shift is implemented for signed values.



                So you would end up in an endless recursion if you pass a negative value to the function. Better:



                void function(unsigned int a);
                // ^^^^^^^^


                For unsigned values, logic and arithmetic shift are the same...



                Side note: if you pass 0 initially to the function, you don't get any output at all (either variant).






                share|improve this answer




























                  1












                  1








                  1







                  void function(int a)    // function declaration
                  {
                  if(a) // a != 0?
                  {
                  function(a>>1); // recursive call to function with a shifted right one bit
                  cout<<(a&1); // print out least significant bit of a
                  }
                  }


                  This function prints out a binary representation of a number given, e. g. a initially 13:



                  f(13)            // 13 == 0b1101
                  {
                  f(6) // 0b1101 >> 1 == 0b0110
                  {
                  f(3) // 0b0110 >> 1 == 0b0011
                  {
                  f(1) // etc
                  {
                  f(0)
                  { } // does nothing
                  std::cout << (1 & 1); // 1, most significant bit of original 13
                  }
                  std::cout << (3 & 1); // 1
                  }
                  std::cout << (6 & 1); // 0
                  }
                  std::cout << (13 & 1); // 1
                  }

                  1101 in total


                  Signature is dangerous, though; wheras it is implementation defined if the shift is logic (always 0 shifted in at the left) or arithmetic (shift in current sign bit again), usually (question is C, but applies for C++ as well) arithmetic shift is implemented for signed values.



                  So you would end up in an endless recursion if you pass a negative value to the function. Better:



                  void function(unsigned int a);
                  // ^^^^^^^^


                  For unsigned values, logic and arithmetic shift are the same...



                  Side note: if you pass 0 initially to the function, you don't get any output at all (either variant).






                  share|improve this answer















                  void function(int a)    // function declaration
                  {
                  if(a) // a != 0?
                  {
                  function(a>>1); // recursive call to function with a shifted right one bit
                  cout<<(a&1); // print out least significant bit of a
                  }
                  }


                  This function prints out a binary representation of a number given, e. g. a initially 13:



                  f(13)            // 13 == 0b1101
                  {
                  f(6) // 0b1101 >> 1 == 0b0110
                  {
                  f(3) // 0b0110 >> 1 == 0b0011
                  {
                  f(1) // etc
                  {
                  f(0)
                  { } // does nothing
                  std::cout << (1 & 1); // 1, most significant bit of original 13
                  }
                  std::cout << (3 & 1); // 1
                  }
                  std::cout << (6 & 1); // 0
                  }
                  std::cout << (13 & 1); // 1
                  }

                  1101 in total


                  Signature is dangerous, though; wheras it is implementation defined if the shift is logic (always 0 shifted in at the left) or arithmetic (shift in current sign bit again), usually (question is C, but applies for C++ as well) arithmetic shift is implemented for signed values.



                  So you would end up in an endless recursion if you pass a negative value to the function. Better:



                  void function(unsigned int a);
                  // ^^^^^^^^


                  For unsigned values, logic and arithmetic shift are the same...



                  Side note: if you pass 0 initially to the function, you don't get any output at all (either variant).







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 2 at 9:56

























                  answered Jan 2 at 9:30









                  AconcaguaAconcagua

                  12.7k32144




                  12.7k32144















                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas