Testing Conditions on Port Bytes - ignoring certain bits?











up vote
2
down vote

favorite












Being new to testing byte and bit conditions, I feel like there has to be a better way to test for conditions/values on a byte (read from a port).



For example, I'm reading 8 bits from PORTK. I need to test for a pattern of high bits. (Say, byte value of 207 - bits 7, 6, 3, 2, 1, 0 all high.) Bits 4 and 5 I don't care about. They CAN be high, or can be low. (Using datasheet terminology, they're "X - Don't Care".)



I could do an if, and test if the value equals 207, 223, 239, or 255, but that seems messy and like there would be an easier (and cleaner to read) way.



Edit to add: I've looked at Bit Operators, and .. something is telling me I should/could be using one of those. But I'm having a difficult time walking through which one would help me.



Thanks!
-Mike










share|improve this question







New contributor




Coyttl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    2
    down vote

    favorite












    Being new to testing byte and bit conditions, I feel like there has to be a better way to test for conditions/values on a byte (read from a port).



    For example, I'm reading 8 bits from PORTK. I need to test for a pattern of high bits. (Say, byte value of 207 - bits 7, 6, 3, 2, 1, 0 all high.) Bits 4 and 5 I don't care about. They CAN be high, or can be low. (Using datasheet terminology, they're "X - Don't Care".)



    I could do an if, and test if the value equals 207, 223, 239, or 255, but that seems messy and like there would be an easier (and cleaner to read) way.



    Edit to add: I've looked at Bit Operators, and .. something is telling me I should/could be using one of those. But I'm having a difficult time walking through which one would help me.



    Thanks!
    -Mike










    share|improve this question







    New contributor




    Coyttl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Being new to testing byte and bit conditions, I feel like there has to be a better way to test for conditions/values on a byte (read from a port).



      For example, I'm reading 8 bits from PORTK. I need to test for a pattern of high bits. (Say, byte value of 207 - bits 7, 6, 3, 2, 1, 0 all high.) Bits 4 and 5 I don't care about. They CAN be high, or can be low. (Using datasheet terminology, they're "X - Don't Care".)



      I could do an if, and test if the value equals 207, 223, 239, or 255, but that seems messy and like there would be an easier (and cleaner to read) way.



      Edit to add: I've looked at Bit Operators, and .. something is telling me I should/could be using one of those. But I'm having a difficult time walking through which one would help me.



      Thanks!
      -Mike










      share|improve this question







      New contributor




      Coyttl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Being new to testing byte and bit conditions, I feel like there has to be a better way to test for conditions/values on a byte (read from a port).



      For example, I'm reading 8 bits from PORTK. I need to test for a pattern of high bits. (Say, byte value of 207 - bits 7, 6, 3, 2, 1, 0 all high.) Bits 4 and 5 I don't care about. They CAN be high, or can be low. (Using datasheet terminology, they're "X - Don't Care".)



      I could do an if, and test if the value equals 207, 223, 239, or 255, but that seems messy and like there would be an easier (and cleaner to read) way.



      Edit to add: I've looked at Bit Operators, and .. something is telling me I should/could be using one of those. But I'm having a difficult time walking through which one would help me.



      Thanks!
      -Mike







      arduino-mega bit






      share|improve this question







      New contributor




      Coyttl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Coyttl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Coyttl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 hours ago









      Coyttl

      133




      133




      New contributor




      Coyttl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Coyttl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Coyttl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You need to use boolean arithmetic. Use the AND operator (&) to combine the incoming value AND the "mask" value (the value of the bits you do care about) and see if it equals the mask bit:



          byte wanted = 0b11001110; // 207
          byte incoming = 223; // for example 11011111

          if ((wanted & incoming) == wanted) {
          // whatever
          }


          The AND operator compares each bit of the bytes in turn. If they are BOTH 1 then the result is 1. Otherwise the result is 0. So the above gives:



          wanted:    11001110
          incoming: 11011111
          AND: 11001110


          And the result matches "wanted". If you had some other value for incoming it could look like this:



          wanted:    11001110
          incoming: 01011011
          AND: 01001010


          And you can see that the result doesn't match.






          share|improve this answer





















          • AH! Okay, that makes sense - I would want my 'mask' value to have bits set to '1' I care about, and '0' I don't. This would effectively force the 'don't care' bits to 0.
            – Coyttl
            12 mins ago


















          up vote
          0
          down vote













          There are ways to mask off the don't care bits.
          Say your example, 0b11xx1111.
          Then



          incomingByte = Serial.read();
          incomingByte = incomingByte || 0b00110000; // make 4, 5 high
          if (incomingByte = 0b11111111){
          // got all 1s, do something
          }
          else {
          // some 0s received, do something else
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("schematics", function () {
            StackExchange.schematics.init();
            });
            }, "cicuitlab");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "540"
            };
            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',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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
            });


            }
            });






            Coyttl is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f58701%2ftesting-conditions-on-port-bytes-ignoring-certain-bits%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








            up vote
            4
            down vote



            accepted










            You need to use boolean arithmetic. Use the AND operator (&) to combine the incoming value AND the "mask" value (the value of the bits you do care about) and see if it equals the mask bit:



            byte wanted = 0b11001110; // 207
            byte incoming = 223; // for example 11011111

            if ((wanted & incoming) == wanted) {
            // whatever
            }


            The AND operator compares each bit of the bytes in turn. If they are BOTH 1 then the result is 1. Otherwise the result is 0. So the above gives:



            wanted:    11001110
            incoming: 11011111
            AND: 11001110


            And the result matches "wanted". If you had some other value for incoming it could look like this:



            wanted:    11001110
            incoming: 01011011
            AND: 01001010


            And you can see that the result doesn't match.






            share|improve this answer





















            • AH! Okay, that makes sense - I would want my 'mask' value to have bits set to '1' I care about, and '0' I don't. This would effectively force the 'don't care' bits to 0.
              – Coyttl
              12 mins ago















            up vote
            4
            down vote



            accepted










            You need to use boolean arithmetic. Use the AND operator (&) to combine the incoming value AND the "mask" value (the value of the bits you do care about) and see if it equals the mask bit:



            byte wanted = 0b11001110; // 207
            byte incoming = 223; // for example 11011111

            if ((wanted & incoming) == wanted) {
            // whatever
            }


            The AND operator compares each bit of the bytes in turn. If they are BOTH 1 then the result is 1. Otherwise the result is 0. So the above gives:



            wanted:    11001110
            incoming: 11011111
            AND: 11001110


            And the result matches "wanted". If you had some other value for incoming it could look like this:



            wanted:    11001110
            incoming: 01011011
            AND: 01001010


            And you can see that the result doesn't match.






            share|improve this answer





















            • AH! Okay, that makes sense - I would want my 'mask' value to have bits set to '1' I care about, and '0' I don't. This would effectively force the 'don't care' bits to 0.
              – Coyttl
              12 mins ago













            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            You need to use boolean arithmetic. Use the AND operator (&) to combine the incoming value AND the "mask" value (the value of the bits you do care about) and see if it equals the mask bit:



            byte wanted = 0b11001110; // 207
            byte incoming = 223; // for example 11011111

            if ((wanted & incoming) == wanted) {
            // whatever
            }


            The AND operator compares each bit of the bytes in turn. If they are BOTH 1 then the result is 1. Otherwise the result is 0. So the above gives:



            wanted:    11001110
            incoming: 11011111
            AND: 11001110


            And the result matches "wanted". If you had some other value for incoming it could look like this:



            wanted:    11001110
            incoming: 01011011
            AND: 01001010


            And you can see that the result doesn't match.






            share|improve this answer












            You need to use boolean arithmetic. Use the AND operator (&) to combine the incoming value AND the "mask" value (the value of the bits you do care about) and see if it equals the mask bit:



            byte wanted = 0b11001110; // 207
            byte incoming = 223; // for example 11011111

            if ((wanted & incoming) == wanted) {
            // whatever
            }


            The AND operator compares each bit of the bytes in turn. If they are BOTH 1 then the result is 1. Otherwise the result is 0. So the above gives:



            wanted:    11001110
            incoming: 11011111
            AND: 11001110


            And the result matches "wanted". If you had some other value for incoming it could look like this:



            wanted:    11001110
            incoming: 01011011
            AND: 01001010


            And you can see that the result doesn't match.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 hours ago









            Majenko

            65.8k42874




            65.8k42874












            • AH! Okay, that makes sense - I would want my 'mask' value to have bits set to '1' I care about, and '0' I don't. This would effectively force the 'don't care' bits to 0.
              – Coyttl
              12 mins ago


















            • AH! Okay, that makes sense - I would want my 'mask' value to have bits set to '1' I care about, and '0' I don't. This would effectively force the 'don't care' bits to 0.
              – Coyttl
              12 mins ago
















            AH! Okay, that makes sense - I would want my 'mask' value to have bits set to '1' I care about, and '0' I don't. This would effectively force the 'don't care' bits to 0.
            – Coyttl
            12 mins ago




            AH! Okay, that makes sense - I would want my 'mask' value to have bits set to '1' I care about, and '0' I don't. This would effectively force the 'don't care' bits to 0.
            – Coyttl
            12 mins ago










            up vote
            0
            down vote













            There are ways to mask off the don't care bits.
            Say your example, 0b11xx1111.
            Then



            incomingByte = Serial.read();
            incomingByte = incomingByte || 0b00110000; // make 4, 5 high
            if (incomingByte = 0b11111111){
            // got all 1s, do something
            }
            else {
            // some 0s received, do something else
            }





            share|improve this answer

























              up vote
              0
              down vote













              There are ways to mask off the don't care bits.
              Say your example, 0b11xx1111.
              Then



              incomingByte = Serial.read();
              incomingByte = incomingByte || 0b00110000; // make 4, 5 high
              if (incomingByte = 0b11111111){
              // got all 1s, do something
              }
              else {
              // some 0s received, do something else
              }





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                There are ways to mask off the don't care bits.
                Say your example, 0b11xx1111.
                Then



                incomingByte = Serial.read();
                incomingByte = incomingByte || 0b00110000; // make 4, 5 high
                if (incomingByte = 0b11111111){
                // got all 1s, do something
                }
                else {
                // some 0s received, do something else
                }





                share|improve this answer












                There are ways to mask off the don't care bits.
                Say your example, 0b11xx1111.
                Then



                incomingByte = Serial.read();
                incomingByte = incomingByte || 0b00110000; // make 4, 5 high
                if (incomingByte = 0b11111111){
                // got all 1s, do something
                }
                else {
                // some 0s received, do something else
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 51 mins ago









                CrossRoads

                9437




                9437






















                    Coyttl is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    Coyttl is a new contributor. Be nice, and check out our Code of Conduct.













                    Coyttl is a new contributor. Be nice, and check out our Code of Conduct.












                    Coyttl is a new contributor. Be nice, and check out our Code of Conduct.
















                    Thanks for contributing an answer to Arduino Stack Exchange!


                    • 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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2farduino.stackexchange.com%2fquestions%2f58701%2ftesting-conditions-on-port-bytes-ignoring-certain-bits%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

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    Alexandru Averescu

                    Trompette piccolo