   03-01-2011, 09:03 PM #1 Solid State Member   Join Date: Jun 2010 Posts: 8 Simple binary question So I'm trying to learn IA-32 Assembly Language and I have a question about a problem. 11010101 + 01101011 = ??????? My calculator gives me 01000000 (64 Dec.) The first integer has a 1 in the MSB so it's negative right? So I'm suppose to use its two's complement right? So that would mean: 00101011 + 01101011 = 10010110 (-106 Dec.) At this point I don't know what to do. Why are the two answers different? What did I miss? Thanks again. I'm working out of a text book alone so I don't have any programmer friends or professor to ask. =\ __________________ __________________   03-02-2011, 06:19 AM   #2
Site Team

Join Date: Mar 2004
Posts: 8,096 Re: Simple binary question

ok... I just had to re-learn twos compliment (it's been a few years). but I'm glad to say that I can still do it on paper!

you know how two's compliment works? (I'll explain for anyone that doesn't).

the most significant bit is the signed bit. (SB)0000000
(0)xxxxxxx = positive (whatever the seven last bits are)
(1)xxxxxxx = negative (whatever the last 7 bits are after conversion).

I'll use 4 bit numbers to make it a bit simpler.
Code:
```positive    negative
0000 = 0  0000 = 0
0001 = 1  1111 = -1
0010 = 2  1110 = -2
0011 = 3  1101 = -3
0100 = 4  1100 = -4
0101 = 5  1011 = -5
0110 = 6  1010 = -6
0111 = 7  1001 = -7
1000 = -8```
to convert from a two's compliment number to a positive number you need to 'flip all the bits' (change 1 to 0 and 0 to 1), then add 1

e.g -3 = 1101

change that to 0010.
0011

-7 = 1001
= 0110
=0111 = 7

got it? (if not then say so. I'll try to explain a bit better, also try writing it out on paper and practising a bit with smaller numbers first).

so let's look at the numbers that you have
Quote:
 11010101 + 01101011 = ???????
11010101 => 00101010 => 00101011 = 43 (1 + 2 + 8 + 32)
e.g 11010101 = -43

01101011 = 107 (1 + 2 + 8 + 32 + 64) (it is positive so no need for a conversion)

-43 + 107 = 64 = 01000000

Now this is where it get's a bit more interesting
Quote:
 00101011 + 01101011 = 10010110 (-106 Dec.)
00101011 = 43 (1+2+8+32)
01101011 = 107 (1+2+8+32+64)

43 + 107 = 150 = (128+16-4-2) = (10010110)

AND you are correct that 10010110 = -106

10010110 => 01101001 => 01101010 = (2+8+32+64 = 106)

Basically in order to use positive integers over 127 AND use negative numbers (so you need a signed bit) you can't use 8bit integers, you have to move to 16 bit.

what you have when trying to write 150, but you actually write -106 is an overflow error.

to go off on a tangent, this is why 32bit computers can only access a certain amount of ram, you can't count higher than the address space allows.
this is why old hard disks used to have a 127GB limit.
and why windows XP (before the patch to allow big disk support) has a 127GB limit on disks.

it's all to do with the amount of bits used to write down a number.

Quote:
 What did I miss?
you did 43 + 107 (which does equal 150)
what you should have done

(minus)43 + 107.

which does, in either base 2 (binary) or base 10 (decimal) [or any other base]
equal 64
__________________

__________________
I didnt fight my way to the top of the food chain to be a vegetarian
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."   03-02-2011, 07:49 AM #3 Solid State Member   Join Date: Jun 2010 Posts: 8 Re: Simple binary question Ahhhhhhh I see now. Thank you very much! New question! What is the signed representation of the following signed binary numbers? 00101010 I can convert it on my own, I just have a question about it. The MSB is 0 so that means it's a positive signed number right? So I wouldn't need to use its twos compliment to convert it, correct? I feel like it's such a silly question but I'm not comfortable continuing unless I know for sure. >< I'll probably just continue to use this same thread for all of my questions. I'm sure there will be many more as I get deeper into this book. __________________   03-09-2011, 11:04 AM #4 Site Team   Join Date: Mar 2004 Posts: 8,096 Re: Simple binary question hello, I only just saw the new question so sorry for the delay you're correct the MSB is zero which means it's signed positive and no conversion needed, you only need to convert using two's compliment as described above when the signed bit is one. e.g. 00101010 = (+)42 __________________ I didnt fight my way to the top of the food chain to be a vegetarian Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns. "The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."   03-09-2011, 04:11 PM #5 Baseband Member   Join Date: Mar 2011 Posts: 23 Re: Simple binary question There is another very simple way to take a 2's compliment start from the right side write as it is until the first 1 comes after 1st one, flip all other bits its very simple.. not a single calculation See here 2's compliment of 01101000 first write as it until the first one comes that is 1000 from the right side then after first one flip the other bits that is 10011000 hows simple __________________   03-09-2011, 08:08 PM #6 Site Team   Join Date: Sep 2006 Posts: 10,720 Re: Simple binary question I'm just going to toss this out, but are you sure the langauge is using signed integers? If it's unsigned then you'd get 320 instead of the 64 you'd otherwise get. The assembly langauge I used in school had unsigned (uint's) and signed (ints). Obviously this can cause drastically different behavior based upon what you are expecting it to do. Also make sure if/when the list over-flows it doesn't just write off the upper-most bits and leave you with an 8-bit integer of a random value. Just to add as well. THe sum of the original two is a 9-bit number assuming we are unsigned. that means, if we take 320 - 256 (It's the first 9th-bit number), you'd also get 64 which leaves good possibility that your calculator is getting rid of that 9th bit. __________________ __________________ "as a fanboy i refuse to admit it and will pull countless things out of my butt to disprove it" Team Thelegorm! Total Kills: 21 (i iz in uor profile, editsing your sigz)  Thread Tools Search this Thread Show Printable Version Email this Page Search this Thread: Advanced Search Display Modes Linear Mode Switch to Hybrid Mode Switch to Threaded Mode Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules » Recent Threads

All times are GMT -5. The time now is 10:23 PM. 