• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

2's complement without negative reference

Thread starter #1
Ok, so I had a REALLY dumb epiphany and I just want to make sure I'm not missing anything.

To make things simple I'll use 4 bits.

I have a number that ranges from 7 to -8 with 0 or -1 being center.
I want to convert that to a number that ranges from 0 to 15 with 7 or 8 being center.

In the past I would check the MSB; if it was a "0" then I would ADD 8; if it was a "1" I would SUBTRACT 8 ... without really even thinking what was going on.

Looking at this closer, all I need to do is invert the MSB and I'm done. No adding or subtracting required. Am I understanding that correctly? Even up to 32 Bit numbers?

Going in reverse, that might make for a really slick way to generate a 2's complement output.

Code:
2's Complement:       Desired Output:
0000 = 0                   8
0001 = 1                   9
0010 = 2                   10
0011 = 3                   11
0100 = 4                   12
0101 = 5                   13
0110 = 6                   14
0111 = 7                   15
1000 = -8                   0
1001 = -7                   1
1010 = -6                   2
1011 = -5                   3
1100 = -4                   4
1101 = -3                   5
1110 = -2                   6
1111 = -1                   7
 

Grossel

Active Member
#2
Not sure get this problem rigth, but have you thaught that if you just simply invert MSB then you would get the output result. No adding, no substraction - only make one bit oposite :cool:
 
Thread starter #3
have you thaught that if you just simply invert MSB then you would get the output result
... Right ... simply XORing the MSB is all that is needed.... before I would just do a compare on the MSB and then based on that actually do an addition or subtraction. Like I said it was a "REALLY dumb epiphany" on my part getting from point A to point B

EDIT: Hey, we are all human and sometimes it takes something obvious right in front of our face... I just wanted to share.
 

large_ghostman

Well-Known Member
Most Helpful Member
#5
Completely messes with my head this! One of those things that you get easy or you DONT!!
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#6
2's complement is easy enough..

Micro's do not do negative numbers end of!! So programmers make it happen... we'll do 4 bit.

1111 = -1 Why!! because 0000 - 1 = 1111 ( which is actually 15 ).. 2's complement is the programming way to say negative..

1 is 0001 complemented ( this is the actual term for inversion ) you get 1110.. If you add 2 you get to get back to zero, so its obviously represents -2.. To make things right you have to add one.. complement once to invert then complement the LSBit with carry!!! or in other words 2's complement.. Programmers just subtract from max + 1 to get it positive...

16 - 1111 = 1.. BUT!! All this is superfluous as we are dealing with negative numbers, so we can just add them ie

1010 + 1110 = 1000 .. Or in English 10 + -2 = 8... Just remember to ditch the MSBit...
 

large_ghostman

Well-Known Member
Most Helpful Member
#7
Thats where i go wrong, i forget to ditch. TBH i havnt come across it for a while, most of what i have needed have been these plug and go sensors, talk about no need to think! This bio reactor is going to test me, i have all the kit except the special power unit and the main controller, Those run in at £6-£12K second hand. The reactors cost enough as it is, i just can justify spending money on something that in theory should be easy. The really hard part is finding the voltages!!
 

Latest threads

EE World Online Articles

Loading

 
Top