Previous in Forum: Solar PV Grid Connected Systems   Next in Forum: Ground (Earthing) to Neutral Voltage
Close
Close
Close
28 comments
Rate Comments: Nested
Commentator

Join Date: Sep 2009
Posts: 59

2's Complement

08/10/2010 1:15 AM

Is two's complement only done on negative numbers?

Example: Find 2's complement on 7

0111 = 7...Nothing done

I do know you invert the bits and add 1. Not sure if that only applies to negative numbers...Please confirm.

Register to Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Good Answers:

These comments received enough positive votes to make them "good answers".

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Guru
Technical Fields - Technical Writing - New Member Engineering Fields - Piping Design Engineering - New Member

Join Date: May 2009
Location: Richland, WA, USA
Posts: 21017
Good Answers: 795
#1

Re: 2's complement

08/10/2010 1:51 AM

I think the 2's complement of a number is considered from a greater power of 2 than needed to express the original number.

Using your example, 7 = 00111. Invert digits to 11000. Add one to get 11001 = 25. Note that 7 + 25 = 32 = 25.

The 2's complement of a number behaves like the negative of the number, notably in modulo arithmetic, but the concept applies just as well to positive numbers.

Even in your example, 0111 inverts to 1000. Add one to get 1001 = 9. Then 7 + 9 = 16 = 24. I don't understand the "nothing done" comment.

__________________
In vino veritas; in cervisia carmen; in aqua E. coli.
Register to Reply
Commentator

Join Date: Sep 2009
Posts: 59
#2
In reply to #1

Re: 2's complement

08/10/2010 2:26 AM

Nothing done means no action required.

Register to Reply Score 1 for Off Topic
Guru
Technical Fields - Technical Writing - New Member Engineering Fields - Piping Design Engineering - New Member

Join Date: May 2009
Location: Richland, WA, USA
Posts: 21017
Good Answers: 795
#3
In reply to #2

Re: 2's complement

08/10/2010 2:32 AM

One of the required actions is to carry out the calculation, as I did.

__________________
In vino veritas; in cervisia carmen; in aqua E. coli.
Register to Reply
Commentator

Join Date: Sep 2009
Posts: 59
#4
In reply to #3

Re: 2's complement

08/10/2010 2:39 AM

The 2's complement of 11 (1011) is 1011. This example sparked the oringal answer to this question.

Register to Reply Score 1 for Off Topic
Guru
Technical Fields - Technical Writing - New Member Engineering Fields - Piping Design Engineering - New Member

Join Date: May 2009
Location: Richland, WA, USA
Posts: 21017
Good Answers: 795
#6
In reply to #4

Re: 2's complement

08/10/2010 3:42 AM

10112 = 1110. Invert digits to get 01002. Add 1 to get 01012 = 510. 11 + 5 = 16 = 24.

So the 2's complement of 1011 is 0101, not 1011. Just do the correct arithmetic.

__________________
In vino veritas; in cervisia carmen; in aqua E. coli.
Register to Reply
Power-User

Join Date: Feb 2010
Posts: 187
Good Answers: 11
#5
In reply to #1

Re: 2's complement

08/10/2010 2:59 AM

First, where is your text book?

Anyway, let me give you one example:

The 2's complement system is used to represent negative numbers using modulus arithmetic and if a 4 bit number is adds to another 4 bit number, the result will be only of 4 bit. Carry if any from the fourth bit will overflow. This is called the modulus arithmetic.

For example:

1100 + 1111 = 1011.

In the 2's complement subtraction, add the 2's complement of the subtrahend to the minuend. If there is a carry out, ignore it. Look at the sign bit. MSB of the sum term.

If the MSB is a 0 the result is positive and is in true binary form. If the MSB is a 1 ( whether there is a carry or no carry at all) the result is negative and is in its 2's complement form. Take 2's complement to find its magnitude in binary.
Here is an example to substract 14 from 46 using 9 bit 2's complement arithmetic.

There is a carry, ignore it. the MSB is 0; so, the result is positive and is in normal binary form. therefore the result is +00100000 = +32

Sorry Tornado, it is for OP reply. It is by mistake in your reply and I tried to fixed it but in vain.

Register to Reply Score 1 for Good Answer
Guru
Technical Fields - Technical Writing - New Member Engineering Fields - Piping Design Engineering - New Member

Join Date: May 2009
Location: Richland, WA, USA
Posts: 21017
Good Answers: 795
#7
In reply to #5

Re: 2's complement

08/10/2010 3:50 AM

Your arithmetic regarding 46 -14 seems correct, unlike some of the other arithmetic in this thread.

__________________
In vino veritas; in cervisia carmen; in aqua E. coli.
Register to Reply
Guru
Technical Fields - Technical Writing - New Member Engineering Fields - Piping Design Engineering - New Member

Join Date: May 2009
Location: Richland, WA, USA
Posts: 21017
Good Answers: 795
#8
In reply to #5

Re: 2's complement

08/10/2010 4:29 AM

I majored in math, but it was about 40 years ago. No doubt I have forgotten a few things in the intervening decades. But not everything. If any of my numbers are wrong, please state where, rather than asking about my textbook. Thank you.

__________________
In vino veritas; in cervisia carmen; in aqua E. coli.
Register to Reply
Power-User

Join Date: Feb 2010
Posts: 187
Good Answers: 11
#9
In reply to #8

Re: 2's complement

08/10/2010 7:54 AM

Tarnado, The reply number #5 is not for you it is for OP.

I just wanted to reply for OP's question. Suddenly, by mistake I have replied to your message.

I have said sorry, in the below because I knew you better and your posts

You are one of experience engineer in this forum which I my self learning from your posts.

Please do not mind, it was by mistake to your reply instead of OP.

I say sorry once again for this.

Register to Reply
Guru
Technical Fields - Technical Writing - New Member Engineering Fields - Piping Design Engineering - New Member

Join Date: May 2009
Location: Richland, WA, USA
Posts: 21017
Good Answers: 795
#10
In reply to #9

Re: 2's complement

08/10/2010 11:18 AM

No problem. I usually enjoy your posts, which are informative.

__________________
In vino veritas; in cervisia carmen; in aqua E. coli.
Register to Reply Off Topic (Score 5)
Guru

Join Date: Feb 2009
Location: Houston, USA
Posts: 946
Good Answers: 244
#11
In reply to #10

Re: 2's complement

08/10/2010 12:58 PM

This is by mistake. Please see the comment # 12.

- MS

__________________
"All my technical advices in this forum must be consulted with and approved by a local registered professional engineer before implementation" - Mohammed Samad (Linkedin Profile: http://www.linkedin.com/in/msamad)
Register to Reply Off Topic (Score 5)
Guru

Join Date: Feb 2009
Location: Houston, USA
Posts: 946
Good Answers: 244
#12
In reply to #5

Re: 2's complement

08/10/2010 1:01 PM

Just to add one little tip about 2's compliment.

There is an easy and simple way to find the 2's compliment of a binary number. Starting from the LSB, start repeating each bit until you find 1 (shown bolded in example below), then change the bit (0 for 1 and 1 for 0) for the rest of the bits. For example:

Binary number : 0001 1100 1000

2's compliment: 1110 0011 1000

Binary number : 0001 1010 0001

2's compliment: 1110 0101 1111

Binary number : 0001 1000 0000

2's compliment: 1110 1000 0000

The MSB is considered the sign bit, so 1 in MSB indicates it is a negative number and 0 indicates positive.

- MS

__________________
"All my technical advices in this forum must be consulted with and approved by a local registered professional engineer before implementation" - Mohammed Samad (Linkedin Profile: http://www.linkedin.com/in/msamad)
Register to Reply
Power-User

Join Date: Feb 2010
Posts: 187
Good Answers: 11
#13
In reply to #12

Re: 2's complement

08/10/2010 2:53 PM

Yes, I am completely agree with you. I have done same thing by changing the +14 to -14 then adding with +46.

The important thing is its arithmetic and sign bit. If the result is positive (0), it is in true binary form. If the result is negative (1), it is in 2's complement form.

To obtain 2's complement of a number there are three methods

1- By obtaining the 1's complement of the given number ( by changing all 0s to 1s and 1s to 0s) and then adding 1.
2- By subtracting the given n-b it number N from 2^n
3- Startig at the LSB, copying down each bit up to and including the first 1 bit encountered and complementing the remaining bits.

Here is previous example with one more example:

Ex-1: adding +46 AND -14


Ex-2: HERE ADDING -75 AND + 26



Here is no carry, the MSB is a 1. So, the result is negative an is in 2's complement form. The magnitude is 2's complement of 11001111, that is 00110001 = 49. Therefore, the result is -49

Register to Reply
Guru
United Kingdom - Member - Hearts of Oak Popular Science - Paleontology - New Member Engineering Fields - Mechanical Engineering - New Member

Join Date: May 2005
Location: In the Garden
Posts: 3389
Good Answers: 75
#15
In reply to #13

Re: 2's complement

08/11/2010 4:36 AM

Where are your examples? Can anyone else see them? (I get the impression Tornado can!)

All I can see is this:

Here is previous example with one more example:

Ex-1: adding +46 AND -14


Ex-2: HERE ADDING -75 AND + 26



Here is no carry, the MSB is a 1. So

so I am learning nothing from your post, which promises to be very informative. The same is true of your first post.

We have seen similar problems with inserted pictures before - although I usually get a box with a red square in it so I know I'm missing something!

Thanks very much

__________________
Chaos always wins because it's better organised.
Register to Reply
Anonymous Poster
#16
In reply to #15

Re: 2's complement

08/11/2010 5:26 AM

A word is sufficient to wisdom. There are sentences for you. Surprising!

Register to Reply Score 2 for Off Topic
Power-User

Join Date: Feb 2010
Posts: 187
Good Answers: 11
#17
In reply to #15

Re: 2's complement

08/11/2010 5:49 AM

English Rose: Please update your internet browser and sometimes I myself face same problem with old browsers.

Guest: It is not good to use proverb in wrong place. English rose is right, if he can not see pictures, it is difficult to understand what I am talking about.

Register to Reply
Guru
United Kingdom - Member - Hearts of Oak Popular Science - Paleontology - New Member Engineering Fields - Mechanical Engineering - New Member

Join Date: May 2005
Location: In the Garden
Posts: 3389
Good Answers: 75
#20
In reply to #17

Re: 2's complement

08/11/2010 7:22 AM

I'm using IE 8.0.6001.18702 and since this is a work computer, on a work network, I can't update it any further, if that's appropriate. As far as I know, work update these things automatically. Obviously, I don't have the option of installing another browser either!

Logging on at home is difficult as I'm still on copper wires and although I pay for "upto 20 Mbps" service, in practice it's rarely even 512 kps <sigh>, which makes loading CR4 pages sooooo sloooow. BBT is now a no-go area

Ignore the guest - if he can't be bothered logging in for such comments, he doesn't deserve attention.

PS What makes you assume I'm male?

__________________
Chaos always wins because it's better organised.
Register to Reply
Anonymous Poster
#14

Re: 2's Complement

08/11/2010 12:40 AM

Go to Digital electronics or maths forum, Guys!

Register to Reply Score 2 for Off Topic
Anonymous Poster
#18

Re: 2's Complement

08/11/2010 6:10 AM

With binary, adding numbers together is direct and simple. But there is no direct method of subtracting binary numbers. The process of 2's complement enables you to subtract two binary numbers by the process of addition. This is done by generating the 2's complement of a binary number, by firstly inverting all the bits and then adding 1 to the LSB and sequence it as far as possible through the complete binary number. This has the effect of making this binary negative. You then take this number and add it to the binary number, you originally wanted to subtract it from.

Confused! I am. Sorry I can explain it any better

Register to Reply
2
Anonymous Poster
#19

Re: 2's Complement

08/11/2010 6:16 AM

The 2's complement of 7 is not 0111. This is it's straight binary value.

The 2's complement of 7 is 0111 invert this number giving 1000 and the adding 1 to the LSB giving 1001. So for example 10 - 7 = 3

1010 (10) - 0111 (7) Or 1010 (10) + 1001 (-7) = 0011

Register to Reply Good Answer (Score 2)
Guru
Technical Fields - Project Managers & Project Engineers - New Member

Join Date: Aug 2006
Location: Midwestern United States
Posts: 843
Good Answers: 76
#21

Re: 2's Complement

08/11/2010 8:41 AM

*sigh* Nothing like a CR4 thread, first thing in the morning, to remind you about how little you actually know.

I try to view and follow threads that are beyond my knowledge, using CR4 as a resource to augment education, and this thread might as well be written in another language.... oh, wait... it is...

*embarrassed that I never learned Binary*

JavaHead

__________________
Reuters - Investigators found that the recent thread derailment in CR4 was caused by over-weight creatures of lore and request that membership DON'T FEED THE TROLLS.
Register to Reply Off Topic (Score 5)
Anonymous Poster
#22
In reply to #21

Re: 2's Complement

08/11/2010 11:14 AM

Relax, JavaHead. I learned about 2's complement about 30 years ago and never used it except to be able to answer the questions on tests. My career just didn't work out that I had a need for it. Maybe yours has been similar. (Your sig. is a good wording of this.)

Register to Reply Off Topic (Score 4)
Guru
Technical Fields - Project Managers & Project Engineers - New Member

Join Date: Aug 2006
Location: Midwestern United States
Posts: 843
Good Answers: 76
#23
In reply to #22

Re: 2's Complement

08/11/2010 11:22 AM

Thanks for the ego boost

I will need it as I am preparing to apply/interview for a position outside of the industry in which I have spent my entire career.

Hopefully, they do not require Binary

JavaHead

__________________
Reuters - Investigators found that the recent thread derailment in CR4 was caused by over-weight creatures of lore and request that membership DON'T FEED THE TROLLS.
Register to Reply Off Topic (Score 5)
Power-User

Join Date: Jul 2010
Location: Near ... New Orleans, Louisiana, USA
Posts: 217
Good Answers: 3
#24

Re: 2's Complement

08/11/2010 5:38 PM

Hello rabat,

Some of what I wrote below is for other readers, to be thorough - notably english rose. Check these out first. http://www.youtube.com/watch?v=qdFmSlFojIw & http://www.binarymath.info/

The MSB (most significant bit) is the far left digit. The LSB (least significant bit) is the far right digit.

The 2's complement is used to add a negative number, instead of subtracting a number, so you do not have to worry about encoding the "subtraction" function.

Say you want to subtract 7 from any number n. Instead of subtracting, you can add negative 7. Negative numbers are represented by the 2's complement of the absolute value. Thus n - 7, would be n + (-7), or n + (2's Complement of 7). But it is important to understand that because you change a "real number" to a "complement", you have to adjust the "answer" ... I'll try to explain.

Binary 111 = 7 in decimal.

To get the 2's complement, first add a zero to the far left (xxx becomes 0xxx). Now look at the bit to the far right (LSB) of 0xx'x'. If it is 1, then keep it and reverse the rest of the 1's & 0's to the left, and that is 0xxx's 2's complement. depending on what number you are adding (actually subtracting) you need to add zeros to the left for 1 more digit that the other number posseses. Just keep reading if you don't get it yet it should become clearer as you read.

So the 2's complement of:

0111 (7) becomes 1001

0101 (5) becomes 1011

0110 (6) becomes 1010

0100 (8) becomes 1100

Note how the bold numbers do not change.

If it is a 0, keep it & move to the next bit to the left, and repeat. You only change the rest to the left once you get to a 1.

0111 = 4bit version of decimal 7, then 1001 is the 4bit 2's complement. I'll subtract 7 from 9 and explain. First, the binary of 9 decimal = 1001 = 8+0+0+1 or in binary notation 2^3+0^2+0^1+2^0. But 1001 'the complement,' is not actually a number, it is a manipulation, so don't think of it as representing a real number.

So 9+(-7) =

01001 (9) (the binary version of 9)

+ 11001 (-7) (which should be thought of as -2's complement of 7)

= 100010 = (drop)(0)(0010) = (drop)(positive binary(0010)) or decimal 2.

The first digit, the 1 in this case, is dropped, or 'ignored', because we only started with 4 digits, we can only have an answer with at most, 4 digits. The next column to the right, tells two things about the answer ... if the answer is negative or positive. Zero(0), says it is positive and in the form or a binary. If the second digit is, One(1)says it is negative and in the form of a complement.

Let's take 15 and subtract 14.

15 = 1111 = o1111

14 = 1110 = 001110

2's compliment of 14 = 10010 ... (added a 0 to the far left, then from the right, keeping zeros and the first 1, change the rest to the left of that first 1).

1111 (15)

+ 10010 (-14)

= 100001 = (drop)(positive binary(0001) or 1.

And one with a negative answer, like 9-14.

01001 (9)

+10010 (-14) add the 0 to far left before converting to complement

= 11011 = (drop)(negative complement(1011)

OHHH so .... it is the negative of the 2's complement of the 1011. We have to convert it back ... so what is the 2's complement of 1011 ... it's 0101 which is really -(0x8+1x4+0x2+1x1) or 5. So the answer is -5.

So it can be thought of as:

= (-)(2's complement of 1011) = -(0101) = -5

Note that this is for ANSWERS not original NUMBERS.

The "NUMBER" 1110 is 1x8+1x4+1x2+0x1 = 14

but when using complements, the "ANSWER" 1110 is (-)(2's compliment of 110) = -(010) = -2

If we were adding 9+9 (two real numbers), we don't ignore the carry over.

1001

+ 1001

= 10010

Register to Reply Score 1 for Good Answer
Guru
United Kingdom - Member - Hearts of Oak Popular Science - Paleontology - New Member Engineering Fields - Mechanical Engineering - New Member

Join Date: May 2005
Location: In the Garden
Posts: 3389
Good Answers: 75
#25
In reply to #24

Re: 2's Complement

08/12/2010 6:20 AM

So now there are two posts on here that explained what's going on that I could see. And you both got a GA from me - thanks, I do like thoroughness

I raised not being able to see the inserts as I'm usually not the only one, and someone has to speak up (and it's usually me!).

__________________
Chaos always wins because it's better organised.
Register to Reply
Anonymous Poster
#26
In reply to #25

Re: 2's Complement

08/12/2010 9:31 AM

Good, Gooder and Goodest

Register to Reply
Guru
United Kingdom - Member - Hearts of Oak Popular Science - Paleontology - New Member Engineering Fields - Mechanical Engineering - New Member

Join Date: May 2005
Location: In the Garden
Posts: 3389
Good Answers: 75
#27
In reply to #26

Re: 2's Complement

08/12/2010 12:22 PM

Are you related to Kris?

__________________
Chaos always wins because it's better organised.
Register to Reply Off Topic (Score 5)
Guru
Hobbies - HAM Radio - New Member

Join Date: Oct 2006
Location: Vancouver (not BC) Washington (not DC) US of A
Posts: 1261
Good Answers: 12
#28

Re: 2's Complement

08/14/2010 3:07 AM

To start with, Twos complement is a numbering system which conviently allows handling of negative numbers in logic.

Something which might be helpful is a table. I will do this for a 4 bit nibble, which can handle +7 down through -8. I will show the numbers in decimal, hex, and then binary.

+7 7 0111

+6 6 0110

+5 5 0101

+4 4 0100

+3 3 0011

+2 2 0010

+1 1 0001

0 0 0000

-1 F 1111

-2 E 1110

-3 D 1101

-4 C 1100

-5 B 1011

-6 A 1010

-7 9 1001

-8 8 1000

--------------------------

You will note that all the negative numbers have 1 in the most significant bit.

So lets take +5 for example and make it negative.

+5 = 0101

complementing the bits = 1010

adding 1 gives us 1011 which agrees with the table

_______________________________________________

NOW Lets go in the other direction starting with -6 = 1010

subtracting 1 gives us 1001 and

complementing the bits gives 0110 which equals +6.

______________________________________________

Now if we were using 16 bit arithmetic,

-1 = FFFF or 1111,1111,1111,1111. I use the commas every 4th bit to make it easier to translate back to Hex. Shall we make this positive??

subtracting 1 gives FFFE or 1111,1111,1111,1110

complementing gives 0001 or 0000,0000,0000,0001... or +1

Hope this helps

Bill

Register to Reply
Register to Reply 28 comments

Good Answers:

These comments received enough positive votes to make them "good answers".

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Copy to Clipboard

Users who posted comments:

Anonymous Poster (6); English Rose (4); Hameedullah Ekhlas (4); JavaHead (2); msamad (2); one2playwitt (1); rabat (2); Sciesis2 (1); Tornado (6)

Previous in Forum: Solar PV Grid Connected Systems   Next in Forum: Ground (Earthing) to Neutral Voltage
You might be interested in: Router Bits, Drill Bits, Video Cameras

Advertisement