I am communicating with a roomba. It sends certain int-sized values (-32768 - 32767), two bytes at a time, high byte first. I am having trouble keeping the value signed when I receive it, and put the two bytes into an int variable using bit math. At the top of my code, I declare ‘distance’ as an int, and later in the code, I do this:
distance = ((sensorbytes << 8) | sensorbytes);
sensorbytes is the high byte, and sensorbytes is the low byte.
What am I doing wrong? I have tried this:
distance = ((int)(sensorbytes << 8) | sensorbytes);