----- Original Message -----
From: "shashi kiran" <kiran_vee@yahoo.com>
To: "Vijay Kumar R Zanvar" <vijoeyz@hotpop.com>
Sent: Thursday, January 29, 2004 8:34 AM
Subject: About Addition

> hai vijay
>
>
> pls do send me the pgm to add nos with out using + r -
> I tried with Bitwise Operators but I am not able
> to get the Exact Logic
>
> than in adv
>

    Well, it took me good time to think about the solution.
If you can not use + and/or - operators, then the only choice
left is to use bitwise operators.  OK.  Let me give, in
brief, the details about the logic I used:


    I have used XOR and AND bitwise operators, and LOGICAL AND
and LOGICAL OR operators.


        XOR| 0 | 1          AND| 0 | 1
        -----------         -----------
         0 | 0 | 1           0 | 0 | 0
        -----------         -----------
         1 | 1 | 0           1 | 0 | 1

Rules:

1.  1 + 1 = 0       carry = 1  implemented as:

        1 ^ 1 = 0   sum
        1 & 1 = 1   carry

    There would be, actually, three operands in the addition:
    two are the given bits and the third would be the carry
    generated from previous addition.

2.  Observe that a carry is generated, i.e., it is 1, if and
    only if two or more operand bits are 1.  For example:

                     1     2     3     4     5     6
    ---------------------------------------------------
           bit 1  |  0  |  0  |  0  |  0  |  1  |  1  |
           bit 2  |  0  |  0  |  1  |  1  |  1  |  1  |
      prev carry  |  0  |  1  |  0  |  1  |  0  |  1  |
                   ____________________________________
             sum  |  0  |  1  |  1  |  0  |  0  |  1  |
    result carry  |  0  |  0  |  0  |  1  |  1  |  1  |
    ---------------------------------------------------

    Only in the cases 4, 5, and 6, a carry is generated.
    By obeservation, one can see that sum is mere the
    XOR of the operands.

    Following is a minimum working program for the above
explaination.  But, anyway... from where do you get such
weird questions?


/* 
 * add_xor.c   
 * About    -   addition of integers without using + or - operators
 * Author   -   Vijay Kumar R Zanvar
 * Date     -   Jan 30, 2004
 */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#define  DEBUG  0

long
add ( unsigned long a, unsigned long b )
{
    unsigned    sum   = 0,
                n     = 1,      /* bit offset */
                carry = 0,
                bit,  /* sum of two bits, carry ignored */
                a_, b_;

    /* see K&R; page 257 */
    size_t      times = sizeof a * CHAR_BIT;

#if  DEBUG
        printf ( "times: %lu\n", times );
#endif

    while ( times-- )
    {
        a_ = ( (a >> (n-1)) & 1 );
        b_ = ( (b >> (n-1)) & 1 );

        bit   = a_ ^ b_ ^ carry;
        sum  |= bit << (n-1);
        carry = ( a_ && b_ ) || ( a_ && carry ) || ( b_ && carry );
        n++;

#if  DEBUG
        printf ( "a moved: %d  b moved: %d  ", a_, b_ );
        printf ( "bit: %d  ", bit );
        printf ( "sum: %u  ", sum );
        printf ( "carry: %d\n", carry );
#endif
    }
    return (long) sum;
}


int
main ( void )
{
    int      a = 130,
             b = -137;

    printf ( "a: %d  b: %d  \nsum: %ld\n", a, b, add ( a, b ) );
    return EXIT_SUCCESS;
}

--
Vijay Kumar R Zanvar