I figured out what is wrong, here is a C# program that describes the problem:
using System;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void your_math()
{
byte high = 0xC1;
byte low = 0x80;
var data = (Int16)(((high << 8) | low) >> 4);
System.Console.WriteLine("your result: " + (double)data);
}
static void correct_math()
{
byte high = 0xC1;
byte low = 0x80;
var data = (Int16)((high << 8) | low) >> 4;
System.Console.WriteLine("correct result: " + (double)data);
}
static void Main(string[] args)
{
your_math();
correct_math();
while (true) ;
}
}
}
The problem is that by shifting to the right before turning your number from an unsigned number to a signed one, you are basically putting a bunch of 0’s on the left side of the number, which messes up the 2’s complement form. By the way, I made this program by following my suggestion to you of simplifying your program by inserting literal values for the bytes you expect to receive.
- Ryan
