Problem:
Increment any given integer without using arithmetic operators.
Example : input 345 , output must be 346.
Solution:
Today I came across a problem wherein I was asked to increment any integer without using any arithmetic operators. Below is the explanation and java code to implement such a problem.
Here we will be utilizing the XOR and & bit wise operators. First lets look at XOR and & operators truth tables given below:
Increment any given integer without using arithmetic operators.
Example : input 345 , output must be 346.
Solution:
Today I came across a problem wherein I was asked to increment any integer without using any arithmetic operators. Below is the explanation and java code to implement such a problem.
Here we will be utilizing the XOR and & bit wise operators. First lets look at XOR and & operators truth tables given below:
Below is the java code to solve the problem.
Lets go through the code using a simple example shown below. Our core implementation starts with the invocation of method calc. Suppose lets take the input value to be 7.
7 is represented as 0000 0111. Output must be 8 which is represented as 0000 1000.
Please follow the below images to understand the working or feel free to comment and i will try to answer your query.
7 is represented as 0000 0111. Output must be 8 which is represented as 0000 1000.
Please follow the below images to understand the working or feel free to comment and i will try to answer your query.