1. 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.
    Dismiss Notice

2's complement without negative reference

Discussion in 'Microcontrollers' started by Beau Schwabe, Feb 25, 2018.

  1. Beau Schwabe

    Beau Schwabe Member

    Joined:
    Jun 4, 2017
    Messages:
    107
    Likes:
    9
    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 (text):

    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
     
     
  2. Grossel

    Grossel Member

    Joined:
    Sep 16, 2008
    Messages:
    980
    Likes:
    24
    Location:
    Norway
    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:
     
  3. Beau Schwabe

    Beau Schwabe Member

    Joined:
    Jun 4, 2017
    Messages:
    107
    Likes:
    9
    ... 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.
     
  4. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  5. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    Simply ORing the MSB would not work...

    Just adding 8 would work ... then you will have a 0 ~ 16 value.. -8 + 8 = 0... 0+ 8 = 8 and 8 + 8 = 16
     
  6. large_ghostman

    large_ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    4,725
    Likes:
    532
    Location:
    SCOTLAND
    Completely messes with my head this! One of those things that you get easy or you DONT!!
     
  7. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    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...
     
    • Like Like x 1
  8. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  9. large_ghostman

    large_ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    4,725
    Likes:
    532
    Location:
    SCOTLAND
    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!!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice