This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes bitwise addition of two integers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "bitwise addition of two integers" Watch "bitwise addition of two integers" New topic
Author

bitwise addition of two integers

T Chakradhar Reddy
Greenhorn

Joined: Aug 31, 2007
Posts: 3
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.
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Chakradhar -

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?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Stan -

Very cool! GREAT link - thank you!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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!
 
jQuery in Action, 2nd edition
 
subject: bitwise addition of two integers
 
Similar Threads
Operators and Assignments ...
Can anyone help me regarding
Beginner question about bit conversion
operators
bitwise question