Thread: Negative Numbers

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    61

    Negative Numbers

    Hi There,

    I have some code that reads inputs from an exterior source, for sum reason it randomly sends nagative numbers should be 128 buts its -128 for example.
    Hex value should 0x80 its 0xffffff80.

    What is the easiest way of making a condition.

    If(Feedback = 80 or -80). For example.

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    The first bit (MSB) from a signed variable is the indicator. When this bit is set, the number is negative. Otherwise, it's positive. If you don't need this bit, you can clear it with the logic opperator &. For example, if the variable is a char, your code will look like this:

    Code:
    my_variable = my_variable & 127;
    127 is bitwise 1111111. So the MSB is set to 0.
    If you want more information, tell me!

    Libpgeak

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    Well, if it "randomly" sends negative numbers, what makes you so sure that -128 should be interpreted as 128? If you can be certain, then it is a simple matter of using say, abs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    Quote Originally Posted by laserlight View Post
    Well, if it "randomly" sends negative numbers, what makes you so sure that -128 should be interpreted as 128? If you can be certain, then it is a simple matter of using say, abs.
    In other words, just always clear the MSB, as I mentioned before...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    Quote Originally Posted by Libpgeak
    In other words, just always clear the MSB, as I mentioned before...
    Sounds like you are assuming that sign & magnitude representation is used, but that is rather unlikely these days. My point is that r_james14 should understand how to interpret these negative numbers to begin with. Finding out the reason for this anomaly may be the most important thing in the end.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    Quote Originally Posted by laserlight View Post
    Sounds like you are assuming that sign & magnitude representation is used, but that is rather unlikely these days. My point is that r_james14 should understand how to interpret these negative numbers to begin with. Finding out the reason for this anomaly may be the most important thing in the end.
    I thought all computers use this sign? Compile and run the code below, than you see all computers work like this...
    Code:
    int main (void)
      {
      char i=0;
      for(i=0; 1; i++)
        {
        printf("%d\n",i);
        getch();
        }
      return 1;
      }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    Quote Originally Posted by Libpgeak
    I thought all computers use this sign?
    Well, all three possible signed integer representations in C involve a sign bit, but that's beside the point: just blindly flipping a bit is not necessarily the right thing to do.

    Quote Originally Posted by Libpgeak
    Compile and run the code below, than you see all computers work like this...
    I can eyeball your code and tell you what the output will be before implementation defined behaviour is reached (if char is signed), and your code certainly does not prove anything.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    3,824
    Quote Originally Posted by Libpgeak View Post
    Code:
    int main (void)
      {
      char i=0;
      for(i=0; 1; i++)
        {
        printf("%d\n",i);
        getch();
        }
      return 1;
      }
    One or Two mistakes in your code.

    On these lines

    Code:
      char i=0;
    Code:
    printf("%d\n",i);
    Hints
    Look up what %d means for printf.
    Look up the keywords signed, unsigned, and char.

    Tim S.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    Quote Originally Posted by stahta01 View Post
    One or Two mistakes in your code.

    On these lines

    Code:
      char i=0;
    Code:
    printf("%d\n",i);
    Hints
    Look up what %d means for printf.
    Look up the keywords signed, unsigned, and char.

    Tim S.
    1. Yes, i could do char i; couse it's set in the for loop, but that's no mistake
    2. It's a char witch I print as a decimal. But the code in it is still decimal, so this is not a mistake and works just fine

    And Laserlight, how would you fix this problem?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    Quote Originally Posted by Libpgeak
    how would you fix this problem?
    I would fix the source. If that is not possible, then I will find out what on earth are these negative numbers, and whether they should be rejected, or otherwise how to interpret them. Since r_james14 gave the example of "128 buts its -128", it may be the case that taking the absolute value is enough, hence my conditional suggestion of abs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by r_james14 View Post
    Hi There,

    I have some code that reads inputs from an exterior source, for sum reason it randomly sends nagative numbers should be 128 buts its -128 for example.
    Hex value should 0x80 its 0xffffff80.

    What is the easiest way of making a condition.

    If(Feedback = 80 or -80). For example.
    Just use unsigned char ... no more negative numbers.

    It's just as likely the device you are communicating with is sending unsigned values. It's not at all uncommon.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There are two totally separate issues here.
    Issue number 1:
    Hi There,

    I have some code that reads inputs from an exterior source, for sum reason it randomly sends nagative numbers should be 128 buts its -128 for example.
    Hex value should 0x80 its 0xffffff80.
    This is caused by assigning a signed char to an int or unsigned int. doing that invokes "sign extension". The original variable held a negative value and the assignment sought to preserve that value.
    If you don't want sign extension to occur then you need to cast to an unsigned type of the same size first.
    e.g.
    Code:
    signed char c = 0x80;
    unsigned int i1 = c; // i1 will be 0xFFFFFF80
    unsigned int i2 = (unsigned char)c; // i2 will be 0x00000080
    However, for clarity you should ideally be doing two casts, one to change the signedness and one to change the size, hence:
    Code:
    unsigned int i3 = (unsigned int)(unsigned char)c; // i3 will be 0x00000080
    Note that if you try and do it with just the one cast to unsigned int then you're actually doing the same thing as the i1 case, i.e.
    Code:
    unsigned int i4 = (unsigned int)c; // i4 will be 0xFFFFFF80
    Happy casting!

    Issue number 2:
    What is the easiest way of making a condition.

    If(Feedback = 80 or -80). For example.
    this is simply asking how to write code such as:
    Code:
    if (feedback == 80 || feedback == -80)
    in a shorter form.

    There are two ways to do this: One is to use an abs function. It then becomes:
    Code:
    if (abs(feedback) == 80)
    The other way is if you want the two values to be like 70 and -90 or any other different values, then you need to make a function that takes all three arguments and just call that:
    Code:
    int equals_any(int value, int test1, int test2) {
        return value == test1 || value == test2;
    }
    ...
    if (equals_any(feedback, 70, -90))
    Note how this doesn't actually save you from having to write it out the long way, but you can at least just do that inside the function in one place.


    Lastly Libpgeak, a modern PC does not used signed-magnitude representation. Simply clearing the top bit does not perform the absolute value function. It undisputably will change the number from negative to positive, but the magnitude may be different afterwards. Simple example:
    -1 in a byte is 0xFF. Flip the top bit and you now have 0x7F. It's positive alright, but it's equal to 127!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    Hi There,
    Thanks everyone for your replies, it was insanely simple in the end and i just unsigned char the input, solved all (well for my purposes anyways).
    Thanks Again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Negative numbers in C
    By BlaX in forum C Programming
    Replies: 18
    Last Post: 06-29-2009, 06:30 PM
  2. strange negative numbers ?!!
    By Meshal in forum C Programming
    Replies: 6
    Last Post: 10-21-2007, 04:41 PM
  3. Negative Numbers
    By cgod in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2005, 08:57 AM
  4. Negative Numbers
    By Quantrizi in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 12:48 AM
  5. negative numbers in a edit box
    By Isometric in forum Windows Programming
    Replies: 2
    Last Post: 12-19-2001, 09:51 PM