All Arithmatic operations using Bitwise operators only

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aftabpasha
    New Member
    • Oct 2008
    • 32

    All Arithmatic operations using Bitwise operators only

    Is it possible to perform all arithmetic operations (+,-,*,/) using Bitwise operations only. Please consider signed numbers also.
    Thank You.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Of course. That's how the processor does it.

    Comment

    • Aftabpasha
      New Member
      • Oct 2008
      • 32

      #3
      @weaknessforcat s

      Yes, I have a method for addition and subtraction given below

      Code:
      int add(int a, int b)
      { 
                 do
                 {
                            a=a^b;
                            b=(a^b)&b;
                            b=b<<1;
                 } while(b);
      
                 return(a);
      }
      Basically using this method, one can perform Multiplication and Division operations.
      Now, is there any other specialized methods for Multiplication and Division operations? e.g. (num<<1) is like (num*2) and (num>>2) is same as (num/2).
      Here we are not using the addition or subtraction operations, but directly obtaining results of Multiplication and Division operations. Please explain any such method.
      Please give example of processors methods of these operations(+,-,*,/).
      Thank You.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I don't see a question in your reply. In any case, I cannot provide solutions, act as a tutor or do research for you.

        Please read the posting guidelines.

        Comment

        • Bassem
          Contributor
          • Dec 2008
          • 344

          #5
          Hi Aftabpasha,

          It is true that you can perform all arithmetic operations using the two basics operations + and -.
          I have studied Z80 Microprocessor by mad by Zilog, it offers only ADD, SUB, INC (increment), and DEC (decrement) arithmetical operations (beside Logic operations), we typed programs in assembly to make Multiplication and Division, and what amazing is that it still produced and successes until now in many new and complicated applications.


          the next Microprocessor generation 8086 provides MUL and DIV operations, and an improvement in H/W (Hardware) like increasing the addressing capacity, data bus 16 bit and pipelining etc.


          But nowadays CPUs with high Memory capacity and provide very complex operations like Log, antiLog, tan, cos, .... and that is what you use in home and me and everyone else.
          Last edited by Bassem; Dec 27 '08, 02:23 AM. Reason: for make it more clear

          Comment

          • Bassem
            Contributor
            • Dec 2008
            • 344

            #6
            Here is Example


            Multiplication of two numbers
            int MULT(int x, int y)
            {
            int n = x;
            while (n) {
            x += y;
            n--;
            }
            return y;
            }

            Division of two numbers
            int DIV(int x, int y)
            {
            int n = 0;
            while (x > y) {
            x -= y;
            n++;
            }
            return n; // truncate fraction
            }

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Is this assignment for a programming class or a Boolean algebra course? You should start with addition: google the term "half adder". You can add two binary numbers by stringing together enough half adders. Once you have addition working it is simple to get subtraction: google the term "two's complement". Multiplication is repeated addition; division is repeated subtraction.

              Comment

              • Bassem
                Contributor
                • Dec 2008
                • 344

                #8
                I worked on a personal Microprocessor Simulator last year, where i used basic logic gates (AND, OR, XOR) to perform all mathematical operations provided by the MP using Boolean to represent data in registers.

                Introduce a big piece of code may confuse or tiered, so i expected these methods to declare the way of thinking, not to explain the whole idea.

                Regards

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by Bassem
                  Here is Example
                  Don't do it that way; it takes ages to complete; the Russian Peasant Method is more clever:

                  Code:
                  int mul(int x, int y) {
                     int p;
                     for (p= 0; x; x>>= 1, y<<= 1)
                        if (x&1) p+= y;
                     return p;
                  }
                  kind regards,

                  Jos

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Bassem, your multiplication algorithm is wrong you return y which is unchanged by the function.

                    Everyone, you are left shifting a signed value. For C (and I assume C++ but I don't actually know) this is a platform defined operation, that is different platforms may implement it differently producing different results (normally depending of whether the platform performs an arithmetic or logical shift).

                    For portability it is only safe to shift unsigned values.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by Banfa
                      Bassem, your multiplication algorithm is wrong you return y which is unchanged by the function.

                      Everyone, you are left shifting a signed value. For C (and I assume C++ but I don't actually know) this is a platform defined operation, that is different platforms may implement it differently producing different results (normally depending of whether the platform performs an arithmetic or logical shift).

                      For portability it is only safe to shift unsigned values.
                      You still must have eggnog in your brains ;-) A left shift is a logical shift to the left; a right shift shifts in the sign bit (by definition) if an artithmetic shift is used with a signed int, otherwise a zero bit is shifted in (a logical shift). But you're right: an unsigned shift has always been more 'natural' to me as well ;-)

                      kind regards,

                      Jos

                      Comment

                      • Aftabpasha
                        New Member
                        • Oct 2008
                        • 32

                        #12
                        Thank you all for your replies. So, from these posts, I think, one can say that there can be different methods for performing the basic arithmetic operations using bit-wise operators on different machines, and therefore it is necessary to know how certain basic bit-wise operations are performed on different platforms before using them in coding. Otherwise it is better to use normal arithmetic operators (+,-,*,/), specially when you want platform-independent code.
                        Thank you all once again.
                        Kind Regards.

                        Comment

                        • donbock
                          Recognized Expert Top Contributor
                          • Mar 2008
                          • 2427

                          #13
                          It is _always_ better to use normal arithmetic operators to perform arithmetic functions. The exceptions to this simple rule are so exceptional that you're unlikely to ever run into them.
                          Originally posted by Aftabpasha
                          Thank you all for your replies. So, from these posts, I think, one can say that there can be different methods for performing the basic arithmetic operations using bit-wise operators on different machines, and therefore it is necessary to know how certain basic bit-wise operations are performed on different platforms before using them in coding. Otherwise it is better to use normal arithmetic operators (+,-,*,/), specially when you want platform-independent code.
                          Thank you all once again.
                          Kind Regards.

                          Comment

                          • Bassem
                            Contributor
                            • Dec 2008
                            • 344

                            #14
                            Thanks a lot Banfa,
                            I ment to return x not y.

                            As you said the platform who provides the operations, we just use.

                            Josah, your idea is clear using Half-Adder or Full-Adder, and that leads me to ask why Bytes.com have no community for Microprocessors / Microcontroller s ?

                            It may be replaced to suggestions (Dept.) - if it's correct to say- Thanks anyway,
                            I get widely help from your (all of you) answers for many questions other put.

                            Regards for all,
                            Bassem

                            Comment

                            • Aftabpasha
                              New Member
                              • Oct 2008
                              • 32

                              #15
                              Thank you all. Donbock, I'll remember your advice.

                              Regards,
                              Aftab

                              Comment

                              Working...