Add binary as string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Add binary as string

I want to input Binary of two numbers and want to get their sum as int. Example : input : 1011 , 1010 output: 101 expected output: 10101 What is wrong with my code ? code link : https://www.sololearn.com/compiler-playground/cm4RfauB0yXn

27th Sep 2022, 5:44 PM
saurav
saurav - avatar
5 Answers
28th Sep 2022, 12:51 AM
zemiak
+ 2
Problem is the way you use stoi function - you are parsing the input strings as decimal numbers, not binary. Just review stoi function syntax.
28th Sep 2022, 3:11 AM
Emerson Prado
Emerson Prado - avatar
+ 1
because int 1011 is one thousand .. and not 0b1011 //c = c >> 1; //d = d >> 1; c = c / 10; d = d / 10;
28th Sep 2022, 7:34 AM
zemiak
0
Emerson Prado Thanks bro 😊 I got it !
28th Sep 2022, 3:26 AM
saurav
saurav - avatar
- 2
Whaaa I just started coding
28th Sep 2022, 12:36 PM
Kalista Rae Smalls