0

how would one manipulate the code below, using pointers?

int add(int a, int b) 
 {
    int result=0;

    int s=0, c=0;        /* result and carry; carry in for bit 0 is 0 */
    /*int bit_a, bit_b;
    int i, n, mag=0;

    for(i=0; i<16; i++) {
        bit_a = ((mask_array[i] & a)?1:0);
        bit_b = ((mask_array[i] & b)?1:0);
        printf("bitA%d: %d  bitB%d: %d\n",i,bit_a,i,bit_b);
        s = gate8(gate0(bit_a,bit_b,c),gate3(bit_a,bit_b,c),
                  gate5(bit_a,bit_b,c),gate6(bit_a,bit_b,c));
        c = gate9(gate0(bit_a,bit_b,c),gate1(bit_a,bit_b,c),
                  gate2(bit_a,bit_b,c),gate4(bit_a,bit_b,c));  /* carry bit forward to next loop */
       /* printf("sum: %d   carry: %d\n",s,c);
        if (i==0) mag = 1;
        else {
            mag = 1;
            for (n=0; n<i; n++) mag = mag * 2;
        }
        if (s > 0) result = result + mag;
    }
    return result;
}
2
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by Ancient Dragon
0

I assume you want to use pointers for mask_array[i]?

Assuming mask_array is an array of integers

int* mask_ptr = mask_array;

for(int i = 0; i < 16; i++, mask_ptr++)
    bit_a = ((*mask_ptr & a)?1:0);       

Another option
bit_a = ((*(mask_array+i) & a)?1:0);

Edited by Ancient Dragon

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.