English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

here is an example of what im thinking. I want to get my program to add two binary string(8-bit 2's complement) number and after show the result like this.

Enter your 1st binary number: 00001111
Enter your 2nd binary number: 00001011

Then I will output the following:
ur 1st binary number is 15
and ur 2nd binary is 11
15 + 11 = 26
26 in binary is 00011010
____________________________

But saying all that, i cant get my program to add or subtract binary string(numbers). Can someone please help me.

2007-09-30 08:53:44 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

To solve this problem, I would convert from binary to decimal first, add the two, and then convert back to binary. My brain thinks in decimal, and I'm sure you have trained your's to as well.

To convert from 2's complement to decimal, just invert the bits and add one. Then loop through the bit string excluding the sign bit and find the decimal value. If the first bit was a one, remeber that its decimal representation is negative.

2007-09-30 12:11:09 · answer #1 · answered by Pete 2 · 0 0

Either get your program to do the sum bit by bit starting at the right.
Or
Convert the strings to decimal and store in an integer field. Add the two numbers and then convert the answer back to binary.

The first option will probably be easier to code.

2007-09-30 09:30:00 · answer #2 · answered by AnalProgrammer 7 · 0 0

convert into decimal first then add and reconvert into binary

2007-09-30 19:04:43 · answer #3 · answered by i_am_the_next_best_one 5 · 0 0

fedest.com, questions and answers