Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How can I add two byte[1] + byte[2]?

How can I add two byte[1] + byte[2]?

Scheduled Pinned Locked Moved C#
question
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    merh
    wrote on last edited by
    #1

    Hi, I have two bytes which I need to obtain the sum of them. I have done like this but the result is wrong. int result = byte[1] + byte[2]; How can I do that? Thanks in advanced Merh

    CPalliniC 1 Reply Last reply
    0
    • M merh

      Hi, I have two bytes which I need to obtain the sum of them. I have done like this but the result is wrong. int result = byte[1] + byte[2]; How can I do that? Thanks in advanced Merh

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      merh wrote:

      I have done like this but the result is wrong.

      Sure? How did you check the result? Please post code, inputs and ouput. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      In testa che avete, signor di Ceprano?

      M 1 Reply Last reply
      0
      • CPalliniC CPallini

        merh wrote:

        I have done like this but the result is wrong.

        Sure? How did you check the result? Please post code, inputs and ouput. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        M Offline
        M Offline
        merh
        wrote on last edited by
        #3

        I have an array of bytes. s below is my array and I try to read byte nr 0 and add to nr 1. for(int i=0; i<=1; i++) int result += s[i]; Thanks Merh

        D CPalliniC 2 Replies Last reply
        0
        • M merh

          I have an array of bytes. s below is my array and I try to read byte nr 0 and add to nr 1. for(int i=0; i<=1; i++) int result += s[i]; Thanks Merh

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #4

          What are the values of s[0] and s[1]? What is the value of result produced by the code? What are you expecting result to be?

          You know, every time I tried to win a bar-bet about being able to count to 1000 using my fingers I always got punched out when I reached 4.... -- El Corazon

          1 Reply Last reply
          0
          • M merh

            I have an array of bytes. s below is my array and I try to read byte nr 0 and add to nr 1. for(int i=0; i<=1; i++) int result += s[i]; Thanks Merh

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            do you mean something similar to:

            int i;
            byte[] s ={ 10, 20 };
            int result=0;
            for (i = 0; i <= 1; i++)
            result += s[i];

            though inelegant, I see no error in the above. On the other hand, your code, ss it stands, doesn't even compile. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            In testa che avete, signor di Ceprano?

            M 1 Reply Last reply
            0
            • CPalliniC CPallini

              do you mean something similar to:

              int i;
              byte[] s ={ 10, 20 };
              int result=0;
              for (i = 0; i <= 1; i++)
              result += s[i];

              though inelegant, I see no error in the above. On the other hand, your code, ss it stands, doesn't even compile. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              M Offline
              M Offline
              merh
              wrote on last edited by
              #6

              Yes, I mean something like this int i; byte[] s ={ 10, 20 }; int result=0; for (i = 0; i <= 1; i++) result += s[i];

              CPalliniC 1 Reply Last reply
              0
              • M merh

                Yes, I mean something like this int i; byte[] s ={ 10, 20 }; int result=0; for (i = 0; i <= 1; i++) result += s[i];

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                Well it works fine on my system and since you have provided no inputs and no output I can only guess what is happening on yours. Should I turn ON my Code Project Mind Reader Unit?

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                In testa che avete, signor di Ceprano?

                M 1 Reply Last reply
                0
                • CPalliniC CPallini

                  Well it works fine on my system and since you have provided no inputs and no output I can only guess what is happening on yours. Should I turn ON my Code Project Mind Reader Unit?

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                  M Offline
                  M Offline
                  merh
                  wrote on last edited by
                  #8

                  Thanks it works now on my system too. /Mehran

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups