Introduction
To find the sum of two numbers without using arithmetic operators, such as +, -, *, or /, we can use bitwise operators. Specifically, we can use the bitwise XOR ^ and AND & operators along with bitwise shift operations to perform the addition.
Explanation:
- XOR (
^): The XOR operation gives us the sum of two bits without carrying. For example,1 ^ 1 = 0,1 ^ 0 = 1, and0 ^ 0 = 0. - AND (
&): The AND operation gives us the carry. For example,1 & 1 = 1, and1 & 0 = 0. - Left Shift (
<<): The carry obtained from the AND operation needs to be added to the sum obtained from the XOR operation. Since the carry is applied to the next bit position, we left shift it by one.
The process is repeated until there is no carry left.
Problem Statement
Create a C program that:
- Takes two integers as input.
- Finds their sum without using any arithmetic operators.
- Outputs the result.
Solution Steps
- Include the Standard Libraries: Use
#include <stdio.h>for standard input-output functions. - Implement the Sum Function Using Bitwise Operators:
- Use XOR to find the sum without carry.
- Use AND and left shift to calculate the carry.
- Repeat until there is no carry left.
- Create a Main Function: Allow the user to input two numbers and display the sum calculated by the sum function.
C Program to Find the Sum of Two Numbers Without Using Arithmetic Operators
#include <stdio.h>
// Function to add two numbers without using arithmetic operators
int add(int a, int b) {
while (b != 0) {
// Carry now contains common set bits of a and b
int carry = a & b;
// Sum of bits of a and b where at least one of the bits is not set
a = a ^ b;
// Carry is shifted by one so that adding it to a gives the required sum
b = carry << 1;
}
return a;
}
int main() {
int num1, num2, sum;
// Input two integers from the user
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Calculate the sum without using arithmetic operators
sum = add(num1, num2);
// Output the result
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
return 0; // Return 0 to indicate successful execution
}
Explanation
Function to Add Two Numbers Without Arithmetic Operators
- The
addfunction calculates the sum ofaandbwithout using arithmetic operators. - XOR (
a ^ b): Computes the sum ofaandbwithout considering the carry. - AND (
a & b): Computes the carry. - Left Shift (
carry << 1): Shifts the carry to the left so it can be added to the next higher bit position. - The process is repeated in a loop until there is no carry left (
b == 0).
Main Function
- The
mainfunction allows the user to input two integers, calculates their sum using theaddfunction, and displays the result.
Output Example
Example Output:
Enter the first number: 15
Enter the second number: 25
The sum of 15 and 25 is: 40
Example Output (Negative Numbers):
Enter the first number: -10
Enter the second number: 5
The sum of -10 and 5 is: -5
Conclusion
This C program demonstrates how to find the sum of two numbers without using arithmetic operators by leveraging bitwise operations. This approach is both an interesting programming exercise and a practical method in situations where arithmetic operators are restricted or unavailable. The program provides a clear example of how to implement bitwise addition in C programming.