Measurement Application Bug

Discussion in 'C-Bus Serial Protocols' started by rhamer, Jul 20, 2015.

  1. rhamer

    rhamer

    Joined:
    Aug 3, 2004
    Messages:
    673
    Likes Received:
    3
    Location:
    Melbourne, Australia
    During the particularly cold weather we have been experiencing recently here in Melbourne, my weather station outside temperature took a trip into the negatives.

    It was at this time I noticed a problem with my interface from it to C-Bus not handling negative values properly. upon tracking down the culprit, I found a bug in the Measurement Application and the way it handles values.

    The short story is the documentation says the following;
    Capture.PNG

    With the bit in question highlighted and saying the value is a signed 2's compliment number.
    In reality it is only a signed 2's compliment number if the value is negative, otherwise it is just a hex representation of the number.

    BTW the Multiplier field is a signed 2's compliment number for both positive and negative values as described.

    So either there is a bug in the Measurement implementation or an error in the documentation.

    I don't really expect anything to come of this, but it might help somebody else who runs into the same problem one day.

    Cheers

    Rohan
     
    rhamer, Jul 20, 2015
    #1
  2. rhamer

    ashleigh Moderator

    Joined:
    Aug 4, 2004
    Messages:
    2,392
    Likes Received:
    24
    Location:
    Adelaide, South Australia
    As far as I know the documentation is correct.

    Signed 2's complement numbers work like this:

    (This is a logical build up to the final explanation)

    To represent signed numbers, the most significant bit could be used as a sign bit; if set then the number is negative.

    A number of early days computers worked like this.

    However, doing this would mean we could have both positive and negative zero, for example in binary:

    %0000 = positive zero
    %1000 = negative zero

    %0001 = positive one
    %1001 = negative one

    So thats actually not very useful, as differentiating positive and negative zero is a pain.

    The other thing we'd really like from signed number is that addition actually works (and in the above examples, it does not).

    This means we'd like things like 2 + (-1) to result in 1.

    And this gives a hint as to what to do:

    Flipping the bits for negative numbers seems a good bet:

    So:
    %0010 = 2

    How about:
    %1101 = -2 ??

    What happens when we add those - we'd like a result of 0.

    But %0010 + %1101 restults in %1111, which is clearly not zero.

    But adding 1 results in zero...

    So a negative number is formed by:

    - take the positive number, write it out in binary
    - invert all the bits
    - add one

    This results in the most significant bit always being 1 for a negative number (an easy test for negative); but after that things are not quite so obvious:

    %0010 = 2

    %1110 = -2

    Also:

    %0001 = 1
    %1111 = -1

    You see the pattern of the negative numbers - the more one bits are present, the LESS negative, this is the opposite of the positive numbers.

    So when the documentation refers to signed 2's complement it just means - the plain ole natural signed integer format that numbers are in.

    That can appear as hex.

    For example, $0001 is a 2 byte quantity, signed 2's complement, positive 1.

    And $1111 is a 2 byte quantity, signed 2's complement, negative 1.
     
    ashleigh, Jul 21, 2015
    #2
  3. rhamer

    rhamer

    Joined:
    Aug 3, 2004
    Messages:
    673
    Likes Received:
    3
    Location:
    Melbourne, Australia
    I see what you are saying (mostly) but it is a bit different to my thinking.

    You are saying the 2's compliment of a positive number is the negative number.

    i.e.
    0011 = 3
    1101 = -3

    This is different to what my interpretation of the documentation was.

    I was interpreting the statement as its a 2's compliment number with a sign bit.

    i.e.
    0011 = 3
    1011 = -3

    I now see I was reading it incorrectly :eek:

    Maybe now it the time to admit maths was never my strong point :rolleyes:

    Cheers

    Rohan Hamer
     
    rhamer, Jul 21, 2015
    #3
  4. rhamer

    ashleigh Moderator

    Joined:
    Aug 4, 2004
    Messages:
    2,392
    Likes Received:
    24
    Location:
    Adelaide, South Australia
    What you have described is use of a simple sign bit.

    There are 3 conventions that have historically been used for representing signed (especially) negative numbers:

    2's complement - which works for addition as I described, therefore the most common, and you'd struggle these days to ever find anything else.

    1's complement - which is just a bitwise inverse, so the most significant bit is the sign bit and all the other bits are upside down. Addition only works by a fudge (the adding of 1).

    Use of a plain sign bit only - which as as you described. This has the disadvantage that addition just does not work at all, you need to treat arithmetic as a series of special cases, for example, turning additions into subtractions after flipping the sign bit.

    Plain sign bits were used in the very early days on some machines some of the time (1950's), but the disadvantages meant that this approach has gone the way of the dinosaurs.
     
    ashleigh, Jul 21, 2015
    #4
  5. rhamer

    rhamer

    Joined:
    Aug 3, 2004
    Messages:
    673
    Likes Received:
    3
    Location:
    Melbourne, Australia
    I'm not quite that old, but looking back I think my instructor may have been...:)

    Thanks as usual.

    Cheers

    Rohan
     
    rhamer, Jul 21, 2015
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.