# Perl program to add two numbers using only bitwise operators
# See https://stackoverflow.com/questions/4068033/add-two-integers-using-only-bitwise-operators
use strict;
use warnings;
# See [id://11135535] (note that use integer subtly affects bit operations in Perl)
use integer;
sub BitAdd1 {
my ($aa, $bb) = @_;
my $carry = $aa & $bb;
my $result = $aa ^ $bb;
while ($carry != 0) {
my $s_carry = $carry << 1;
$carry = $result & $s_carry;
$result ^= $s_carry;
}
return $result;
}
sub BitAdd2 {
my ($aa, $bb) = @_;
$bb == 0 and return $aa;
return BitAdd2($aa ^ $bb, ($aa & $bb) << 1);
}
for my $r ( [0, 1],
[1, -1],
[69, -42],
[42, 69],
[-42, 69],
[-42, -69],
[256, 512],
[123456789, 1],
[2147483647, 1],
[-2147483648, 1] ) {
my $sum0 = $r->[0] + $r->[1];
my $sum1 = BitAdd1($r->[0], $r->[1]);
my $sum2 = BitAdd2($r->[0], $r->[1]);
print "$r->[0] + $r->[1] = $sum0 ($sum1 $sum2)\n";
$sum0 == $sum1 or die "oops 1";
$sum0 == $sum2 or die "oops 2";
$sum1 == $sum2 or die "oops 3";
}
####
0 + 1 = 1 (1 1)
1 + -1 = 0 (0 0)
69 + -42 = 27 (27 27)
42 + 69 = 111 (111 111)
-42 + 69 = 27 (27 27)
-42 + -69 = -111 (-111 -111)
256 + 512 = 768 (768 768)
123456789 + 1 = 123456790 (123456790 123456790)
2147483647 + 1 = 2147483648 (2147483648 2147483648)
-2147483648 + 1 = -2147483647 (-2147483647 -2147483647)
##
##
// C++ Program to add two numbers without using arithmetic operator.
// bitadd.cpp
// Built with : g++ -o bitadd -std=c++11 -Wall -O3 bitadd.cpp
#include
#include
#include
#include
#include
#include
#include
// uncomment one of these
// typedef int32_t my_int;
typedef int64_t my_int;
my_int Add(my_int x, my_int y)
{
// Iterate till there is no carry
while (y != 0) {
// carry now contains common set bits of x and y
my_int carry = x & y;
// Sum of bits of x and y where at least one of the bits is not set
x = x ^ y;
// Carry is shifted by one so that adding it to x gives required sum
y = carry << 1;
}
return x;
}
using my_list_type = std::vector< std::pair >;
int main()
{
my_list_type ops = {
{ 0, 1 },
{ 1, -1 },
{ 69, -42 },
{ 42, 69 },
{ -42, 69 },
{ -42, -69 },
{ 256, 512 },
{ 123456789, 1 },
{ 2147483647, 1 },
{ -2147483648, 1 }
};
my_int sum0, sum1;
std::cout << "sizeof my int = " << sizeof(my_int) << "\n";
for (const auto& c : ops) {
sum0 = c.first + c.second;
sum1 = Add(c.first, c.second);
if (sum0 != sum1) { std::cout << "oops!\n"; }
std::cout << c.first << " + " << c.second << " = " << sum0 << " (" << sum1 << ")\n";
}
return 0;
}
##
##
sizeof my int = 8
0 + 1 = 1 (1)
1 + -1 = 0 (0)
69 + -42 = 27 (27)
42 + 69 = 111 (111)
-42 + 69 = 27 (27)
-42 + -69 = -111 (-111)
256 + 512 = 768 (768)
123456789 + 1 = 123456790 (123456790)
2147483647 + 1 = 2147483648 (2147483648)
-2147483648 + 1 = -2147483647 (-2147483647)