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

Twos compliment in Oshonsoft

Discussion in 'Oshonsoft' started by camerart, Feb 24, 2018.

  1. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi,
    I've asked a similar question in another thread, but this is to clarify a point.

    I am getting results of a HIGH BYTE and a LOW BYTE. The D/S points out that these are in 'twos compliment form'

    Am I correct that these two BYTEs must be converted to a WORD, then 'twos complimented' or are the two BYTES used 'against' each other in a calculation?

    Thanks, Camerart
     
  2. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi,
    I think I may be able to answer my own question.

    Farther reading of the D/S says the BYTEs are in 16BIT 'twos compliment'
    The results alway show the HIGH BYTE as 0xff, this was what was throwing me, I thought the LOW BYTE was NANDed (from experiment in Oshonsoft) from the HIGH BYTE.
    Still testing!
    C.
     
  3. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    Two's complement... Invert and add 1..

    binary 22 = 00010110 Invert 11101001 ( - 23 ).. then add 1, 11101010 ( -22 )

    The Idea is for subtraction.. 7 - 8 = -1.
    8 inverted = -9 then +1 = -8.. then addition.. 7 + -8 = -1

    So if you are given a two's complement number it is basically a negative number.
    Minus one and toggle all the bits to get a positive number!!
    I use ABS to get a negative number to a positive one..

    Code (basic):

    Function abs(value As Word) As Word
     Dim result As Word
     result = value - 1
     result = 65535 - result
     abs = result
    End Function
     
     
  4. dave miyares

    Dave New Member

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


     
  5. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi I,

    I was thinking that the TWO BYTES were in 'twos compliment', but now I think the TWO BYTEs need to be joined to make a WORD, then 'twos complimented. Is this correct?
    From D/S: The value stored in these two registers is a 16-bit value in 2’s complement form, whose range is 0xF800 to 0x07FF.

    If correct then I now have a WORD in 'twos compliment' which needs your calculation.

    I've been testing ideas in Oshonsoft, does this do the same as your suggestion?
    NOTE: This is before I read the 16-BIT section in D/S (The resulting READings have always given the HB as 255 so far)
    C
     

    Attached Files:

  6. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    You are confusing 2's complement with the fact you have two bytes... 2's complement just means negative..

    A word is 16 bits.. Lets take random number 437 in 16 bit binary 0000 0001 1011 0101
    To make it negative we use the term 2's complement... If you complement the number above ( invert ) we get

    1111 1110 0100 1010 which isn't -437! It is -438. To make it right, we need another operation, ie.. we add 1
    1111 1110 0100 1011 which is -437.. Two operations or 2's complement!!

    It could contain any number of bytes. I'm using a word as you have a word ( two bytes)
     
  7. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi I,
    Yes, I was thrown by having 2x BYTES.

    Here's my effort in Oshonsoft, with your random number, is this ok?
    C
     

    Attached Files:

  8. dave miyares

    Dave New Member

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


     
  9. jjw

    jjw Member

    Joined:
    Apr 16, 2012
    Messages:
    388
    Likes:
    18
    Location:
    Helsinki, Finland
    A simple way.

    result = Not result
    result = result + 1
     
  10. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    Yes.. But remember you need the reverse You are going back to positive..

    Can I ask... What device is supplying you a 2's complement number??
     
  11. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,710
    Likes:
    421
    Location:
    Brisbane Australia
    ONLINE
    You simply have a 16 bit number. It may be negative or positive. You don't need to do anything to it. Just use it as a signed 16 bit variable.

    Mike.
     
  12. jjw

    jjw Member

    Joined:
    Apr 16, 2012
    Messages:
    388
    Likes:
    18
    Location:
    Helsinki, Finland
    But Oshonsoft uses only unsigned 16 bit variables.
     
  13. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,710
    Likes:
    421
    Location:
    Brisbane Australia
    ONLINE
    Telling the OP to negate the value no matter what it's value is bad advice.

    If Oshonsoft doesn't handle signed numbers then the OP needs to check bit 15 of the value and act accordingly - not just negate what maybe a positive number.

    Mike.
     
  14. jjw

    jjw Member

    Joined:
    Apr 16, 2012
    Messages:
    388
    Likes:
    18
    Location:
    Helsinki, Finland
    I just showed how to make a 2's complement in Oshonsoft Basic, no advice how to handle signed words.
    He could convert 16bit word to a floating point variable and do the math with them:

    Dim x as word
    Dim xf as single ' single precision floating point
    xf = x
    if xf > 32767
    xf = 65536 - xf​
    endif
     
  15. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    You are missing the point!!! The input value is 2's complement...( negative ) he needs it positive as Oshonsoft will display 65535 instead of -1.. Its easy to put a sign in front...

    BUT!!! unless C tells us what the intension is, we are unsure.. If its added to a larger positive number, this argument is academic...
     
  16. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi,
    Interesting!
    My overnight question would have been 'How do I find if the WORD is positive or not? before applying the equation.

    The Compass chip in question is HMC5983. Here is a jpg:
    C
     

    Attached Files:

  17. jjw

    jjw Member

    Joined:
    Apr 16, 2012
    Messages:
    388
    Likes:
    18
    Location:
    Helsinki, Finland
    If the value is < 32768 it is positive.
    Or if bit (15) is 0 it is positive.
     
  18. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    This is as I thought!! As jjw has already explained..

    if compass_ value > 32768 then
    compass_value = abs(compass_value )
    sign = '-'​
    end if
    Something like this should work..
     
    Last edited: Feb 25, 2018
  19. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi,
    From previous similar answers a couple of years ago, there is a reference to check WORD.N (I assume check WORD.15? for negativity) and I don't think Oshonsoft deals with ABS.

    I think from all of your answers, I should be able to get it working.

    I now wonder that, if Oshonsoft doesn't deal with negatives, will any BYTE or WORD be a negative?

    C.
     
  20. jjw

    jjw Member

    Joined:
    Apr 16, 2012
    Messages:
    388
    Likes:
    18
    Location:
    Helsinki, Finland
    No byte or word or long is negative in Oshonsoft.
    It handles all integers as positive.
    What are you doing with these x, y values?
    Calculate directions / angles ?
     
  21. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi J,
    The way I wrote the last sentence isn't clear.
    To clarify: Oshonsoft doesn't handle negative integers, so if one arrives and it is negative, does Oshonsoft simply see it as positive, which I assume is erroneous. (Perhaps does what we are trying to achieve?)

    The HMC5983 compass module, along with other types, delivers their READings to different registers. In this case X,Z and Y, possibly a triangle of magnets inside the chip. The 2x BYTEs for each register, combine to make a WORD which then goes through a calculation to give a compass reading.

    So far I haven't got sensible results from the chip yet although some of them look likely.
    I haven't seen any calculation yet, so that's to come.
    C
     
  22. jjw

    jjw Member

    Joined:
    Apr 16, 2012
    Messages:
    388
    Likes:
    18
    Location:
    Helsinki, Finland
    Oshonsoft interpreters all integers as positive.
    For example 16 bit word as 0-65535.
    When you know, that the integer is 2's complement, you must take care of the sign in the program.
    You can do it easily by converting the signed integer to a floating point variable ( see msg #12 ), as was done in the BMP280 calculation or carry the sign in a separate variable.
     

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