Add two unsigned numbers using bits in C++.

In this problem, we are given two unsigned numbers, and we need to add them using bits in C. Bits are binary digits that can be either 0 or 1. Unsigned numbers are positive numbers represented by these bits. To add two unsigned numbers, we add their bits one by one using binary addition rules.

Syntax

unsigned int addBits(unsigned int a, unsigned int b);

Binary Addition Rules

Binary addition works like decimal addition, but with simpler rules −

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 0 = 1
  • 1 + 1 = 0 with a carry of 1

When there is a carry, we add it to the next bit. For example, if the carry is 1, then adding 1 + 1 + carry (1) gives 1 with another carry of 1.

Binary Addition Example: 5 + 3 = 8 Number 5: 0101 Number 3: 0011 Result 8: 1000 Carries: 1110 Rightmost: 1+1=0 carry 1, Next: 0+1+1=0 carry 1, etc.

Method 1: Using XOR and AND Operations

This approach uses bitwise operations to simulate binary addition. XOR gives the sum without carry, and AND shifted left gives the carry −

#include <stdio.h>

unsigned int addBits(unsigned int a, unsigned int b) {
    while (b != 0) {
        unsigned int carry = a & b;  /* Find carry bits */
        a = a ^ b;                  /* Sum without carry */
        b = carry << 1;             /* Shift carry left */
    }
    return a;
}

int main() {
    unsigned int num1 = 5, num2 = 3;
    printf("Binary of %u: ", num1);
    for (int i = 3; i >= 0; i--) {
        printf("%d", (num1 >> i) & 1);
    }
    printf("<br>");
    
    printf("Binary of %u: ", num2);
    for (int i = 3; i >= 0; i--) {
        printf("%d", (num2 >> i) & 1);
    }
    printf("<br>");
    
    unsigned int result = addBits(num1, num2);
    printf("Sum: %u + %u = %u<br>", num1, num2, result);
    
    printf("Binary result: ");
    for (int i = 3; i >= 0; i--) {
        printf("%d", (result >> i) & 1);
    }
    printf("<br>");
    
    return 0;
}
Binary of 5: 0101
Binary of 3: 0011
Sum: 5 + 3 = 8
Binary result: 1000

Method 2: Manual Bit-by-Bit Addition

This method manually processes each bit position with carry propagation −

#include <stdio.h>

unsigned int addBitsManual(unsigned int a, unsigned int b) {
    unsigned int result = 0;
    unsigned int carry = 0;
    
    for (int i = 0; i < 32; i++) {
        unsigned int bitA = (a >> i) & 1;
        unsigned int bitB = (b >> i) & 1;
        unsigned int sum = bitA + bitB + carry;
        
        result |= (sum & 1) << i;  /* Set result bit */
        carry = sum >> 1;          /* Update carry */
    }
    
    return result;
}

int main() {
    unsigned int num1 = 21, num2 = 27;
    printf("Adding %u and %u using manual bit addition<br>", num1, num2);
    
    unsigned int result = addBitsManual(num1, num2);
    printf("Result: %u<br>", result);
    
    /* Verify with normal addition */
    printf("Verification: %u + %u = %u<br>", num1, num2, num1 + num2);
    
    return 0;
}
Adding 21 and 27 using manual bit addition
Result: 48
Verification: 21 + 27 = 48

Comparison

Method Time Complexity Space Complexity Advantages
XOR and AND O(log n) O(1) Faster, fewer iterations
Manual Bit-by-Bit O(32) = O(1) O(1) Easy to understand, predictable

Key Points

  • XOR operation gives sum without carry: a ^ b
  • AND operation finds carry positions: a & b
  • Left shift propagates carry to next position: carry << 1
  • Manual method processes all 32 bits sequentially

Conclusion

Adding unsigned numbers using bits in C can be achieved through XOR/AND operations or manual bit processing. The XOR approach is more efficient, while manual addition provides better understanding of binary arithmetic fundamentals.

Nishu Kumari
Nishu Kumari

Technical Content Engineer

Updated on: 2026-03-15T12:03:43+05:30

860 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements