# Simple binary question

#### styxxxola

##### Solid State Member
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. =\

#### root

##### Site Team
Staff member
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
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
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.

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

#### styxxxola

##### Solid State Member
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.

#### root

##### Site Team
Staff member
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

#### Jack4436

##### Baseband Member
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

#### Celegorm

##### Site Team
Staff member
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.