PDA

View Full Version : How does computer add two integers


moonstone
04-06-2003, 02:29 PM
hi,
i am so far doing some c programming. but there is something i don't know and really wonder...

i know computers see everyhing bitwise, just 0's and 1's... and i think that those 1's and 0's are represented by open or closed circles(i hope i am right).

and i know the main operation computers do is addition. so i wonder, how does computer add two integers. ho does the system in this machine works?

PS:you can send some links where i can get more information.

thans for helps

peace.

Phantom
04-06-2003, 04:37 PM
The computer adds the binary code, and outputs it in decimal form (I beleive, I may be wrong)

moonstone
04-06-2003, 04:52 PM
ok, but how does the computer determine what to do?

for example
4+5 is trivial (100+101=1001)
but what about
4+(-5)
how does computer understeand that the number is negative.
what's the procedure.

i am afraid my question seems so simple and unimportant but i really need to know that.

moonstone
04-06-2003, 04:52 PM
and thanks for your reply btw.

djdante97
04-06-2003, 05:10 PM
first of all, when the processor does operations, the computer doesn't understand that it's a negative number, it's all in how the user/program interperets the data. negative numbers are represnted using 2's compliment at the binary level, which means that the first bit will be a 1. so (using 8 bits instead of 32 because i do'nt feel like writing all of them out) -5 is -00000101, and in 2's compliment it's just 11111011, so when you add 00000100 (4) to it, you get 11111111 which is the 2's compliment of -00000001. to get the 2's compliment is easy, you just flip the bits and add 1. of course there's a lot more to the entire process, like setting the flags (negative, zero, overlflow, carry) but i hope this helps your understanding.