Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
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
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
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
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

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
Nit: st_length is really no longer a length but a position in a string.

Rancher
Posts: 3444
33
My approach seems to take a couple of more if statements that originally thought

Bartender
Posts: 3519
150
The assignment says nothing about the use of the shift right operator. For a really short and lazy solution, see the BigInteger class.