• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

bitwise addition of two integers

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stan -

Very cool! GREAT link - thank you!
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
no wonder he is so sad, he hasn't seen this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic