I have this question: " Given a list of 0s and 1s representing a number in twos 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.

For #2,
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.


All Answers

Answers by Expert:

Ask Experts


Artemus Harper


I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.


I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Washington State University

MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2017 All rights reserved.