This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am having difficulty with bitwise addition of 2 integers without using arithmetic operators. My logic is suppose we have to add 3+3=6 in 2's compliment addition the logic is
carry 11 (3)0000 0011 (3)0000 0011 _____________ 0000 0110 (6 ) ------------------------ I have understood the boolean logic. The task is now to write a java program without using any arithmetic operators. I am allowed to use only bitwise operators. The logic i have come up with is to XOR each bit at the end one by one in a for loop and at the end i will capture only the carry bits it should look like 0000 0110 which is the carry generated above. To accomplish this i will use the >> operator to move each bit of first and second operand and test the bits individually. This is my vague logic. Some clues would be of great help. I thought of using arrays to convert the integer into boolean values store the bits in the arrays and use OR XOR and condition statements but seems very lenghty procedure.
If you check out the Sun Online Tutorial you will find a pretty decent explanation of the bitwise operators and their effect; Bruce Eckel's Thinking in Java gives an extensive discussion with lots of examples.
If you're already pretty sure what you need to do, why don't you write the code and see if it works?
Ok, editing this post for the last time, I hope. The first time it had a goofy suggestion that was much worse than the OP. The second time I figured that out and wondered about modeling binary adders. This time, I did that and it was fun. I made a HalfAdder, a FullAdder and a EightBitAdder right off the pictures HERE. The last one looks like ... hope it's not too big a hint. The others are shorter than this.
[ September 04, 2007: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Apr 29, 2007
Very cool! GREAT link - thank you!
Joined: Jan 29, 2003
The adder classes turned out to be tiny. They have an add() method and public variables for sum & carry. Of course private variables and getters would be more correct, but for something this tiny I didn't bother. Show us what you make!