add without carry

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hugh

    add without carry

    I would like to perform an addition without carrying of two integers...
    I've got no idea how to do this in python, although I've been using it
    for web/cgi/db work for a few years now.

    Any help would be great.

    Hugh

  • Marc 'BlackJack' Rintsch

    #2
    Re: add without carry

    In <1158308274.680 123.100070@i42g 2000cwa.googleg roups.com>, Hugh wrote:
    I would like to perform an addition without carrying of two integers...
    I've got no idea how to do this in python, although I've been using it
    for web/cgi/db work for a few years now.
    Your description is a bit vague. What is `without carrying`? Do you want
    to limit the result to a given bit length with a "wrap around" behavior if
    the number gets too large to fit into those bits? Then this should do the
    trick:

    a = (b + c) & (2**bits - 1)

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • John Machin

      #3
      Re: add without carry


      Hugh wrote:
      I would like to perform an addition without carrying of two integers...
      I've got no idea how to do this in python, although I've been using it
      for web/cgi/db work for a few years now.
      >
      In multiword addition in assembly language, one uses a normal ADD
      instruction on the lowest-order pair of words, and an ADC (or ADDC or
      whatever) i.e add *with* carry which adds in the carry bit from the
      previous operation for each subsequent pair of words. IOW, addition
      *without* carrying is normal, and I don't recall hearing the expression
      before ......

      Do you possibly mean: add two 32-bit integers such the result fits in
      32 bits i.e. discard the generated carry bit, keep the low-order
      32-bits?

      If so, that's simply (a + b) & 0xFFFFFFFF (in Python as well as a few
      other languages).

      If you meant a different number of bits, well the answer for 8 bits
      would be (a + b) & 0xFF.

      Otherwise, you need to provide some examples of two integers and what
      "addition without carrying" produces.

      HTH,
      John

      Comment

      • Hugh

        #4
        Re: add without carry

        Sorry, here's an example...

        5+7=12

        added without carrying, 5+7=2

        i.e the result is always less than 10

        I've been thinking some more about this and my brain is starting to
        work something out... I just wondered if there was a function in python
        math to do this automatically.. .

        Hugh

        Comment

        • MonkeeSage

          #5
          Re: add without carry

          Hugh wrote:
          Sorry, here's an example...
          >
          5+7=12
          >
          added without carrying, 5+7=2
          >
          i.e the result is always less than 10
          def add(a, b, c=10):
          an = a + b
          if an >= c:
          an -= c
          return an

          add(5, 7) # = 2

          ?

          Regards,
          Jordan

          Comment

          • Bryan Olson

            #6
            Re: add without carry

            Hugh wrote:
            Sorry, here's an example...
            >
            5+7=12
            >
            added without carrying, 5+7=2
            >
            i.e the result is always less than 10
            Are you looking for bitwise exclusive or? In Python it's
            the '^' operator. For example:

            print 5 ^ 7


            --
            --Bryan

            Comment

            • Hugh

              #7
              Re: add without carry

              Thankyou everyone this gives me something to work with.

              Hugh

              Comment

              • Peter Otten

                #8
                Re: add without carry

                Hugh wrote:
                Sorry, here's an example...
                >
                5+7=12
                >
                added without carrying, 5+7=2
                >
                i.e the result is always less than 10
                >
                I've been thinking some more about this and my brain is starting to
                work something out... I just wondered if there was a function in python
                math to do this automatically.. .
                >>(5 + 7) % 10
                2

                In this context '%' is called 'modulo operator'. What John and Marc
                suggested is basically the same operation for the special case of binary
                numbers, i. e.

                a % b == a & (b-1)

                if a >= 0 and b == 2**N.

                Peter

                Comment

                • Hugh

                  #9
                  Re: add without carry

                  Peter,

                  That was what I was thinking along the lines of, It's been two years
                  since I finished my CS degree and working in mechanical engineering
                  means I've nearly forgotten it all! :(

                  Thanks, I'll write a function in my app to handle this...

                  Hugh
                  >(5 + 7) % 10
                  2
                  >
                  In this context '%' is called 'modulo operator'. What John and Marc
                  suggested is basically the same operation for the special case of binary
                  numbers, i. e.
                  >
                  a % b == a & (b-1)
                  >
                  if a >= 0 and b == 2**N.
                  >
                  Peter

                  Comment

                  • Bruno Desthuilliers

                    #10
                    Re: add without carry

                    Hugh wrote:
                    Sorry, here's an example...
                    >
                    5+7=12
                    >
                    added without carrying, 5+7=2
                    >
                    i.e the result is always less than 10
                    I've been thinking some more about this and my brain is starting to
                    work something out...
                    No need to think too long to come up with the most possibly Q&D solution:

                    res = int(str(5 + 7)[-1])

                    Like it ?-)

                    --
                    bruno desthuilliers
                    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                    p in 'onurb@xiludom. gro'.split('@')])"

                    Comment

                    • Bruno Desthuilliers

                      #11
                      Re: add without carry

                      Bryan Olson wrote:
                      Hugh wrote:
                      >Sorry, here's an example...
                      >>
                      >5+7=12
                      >>
                      >added without carrying, 5+7=2
                      >>
                      >i.e the result is always less than 10
                      >
                      Are you looking for bitwise exclusive or? In Python it's
                      the '^' operator. For example:
                      >
                      print 5 ^ 7
                      >
                      >
                      >>10 ^ 21
                      31

                      Not really "less than 10"...

                      --
                      bruno desthuilliers
                      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                      p in 'onurb@xiludom. gro'.split('@')])"

                      Comment

                      • Christophe

                        #12
                        Re: add without carry

                        Bruno Desthuilliers a écrit :
                        Bryan Olson wrote:
                        >Hugh wrote:
                        >>Sorry, here's an example...
                        >>>
                        >>5+7=12
                        >>>
                        >>added without carrying, 5+7=2
                        >>>
                        >>i.e the result is always less than 10
                        >Are you looking for bitwise exclusive or? In Python it's
                        >the '^' operator. For example:
                        >>
                        > print 5 ^ 7
                        >>
                        >>
                        >>>10 ^ 21
                        31
                        >
                        Not really "less than 10"...
                        But you must use numbers smaller than 10 as input! Still :
                        >>8 ^ 2
                        10

                        :D

                        Comment

                        • Bryan Olson

                          #13
                          Re: add without carry

                          Bruno Desthuilliers wrote:
                          Bryan Olson wrote:
                          >Hugh wrote:
                          >>Sorry, here's an example...
                          >>>
                          >>5+7=12
                          >>>
                          >>added without carrying, 5+7=2
                          >>>
                          >>i.e the result is always less than 10
                          >Are you looking for bitwise exclusive or? In Python it's
                          >the '^' operator. For example:
                          >>
                          > print 5 ^ 7
                          >>
                          >>
                          >>>10 ^ 21
                          31
                          >
                          Not really "less than 10"...
                          I had little idea what he meant by that. My guess was that
                          no bit clear in an operand could be set in the result.
                          XOR is also known as mod-2 addition. It's what you get
                          if you add each bit independently, dropping carries, so
                          I thought it might be what he was looking for.
                          Unfortunately the bitwise mod-2 addition of 5 and 7 gives
                          the same result as mod-10 addition. Oh well.


                          --
                          --Bryan

                          Comment

                          • Jon Ribbens

                            #14
                            Re: add without carry

                            In article <450a7dec$0$146 61$626a54ce@new s.free.fr>, Bruno Desthuilliers wrote:
                            Hugh wrote:
                            >Sorry, here's an example...
                            >>
                            >5+7=12
                            >>
                            >added without carrying, 5+7=2
                            >>
                            >i.e the result is always less than 10
                            >
                            >I've been thinking some more about this and my brain is starting to
                            >work something out...
                            >
                            No need to think too long to come up with the most possibly Q&D solution:
                            >
                            res = int(str(5 + 7)[-1])
                            Am I missing something subtle in the question or is there some reason
                            that nobody has posted the correct solution:

                            (a + b) % 10

                            ?

                            Comment

                            • Bruno Desthuilliers

                              #15
                              Re: add without carry

                              Christophe wrote:
                              Bruno Desthuilliers a écrit :
                              >Bryan Olson wrote:
                              >>Hugh wrote:
                              >>>Sorry, here's an example...
                              >>>>
                              >>>5+7=12
                              >>>>
                              >>>added without carrying, 5+7=2
                              >>>>
                              >>>i.e the result is always less than 10
                              >>Are you looking for bitwise exclusive or? In Python it's
                              >>the '^' operator. For example:
                              >>>
                              >> print 5 ^ 7
                              >>>
                              >>>
                              >>>>10 ^ 21
                              >31
                              >>
                              >Not really "less than 10"...
                              >
                              But you must use numbers smaller than 10 as input! Still :
                              >
                              >>>8 ^ 2
                              10
                              :D
                              Still fails:
                              >>8 ^ 7
                              15

                              Sorry !-p



                              --
                              bruno desthuilliers
                              python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                              p in 'onurb@xiludom. gro'.split('@')])"

                              Comment

                              Working...