English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Below is an example of adding two numbers without using arithmetic operators.
#include <iostream> #include <cmath> using namespace std; int add(int val1, int val2) { while(val2 != 0) { int c = val1 & val2; val1 = val1 ^ val2; val2 = c << 1; } return val1; } int main() { cout << "The sum of two numbers: " << add(28, 8); return 0; }
Output result
The sum of two numbers: 36
In the above program, the function add() defines two int type parameters. The addition of two numbers is encoded in the add() function
int add(int val1, int val2) { while(val2 != 0) { int c = val1 & val2; val1 = val1 ^ val2; val2 = c << 1; } return val1; }
Print the result by calling the function add() in the main() function
cout << "The sum of two numbers: " << add(28, 8);