1. ## 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. 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.

Libpgeak

3. 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.

4. Originally Posted by laserlight
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. 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.

6. Originally Posted by laserlight
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. 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.

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.

8. Originally Posted by Libpgeak
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. Originally Posted by stahta01
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. 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.

11. Originally Posted by r_james14
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. 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!

13. 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