Posted on by Kalkicode
Code Bit Logic

Add two numbers using bitwise operators in kotlin

Kotlin program for Add two numbers using bitwise operators. Here problem description and explanation.

``````// Kotlin program for
// Add two numbers using of bitwise operators
{
fun addition(x: Int, y: Int): Unit
{
var a = x;
var b = y;
// Display given numbers
print("\n [(" + a + ") + (" + b + ")] ");
// Auxiliary variable which are store the carry bit result
var carry: Int;
// Executing loop, until b value is not zero
while (b != 0)
{
// Get carry bits of a & b
carry = a and b;
// Sum of bits of a ^ b
a = a xor b;
// Shift the carry bit by one bit in left side
b = carry shl 1;
}
print(" : " + a);
}
}
fun main(args: Array < String > ): Unit
{
// Test Case
}``````

Output

`````` [(8) + (-2)]  : 6
[(5) + (3)]  : 8
[(-3) + (-5)]  : -8``````

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post