Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 03-01-2011, 10:03 PM   #1
Solid State Member
 
Join Date: Jun 2010
Posts: 8
Default 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. =\
__________________

styxxxola is offline   Reply With Quote
Old 03-02-2011, 07:19 AM   #2
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 7,999
Default 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.
then add 1
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

so my head agrees with your calculator.



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.



anyway... back to your question.

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."
root is offline   Reply With Quote
Old 03-02-2011, 08:49 AM   #3
Solid State Member
 
Join Date: Jun 2010
Posts: 8
Default 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.
styxxxola is offline   Reply With Quote
Old 03-09-2011, 12:04 PM   #4
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 7,999
Default 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."
root is offline   Reply With Quote
Old 03-09-2011, 05:11 PM   #5
Baseband Member
 
Join Date: Mar 2011
Posts: 23
Default 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
Jack4436 is offline   Reply With Quote
Old 03-09-2011, 09:08 PM   #6
Site Team
 
celegorm's Avatar
 
Join Date: Sep 2006
Posts: 10,713
Send a message via AIM to celegorm
Default 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)
celegorm is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 02:44 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0