Why it outputs 73 while it should output 0111 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why it outputs 73 while it should output 0111

I also want to use it later in my programme please tell https://code.sololearn.com/cVe1nJ3Q1780/?ref=app

18th Sep 2017, 5:26 PM
Hassan Sajjad
Hassan Sajjad - avatar
7 Answers
+ 13
Using 0 initially, makes it an octal value 111 which has a value of 73. See this table for more info : http://ascii.cl/conversion.htm EDIT - @Kirk Schafer explains it well ^^
18th Sep 2017, 5:29 PM
Dev
Dev - avatar
+ 7
In many languages, numbers starting with 0 are interpreted as octal (base 8). 0111 = 0*8^3 + 1*8^2 + 1*8^1 + 1*8^0 (0 times 8³, 1 times 8², 1 times 8¹, 1 times 8°) 0 + 64 + 8 + 1 = 73
18th Sep 2017, 5:30 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
bitset is really helpful thabks ace
18th Sep 2017, 6:09 PM
Hassan Sajjad
Hassan Sajjad - avatar
+ 3
Or just try : cout<<oct<<l; This will print 111, exactly what your number is... //You see, 0 in a number in C++ makes the compiler think of it as an octal value... If you wish to add like in binary, make a struct instead: struct bin { int val; bool sign; }; // Then you may even overload operators for the same numbers... /* Then you may do : bin a, b; cout << a+b; // Will print result of a + b in binary only... */ Use it then as you wish to...
19th Sep 2017, 4:32 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
so what if I want to use 0111 as it is used in binary addition.as 1st bit of binary shows sign of the number and 0 is use for positive sign
18th Sep 2017, 5:33 PM
Hassan Sajjad
Hassan Sajjad - avatar
+ 2
also tell me the shortest way of finding binary of some decimal number
18th Sep 2017, 5:36 PM
Hassan Sajjad
Hassan Sajjad - avatar
+ 2
ace please explain your answer
18th Sep 2017, 5:57 PM
Hassan Sajjad
Hassan Sajjad - avatar