Integer as an condition in C++'s “ :?” operator












2















So , for my exam I have to evaluate some C++ expressions.
Here s the expression :



float x=3<<2>>1?4.:.5?6:7>8;


Can someone explain it with words because I can t understand it.
So , I have to convert 3 to binary and shift it 2 bits. But After conversion the result is 11. I may add the bit sign so it becomes 011. And after the bit sign I can add as many 0's as i need and so 3<<2-> 01100. But after that I can't understand a thing.. Where s the condition on the conditional operator ?: because I can't see any?
Thank you in advice for helping me solve this out :)










share|improve this question




















  • 3





    Remember that in C++ any non-zero integer value evaluates to true.

    – scohe001
    Nov 15 '18 at 22:16






  • 6





    This exam question is just silly. Nobody ever writes code like that.

    – Paul Sanders
    Nov 15 '18 at 22:19






  • 3





    Don't worry about understanding it. The code will never pass code review and the dev has been "deleted" ;). I get they want to test on syntax understanding but that example is just laughable.

    – NathanOliver
    Nov 15 '18 at 22:21






  • 1





    Look at the C++ operator precedence table, add parens to group things appropriately. Mind the left-to-right and right-to-left associativity. I would not be surprised if there is a website that will parenthesize the operations of a C++ expression for you.

    – Eljay
    Nov 15 '18 at 22:22











  • As a test question it's remarkably ineffective for determining programming knowledge. One can have the grouping order of the shift operations wrong and still get the correct answer.

    – doug
    Nov 15 '18 at 23:14
















2















So , for my exam I have to evaluate some C++ expressions.
Here s the expression :



float x=3<<2>>1?4.:.5?6:7>8;


Can someone explain it with words because I can t understand it.
So , I have to convert 3 to binary and shift it 2 bits. But After conversion the result is 11. I may add the bit sign so it becomes 011. And after the bit sign I can add as many 0's as i need and so 3<<2-> 01100. But after that I can't understand a thing.. Where s the condition on the conditional operator ?: because I can't see any?
Thank you in advice for helping me solve this out :)










share|improve this question




















  • 3





    Remember that in C++ any non-zero integer value evaluates to true.

    – scohe001
    Nov 15 '18 at 22:16






  • 6





    This exam question is just silly. Nobody ever writes code like that.

    – Paul Sanders
    Nov 15 '18 at 22:19






  • 3





    Don't worry about understanding it. The code will never pass code review and the dev has been "deleted" ;). I get they want to test on syntax understanding but that example is just laughable.

    – NathanOliver
    Nov 15 '18 at 22:21






  • 1





    Look at the C++ operator precedence table, add parens to group things appropriately. Mind the left-to-right and right-to-left associativity. I would not be surprised if there is a website that will parenthesize the operations of a C++ expression for you.

    – Eljay
    Nov 15 '18 at 22:22











  • As a test question it's remarkably ineffective for determining programming knowledge. One can have the grouping order of the shift operations wrong and still get the correct answer.

    – doug
    Nov 15 '18 at 23:14














2












2








2








So , for my exam I have to evaluate some C++ expressions.
Here s the expression :



float x=3<<2>>1?4.:.5?6:7>8;


Can someone explain it with words because I can t understand it.
So , I have to convert 3 to binary and shift it 2 bits. But After conversion the result is 11. I may add the bit sign so it becomes 011. And after the bit sign I can add as many 0's as i need and so 3<<2-> 01100. But after that I can't understand a thing.. Where s the condition on the conditional operator ?: because I can't see any?
Thank you in advice for helping me solve this out :)










share|improve this question
















So , for my exam I have to evaluate some C++ expressions.
Here s the expression :



float x=3<<2>>1?4.:.5?6:7>8;


Can someone explain it with words because I can t understand it.
So , I have to convert 3 to binary and shift it 2 bits. But After conversion the result is 11. I may add the bit sign so it becomes 011. And after the bit sign I can add as many 0's as i need and so 3<<2-> 01100. But after that I can't understand a thing.. Where s the condition on the conditional operator ?: because I can't see any?
Thank you in advice for helping me solve this out :)







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 22:15









zzxyz

2,2041624




2,2041624










asked Nov 15 '18 at 22:13









Andy VavilovAndy Vavilov

183




183








  • 3





    Remember that in C++ any non-zero integer value evaluates to true.

    – scohe001
    Nov 15 '18 at 22:16






  • 6





    This exam question is just silly. Nobody ever writes code like that.

    – Paul Sanders
    Nov 15 '18 at 22:19






  • 3





    Don't worry about understanding it. The code will never pass code review and the dev has been "deleted" ;). I get they want to test on syntax understanding but that example is just laughable.

    – NathanOliver
    Nov 15 '18 at 22:21






  • 1





    Look at the C++ operator precedence table, add parens to group things appropriately. Mind the left-to-right and right-to-left associativity. I would not be surprised if there is a website that will parenthesize the operations of a C++ expression for you.

    – Eljay
    Nov 15 '18 at 22:22











  • As a test question it's remarkably ineffective for determining programming knowledge. One can have the grouping order of the shift operations wrong and still get the correct answer.

    – doug
    Nov 15 '18 at 23:14














  • 3





    Remember that in C++ any non-zero integer value evaluates to true.

    – scohe001
    Nov 15 '18 at 22:16






  • 6





    This exam question is just silly. Nobody ever writes code like that.

    – Paul Sanders
    Nov 15 '18 at 22:19






  • 3





    Don't worry about understanding it. The code will never pass code review and the dev has been "deleted" ;). I get they want to test on syntax understanding but that example is just laughable.

    – NathanOliver
    Nov 15 '18 at 22:21






  • 1





    Look at the C++ operator precedence table, add parens to group things appropriately. Mind the left-to-right and right-to-left associativity. I would not be surprised if there is a website that will parenthesize the operations of a C++ expression for you.

    – Eljay
    Nov 15 '18 at 22:22











  • As a test question it's remarkably ineffective for determining programming knowledge. One can have the grouping order of the shift operations wrong and still get the correct answer.

    – doug
    Nov 15 '18 at 23:14








3




3





Remember that in C++ any non-zero integer value evaluates to true.

– scohe001
Nov 15 '18 at 22:16





Remember that in C++ any non-zero integer value evaluates to true.

– scohe001
Nov 15 '18 at 22:16




6




6





This exam question is just silly. Nobody ever writes code like that.

– Paul Sanders
Nov 15 '18 at 22:19





This exam question is just silly. Nobody ever writes code like that.

– Paul Sanders
Nov 15 '18 at 22:19




3




3





Don't worry about understanding it. The code will never pass code review and the dev has been "deleted" ;). I get they want to test on syntax understanding but that example is just laughable.

– NathanOliver
Nov 15 '18 at 22:21





Don't worry about understanding it. The code will never pass code review and the dev has been "deleted" ;). I get they want to test on syntax understanding but that example is just laughable.

– NathanOliver
Nov 15 '18 at 22:21




1




1





Look at the C++ operator precedence table, add parens to group things appropriately. Mind the left-to-right and right-to-left associativity. I would not be surprised if there is a website that will parenthesize the operations of a C++ expression for you.

– Eljay
Nov 15 '18 at 22:22





Look at the C++ operator precedence table, add parens to group things appropriately. Mind the left-to-right and right-to-left associativity. I would not be surprised if there is a website that will parenthesize the operations of a C++ expression for you.

– Eljay
Nov 15 '18 at 22:22













As a test question it's remarkably ineffective for determining programming knowledge. One can have the grouping order of the shift operations wrong and still get the correct answer.

– doug
Nov 15 '18 at 23:14





As a test question it's remarkably ineffective for determining programming knowledge. One can have the grouping order of the shift operations wrong and still get the correct answer.

– doug
Nov 15 '18 at 23:14












2 Answers
2






active

oldest

votes


















4














The expression is



3<<2>>1?4.:.5?6:7>8


Let's add some spaces and parens:



((3 << 2) >> 1) ? 4. : (.5 ? 6 : (7 > 8))


Shifting by 1 is just multiplying by 2. Shifting by 2 is multiplying by 2, twice.



So 3 << 2 is 12.



12 >> 1 divides by 2, so that's just 6.



As a condition, all integers (except 0) are true, so this simply returns 4.0.






share|improve this answer































    5














    Let's format this a little better:



    float x = (3 << 2) >> 1 ? 4. :
    .5 ? 6 :
    (7 > 8);


    Or even better:



    float x;
    if( 3<<2>>1 ) {
    x = 4.;
    } else if( .5 ) {
    x = 6;
    } else {
    x = 7 > 8;
    }


    Walking through the evaluation of 3 << 2 >> 1:



    (3 << 2) >> 1      =>
    (b0011 << 2) >> 1 =>
    b1100 >> 1 =>
    b0110 =>
    6


    Since 6 is a nonzero value (which evaluates to true in C++) x will have the value 4.






    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%2f53328636%2finteger-as-an-condition-in-cs-operator%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









      4














      The expression is



      3<<2>>1?4.:.5?6:7>8


      Let's add some spaces and parens:



      ((3 << 2) >> 1) ? 4. : (.5 ? 6 : (7 > 8))


      Shifting by 1 is just multiplying by 2. Shifting by 2 is multiplying by 2, twice.



      So 3 << 2 is 12.



      12 >> 1 divides by 2, so that's just 6.



      As a condition, all integers (except 0) are true, so this simply returns 4.0.






      share|improve this answer




























        4














        The expression is



        3<<2>>1?4.:.5?6:7>8


        Let's add some spaces and parens:



        ((3 << 2) >> 1) ? 4. : (.5 ? 6 : (7 > 8))


        Shifting by 1 is just multiplying by 2. Shifting by 2 is multiplying by 2, twice.



        So 3 << 2 is 12.



        12 >> 1 divides by 2, so that's just 6.



        As a condition, all integers (except 0) are true, so this simply returns 4.0.






        share|improve this answer


























          4












          4








          4







          The expression is



          3<<2>>1?4.:.5?6:7>8


          Let's add some spaces and parens:



          ((3 << 2) >> 1) ? 4. : (.5 ? 6 : (7 > 8))


          Shifting by 1 is just multiplying by 2. Shifting by 2 is multiplying by 2, twice.



          So 3 << 2 is 12.



          12 >> 1 divides by 2, so that's just 6.



          As a condition, all integers (except 0) are true, so this simply returns 4.0.






          share|improve this answer













          The expression is



          3<<2>>1?4.:.5?6:7>8


          Let's add some spaces and parens:



          ((3 << 2) >> 1) ? 4. : (.5 ? 6 : (7 > 8))


          Shifting by 1 is just multiplying by 2. Shifting by 2 is multiplying by 2, twice.



          So 3 << 2 is 12.



          12 >> 1 divides by 2, so that's just 6.



          As a condition, all integers (except 0) are true, so this simply returns 4.0.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 22:23









          melpomenemelpomene

          59.9k54693




          59.9k54693

























              5














              Let's format this a little better:



              float x = (3 << 2) >> 1 ? 4. :
              .5 ? 6 :
              (7 > 8);


              Or even better:



              float x;
              if( 3<<2>>1 ) {
              x = 4.;
              } else if( .5 ) {
              x = 6;
              } else {
              x = 7 > 8;
              }


              Walking through the evaluation of 3 << 2 >> 1:



              (3 << 2) >> 1      =>
              (b0011 << 2) >> 1 =>
              b1100 >> 1 =>
              b0110 =>
              6


              Since 6 is a nonzero value (which evaluates to true in C++) x will have the value 4.






              share|improve this answer






























                5














                Let's format this a little better:



                float x = (3 << 2) >> 1 ? 4. :
                .5 ? 6 :
                (7 > 8);


                Or even better:



                float x;
                if( 3<<2>>1 ) {
                x = 4.;
                } else if( .5 ) {
                x = 6;
                } else {
                x = 7 > 8;
                }


                Walking through the evaluation of 3 << 2 >> 1:



                (3 << 2) >> 1      =>
                (b0011 << 2) >> 1 =>
                b1100 >> 1 =>
                b0110 =>
                6


                Since 6 is a nonzero value (which evaluates to true in C++) x will have the value 4.






                share|improve this answer




























                  5












                  5








                  5







                  Let's format this a little better:



                  float x = (3 << 2) >> 1 ? 4. :
                  .5 ? 6 :
                  (7 > 8);


                  Or even better:



                  float x;
                  if( 3<<2>>1 ) {
                  x = 4.;
                  } else if( .5 ) {
                  x = 6;
                  } else {
                  x = 7 > 8;
                  }


                  Walking through the evaluation of 3 << 2 >> 1:



                  (3 << 2) >> 1      =>
                  (b0011 << 2) >> 1 =>
                  b1100 >> 1 =>
                  b0110 =>
                  6


                  Since 6 is a nonzero value (which evaluates to true in C++) x will have the value 4.






                  share|improve this answer















                  Let's format this a little better:



                  float x = (3 << 2) >> 1 ? 4. :
                  .5 ? 6 :
                  (7 > 8);


                  Or even better:



                  float x;
                  if( 3<<2>>1 ) {
                  x = 4.;
                  } else if( .5 ) {
                  x = 6;
                  } else {
                  x = 7 > 8;
                  }


                  Walking through the evaluation of 3 << 2 >> 1:



                  (3 << 2) >> 1      =>
                  (b0011 << 2) >> 1 =>
                  b1100 >> 1 =>
                  b0110 =>
                  6


                  Since 6 is a nonzero value (which evaluates to true in C++) x will have the value 4.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 16 '18 at 4:42

























                  answered Nov 15 '18 at 22:22









                  scohe001scohe001

                  7,87012241




                  7,87012241






























                      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%2f53328636%2finteger-as-an-condition-in-cs-operator%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







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini