Verdris:

I’m seeing the minus sign added arbitrarily. The absolute value of the result doesn’t change

Those two statements are incongruous with the way ints handle/store negative numbers at the binary level.

For the “minus sign added arbitrarily”, in the absence of code, the only real way I see this happening is that your binary operations are occasionally settings the MSB of your int, which indicates a negative number. However, if this is the case, the actual negative value that the lower 15 bits represents is going to be completely different from the positive value those same 15 bits represents. Thus “The absolute value of the result doesn’t change” is inconsistent with such behavior.

This is because ints utilize what is called a two’s complement system for storing positive and negative numbers in binary form. So, the example code below:

```
//define a char with a positive value
char x = 23;
//Set the MSB of that char
bitSet(x, 7);
//Display the new value. It is not -23
Serial.println(x, DEC);
```

will output -105. There really isn’t any accidental way of only flipping the sign of a value through binary manipulation of the variable. Any simple manipulation is going to significantly change the entire value, whether it’s a shift, a NOT, an AND, etc.

So, why does it output -105 instead of -23? Again, because values are represented using a two’s complement system, see here for http://en.wikipedia.org/wiki/Two’s_complement

In the above example, the value 23 in binary form is:

00010111

When we take this value and just set the MSB, we get:

10010111

And when we output the new decimal representation of this value, we get -105.

In order to get the binary representation of the negative value of 23, we have to take the complement of 23, add one to that complement, and then set the MSB. So,

!0010111 = 1101000 (we’re only complementing seven bits here, since the MSB doesn’t represent a value, only the sign of the value)

1101000 + 1 = 1101001

Then set MSB to get 11101001

We can easily verify that with the following snippet of code:

```
char x = B11101001;
Serial.println(x, DEC);
```

This code will output -23.

So the binary representation of the two values are:

00010111

11101001

so it is clear to see that, from a binary perspective, the two values are far more different than just a sign flip.

Now, having said all that, it does nothing to address your specific problem, merely provides indicators as to what the problem could be. As is the case with most of these problems though, we really can’t provide any useful answers without seeing the code. Vague descriptions of behavior are almost completely worthless. Small snippets of code are only slightly less worthless.