I have this question: " Given a list of 0’s and 1’s representing a number in two’s complement, write a pseudo code algorithm that converts the number into decimal "
I'm stuck in the middle of the code; after making sure that the given number in binary is negative I flipped it (changed every 0 to 1 & every 1 to 0) but then I had to add 1, at this point I faced a problem! I can't manage to write the code in order to add taking into consideration the carries (e.g.: 1+1=0 & carry 1)
Hope my question is clear :)
I really need a quick reply! Thank You in advance.
For just positive numbers you would need too:
1. Read in the whole number and store it internally.
2. Write out a decimal
For #1 it is simply
Start with a = 0
For each 1 & 0
a = a * 2
Add the 1 or 0 to a.
If number is 0, print 0 and exit
decimal place is number mod 10. Store this number
divide number by 10 (rounding down) to shift it over 1 decimal place
repeat until number is 0
Print numbers in reverse order that you stored them in.