Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

right shift operator

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
given two binary numbers represented as strings, prints their sum in binary. The binary strings are comma separated, two per line. The final answer should not have any leading zeroes. In case the answer is zero, just print one zero i.e. 0

Input:
Your program should read lines from standard input. Each line contains two binary strings, separated by a comma and no spaces.

Output:
For each pair of binary numbers print to standard output their binary sum, one per line.

THIS IS THE INPUT:
input 1: 110011,1010
input 2: 11010,00101001

THIS IS MY OUTPUT
out put 1: 111010
output 2: 01011011

THIS IS A DESIRE OUTPUT
output1: 111101
output2: 1000011



 
Saloon Keeper
Posts: 6246
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 26, your program would already have thrown a NPE before you got here to check for null.
Lines 33 & 38, you want to compare using >= 0.
Line 30 -ditto-
Lines 46 & 47 should be outside the loop.
Line 49 should be outside the loop.
 
Rancher
Posts: 3444
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also posted at: http://www.javaprogrammingforums.com/whats-wrong-my-code/40968-shift-left-shift-right-operator-problem.html

I think this is not the right approach.  See discussion at the above link.
The binary digits in the String should not be converted to int values.
 
Carey Brown
Saloon Keeper
Posts: 6246
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:The binary digits in the String should not be converted to int values.


Not sure I agree with this, converting to an int of 0 or 1 makes summing easier. Otherwise you have to handle all combinations of digit characters from str1, str2, and carry.
 
Carey Brown
Saloon Keeper
Posts: 6246
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Norm Radder
Rancher
Posts: 3444
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My approach seems to take a couple of more if statements that originally thought
 
Bartender
Posts: 3519
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The assignment says nothing about the use of the shift right operator. For a really short and lazy solution, see the BigInteger class.
 
Norm Radder
Rancher
Posts: 3444
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there are no requirements for indexing through Strings, use the Integer or Long class's parse  and  toBinary methods depending on the input String's lengths.

I guess BigInteger is the way to go.
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!