LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-21-2015, 07:44 AM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
Binary addition/subtraction


I’m wondering about the fine logical points in the evaluation of the following example of binary manipulation.
The following monograph on “Representing signed integer numbers inside a computer”
http://www.mathcs.emory.edu/~cheung/Courses/561/Syllabus/1-Intro/2-data-repr/signed.html
gives the following example for “subtracting 2’s complement numbers”. [Sorry that I can't figure out how to post this in a properly formatted way as an arithmetic operation]

Example: subtract -9 from +5 in binary format:
00000101 - 11110111 = 00001110

It seems that, in order to proceed with the above calculation, one must first evaluate the one’s complement of the ‘negative’ binary number so that it is now a ‘positive’ representation of a binary.
(I'm aware that the unary one's complement operator has precedence over the addition/subtraction operator).
What confuses me is that to get the correct binary result, I must still treat that number as a negative after employing the complement operator, so that the operation of subtracting a ‘negative’ becomes, in effect, an addition of the two numbers.
Is there a simpler way of viewing what’s going on here that will help un-rattle my brain?
 
Old 03-21-2015, 10:20 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by atlantis43 View Post
It seems that, in order to proceed with the above calculation, one must first evaluate the one’s complement of the ‘negative’ binary number so that it is now a ‘positive’ representation of a binary.
I don't know where you got that idea, but it's incorrect.

Quote:
(I'm aware that the unary one's complement operator has precedence over the addition/subtraction operator).
If you are talking about the C unary complement operator precedence, it's completely irrelevant.

Quote:
What confuses me is that to get the correct binary result, I must still treat that number as a negative after employing the complement operator, so that the operation of subtracting a ‘negative’ becomes, in effect, an addition of the two numbers.
Subtracting any number (negative or positive) is equivalent to adding its negation, regardless of what base you're using. So yes, subtracting a negative is equivalent to adding the positive. Again, nothing to do with binary in particular.
 
Old 03-21-2015, 10:29 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Maybe you should do some example calculations with 2-bit numbers:

Code:
unsigned interpretation: 00=0, 01=1, 10=2, 11=3
signed interpretation: 10=-2, 11=-1, 00=0, 01=1

00-10=10 -- unsigned: 0-2 = 2 (!), signed: 0 - (-2) = -2 (!))
00-01=11 -- unsigned: 0-1 = 3 (!), signed: 0 -   1  = -1
00-11=01 -- unsigned: 0-3 = 1 (!), signed: 0 - (-1) =  1

01-10=11 -- unsigned: 1-2 = 3 (!), signed: 1 - (-2) = -1 (!)
01-11=10 -- unsigned: 1-3 = 2 (!), signed: 1 - (-1) = -2 (!)

10-01=01 -- unsigned: 2-1 = 1    , signed: (-2) -   1  =  1 (!)
10-11=11 -- unsigned: 2-3 = 3 (!), signed: (-2) - (-1) = -1

11-01=10 -- unsigned: 3-1 = 2    , signed: (-1) -   1  = -2
11-10=01 -- unsigned: 3-2 = 1    , signed: (-1) - (-2) =  1
(!) marks the over-/underflow

Last edited by NevemTeve; 03-21-2015 at 02:13 PM. Reason: bugfix -- thank you, atlantis43 and genss
 
1 members found this post helpful.
Old 03-21-2015, 12:20 PM   #4
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Maybe you should do some example calculations with 2-bit numbers:
Code:
unsigned interpretation: 00=0, 01=1, 10=2, 11=3
signed interpretation: 10=-2, 11=-1, 00=0, 11=1
Before I go further with your example, did you mean to write:
Quote:
signed interpretation: 10=-2, 11=-1, 00=0, 11=1 ?
--or am I missing something fundamental about the difference between signed & unsigned?
[2 PM: Oops! I see that I'm not really understanding your reference to "signed interpretation" at all!]
I would add that other examples in the cited link were obvious: only the example I listed caused my dilemma.

Last edited by atlantis43; 03-21-2015 at 01:14 PM.
 
Old 03-21-2015, 01:35 PM   #5
genss
Member
 
Registered: Nov 2013
Posts: 744

Rep: Reputation: Disabled
Quote:
Originally Posted by atlantis43 View Post
Before I go further with your example, did you mean to write:
did you mean "01=1" ?
 
Old 03-21-2015, 01:57 PM   #6
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by genss View Post
did you mean "01=1" ?
No, I 'bolded' what I meant, as I thought that the 'significant' digit being '1' in '11' would indicate 'negative' in a 'signed' number. I think that's what's confusing me in the term "signed interpretation", where 11 is listed as equal to both -1 and +1.
At my level of understanding, I only know such things as that an unsigned byte can represent (digital) 0-255, and a signed byte can represent (digital) -128 to + 127.
 
Old 03-21-2015, 02:15 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
I've fixed the bug, thank you guys.
 
1 members found this post helpful.
Old 03-21-2015, 03:00 PM   #8
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
I've fixed the bug, thank you guys.
That clarified, now I'll try seeing if I can comprehend the principle from your two-bit examples.
 
Old 03-24-2015, 02:36 PM   #9
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Subtracting any number (negative or positive) is equivalent to adding its negation, regardless of what base you're using. So yes, subtracting a negative is equivalent to adding the positive. Again, nothing to do with binary in particular.
Just to understand what goes on in the processor with complements, etc, I'm wondering how the processor would, for a = 27 and b = -34, calculate (a) - (b). Would it not first have to convert b into it's 2's complement, or would the processor be able to evaluate that the double negative represents mere addition. If not the latter, then I presume that it would process the 2's complement twice in the above calculation, for subsequent addition?
 
Old 03-24-2015, 02:57 PM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by atlantis43 View Post
I'm wondering how the processor would, for a = 27 and b = -34, calculate (a) - (b). Would it not first have to convert b into it's 2's complement,
No, the binary algorithms for addition and subtraction work independent of the signed/unsigned interpretation.

That said, it's possible the processor might implement subtraction internally by performing negation followed by addition (maybe because it saves silicon). But then it would always perform the negation, even if b is "positive".

Quote:
If not the latter, then I presume that it would process the 2's complement twice in the above calculation, for subsequent addition?
Not sure what "subsequent addition" you refer to here.
 
Old 03-24-2015, 03:02 PM   #11
genss
Member
 
Registered: Nov 2013
Posts: 744

Rep: Reputation: Disabled
a cpu register wraps around
that's why 2's complement

lets say 1111 is -1
adding 1 to it would cause it to wrap arround, giving 0000

same for subtraction
 
Old 03-24-2015, 05:48 PM   #12
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Not sure what "subsequent addition" you refer to here.
ie, first to get the complement of the subtrahend, and 'subsequently' add it to the minuend to get the result.
 
Old 03-24-2015, 07:58 PM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by atlantis43 View Post
ie, first to get the complement of the subtrahend, and 'subsequently' add it to the minuend to get the result.
Oh, I thought "subsequent addition" implied more than 1 addition. But now I'm not sure what you meant by "process the 2's complement twice". Supposing subtraction is implemented like
Code:
     +------------------------------+
     |    subtract                  |
     |               +----------+   |
a ---|---------------|          |   |
     |  +--------+   |  add     |---|---- (a - b)
b ---|--| negate |---|          |   |
     |  +--------+   +----------+   |
     |                              |
     +------------------------------+
b is only negated once.
 
Old 03-25-2015, 01:13 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Sometimes the CPU doesn't have "signed-add/sub" and "unsigned-add/sub" operations, it simply performs binary addition/subtraction, then sets two flags, one for 'unsigned overflow' and one for 'signed overflow' (these are not the same, see the (!) marks in post #3).
 
Old 03-26-2015, 07:32 AM   #15
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by atlantis43 View Post
Example: subtract -9 from +5 in binary format:
00000101 - 11110111 = 00001110

It seems that, in order to proceed with the above calculation, one must first evaluate the ones complement of the negative binary number so that it is now a positive representation of a binary
Wrong at that very point.

11110111 is TWO's complement representing -9

Why?

00001001 is +9
One's complement of that becomes: 11110110
To make it Two's complement you add binary one to the One's complement, so it becomes: 11110111

Now to convert that negative number back to positive, thus making it so you would be adding a positive versus subtracting a negative, you take the reverse of that Two's complement.

To do that you first take the One's complement, which is inversion of all ones and zeros: 00001000
And then you add binary one: 00001001
That result is +9

So now the mathematical equation becomes adding +9 to +5, which becomes +14 --> 00001110

Some things to note:
  1. When converting sign you ALWAYS invert bits first and then add binary one no matter which direction you are going
  2. It is important to understand the size of variables as they are represented in your machine, thus if you call something an integer, "int" you need to understand if that is 8, 16, 32, or 64 bits or something different. You can test this by using something like printf("Size of an integer is: d\n, sizeof(int));
  3. When performing these transformations, be aware of how large the variable is in bits, but also declare your variables as unsigned, be that unsigned char, unsigned short, unsigned int, unsigned long, and so forth. Why? Because "you" are performing the transformations on your own. If you declare a variable as signed, the compiler will treat it as such and there could be complications, best to declare it as unsigned
  4. There are two ways to invert bits, "Exclusive OR" or the Invert Operator:
    1. Exclusive OR is the ^ operator and to invert a variable you would exclusive OR that variable against a same sized variable populated as all 1's. For instance, to invert a byte, 8-bits using XOR you would do something like this: byte-value-result = original-byte-value ^ 0xFF
    2. Invert Operator is the ~ operator and to invert a variable you would apply the invert operator to it like this: byte-value-result = ~original-byte-value
  5. Note that just inverting using either XOR or Invert is only the first part, you next need to add "1" to complete the sign conversion
As others have pointed out, you can do this logically on paper to back up your programming.

I can't emphasize enough to be cautious and use unsigned variables if you're performing the sign conversions with your own programming. Because the language and compiler can deal with signed numbers quite correctly, however in analyzing them you wish to avoid any automatic conversions which would confuse you in your study.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH ... Date /days subtraction Addition and compare em31amit Programming 5 05-24-2012 01:12 PM
Storing multiple options in a single variable (binary addition?) matt_heys Programming 1 04-03-2009 03:38 PM
shell script help - subtraction?? resolute155 Programming 6 10-18-2008 06:33 AM
Arrays subtraction in C nesta Programming 13 09-29-2007 02:36 AM
date subtraction(Urgent) Uday123 Fedora 1 04-05-2006 03:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration