diff options
author | Michael Hunteman <michael@michaelted.xyz> | 2022-09-22 10:14:04 -0500 |
---|---|---|
committer | Michael Hunteman <michael@michaelted.xyz> | 2022-09-22 10:14:04 -0500 |
commit | 6522012065712fb0ece31bff9ff10b38a83b10e1 (patch) | |
tree | 743f3c13d08be9f60bad57b74e7a1fe7cc675e89 /SumTwoIntegers.java |
Initial commit
Diffstat (limited to 'SumTwoIntegers.java')
-rw-r--r-- | SumTwoIntegers.java | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/SumTwoIntegers.java b/SumTwoIntegers.java new file mode 100644 index 0000000..acb2590 --- /dev/null +++ b/SumTwoIntegers.java @@ -0,0 +1,30 @@ +import java.lang.*; +import java.util.*; + +class SumTwoIntegers { + public static int getSum(int a, int b) { + int bitmask = 0b1; + int carry = 0b0; + int sum = 0; + + while (bitmask != 0) { + int i = a & bitmask; + int j = b & bitmask; + + sum |= (i ^ j) ^ carry; + carry = (i & j) | ((i | j) & carry); + + bitmask <<= 1; + carry <<= 1; + } + return sum; + } + + public static void main(String[] args) { + int a = 12; + int b = 32; + int ans = getSum(a, b); + System.out.println(ans); + System.out.println(Integer.toBinaryString(ans)); + } +} |