Signed number grief.

Hi all,

I have a device that returns info as a 10 bit number with the MSB being the sign bit. Therefore, the range is +511 to -512.

I'm trying to convert this to a float (i.e. actually negative or positive) and came up with this code:

        x = (x & 0b0000001000000000) ? ((x & 0b0000000111111111) - 0b0000001000000000) : (x & 0b0000000111111111);

Something is telling me that I'm doing this in a WAY too complicated manner and I wonder if there is a simpler method.

Another thing I don't like about my method is that it's not "generic". If the range of numbers had more or less bits, the above code would fail.

Ideas will be appreciated.

Thanks!

-- Roger

How about (assuming x is an int):

if (x & 0b0000001000000000) 
   x |= 0b1111110000000000;   // sign extend

Then just convert it into a float.

That works... but it depends on an int to be 16 bit.... which seems "unclean" to me.

What I'm doing - if you're interested - is writing a custom driver for the Dallas / Maxim DS-3234 real time clock chip.

One of the things it returns is the temperature of the device. It returns 2 bytes... the MSB contains 7 bits of data and the high bit is the sign bit. The LSB returns the last 2 bits as bits 7 and 6 (screwy).

So I take MSB << 8 to put it into the "top half" of an int, then add in the LSB, then finally take the whole thing and >> 6 to bring it down to bits 0 through 9. Lastly multiply by 0.25 since the temperature is 0.25 degrees C per bit.

This is the code I'm describing:

float DS3234_read_temp(void)
{
        int x;
        digitalWrite(CS, LOW);
        SPI.transfer(0x11 + 0x00);
        x = SPI.transfer(0) << 8; // read hi byte
        digitalWrite(CS, HIGH);
        digitalWrite(CS, LOW);
        SPI.transfer(0x12 + 0x00); // read lo byte
        x += SPI.transfer(0); // add it to hi byte
        digitalWrite(CS, HIGH);
        x = (x >> 6); // slide bits down
        x = (x & 0b0000001000000000) ? ((x & 0b0000000111111111) - 0b0000001000000000) : (x & 0b0000000111111111); // convert sign :(
        return (x * 0.25); // return temperature
}

I'm wondering... since the high byte has the sign bit as bit 7, could I do the signed conversion THERE before I slide everything down?

I've been racking my brain on this and I'm just not seeing it... I KNOW it must be simple but I just don't see it.

Krupski:
Another thing I don't like about my method is that it's not "generic". If the range of numbers had more or less bits, the above code would fail.

I don't see a way to make the code completely "generic," as you'd say, or "general," as I'd prefer to say. There's no way for the program to divine the number of bits that the input device provides - you have to tell it that number somewhere.

Here's my favorite way to do it:

const long deviceBits = 10;
const long deviceMask = -(1 << (deviceBits - 1));
int x;
...
    if (x & deviceMask) {
      x |= deviceMask;
    }
    someFloatVariable = x;

Here's why I like it:

  • There's only one place to describe the sensor input, and it's easy to find, and easy to determine what number to use
  • It works whenever the input device provides something that's not bigger than a long
  • It works for an int sized at something other than two bytes - it just has to be no bigger than a long
  • The compiler remembers and uses - but doesn't need to allocate memory for - the constants

Krupski:
I'm wondering... since the high byte has the sign bit as bit 7, could I do the signed conversion THERE before I slide everything down?
I've been racking my brain on this and I'm just not seeing it... I KNOW it must be simple but I just don't see it.

It may be even simpler than you think.

From the Arduino Refrence page describing the bitshift operators at http://arduino.cc/en/Reference/Bitshift:

When you shift x right by y bits (x >> y), and the highest bit in x is a 1, the behavior depends on the exact data type of x. If x is of type int, the highest bit is the sign bit, determining whether x is negative or not, as we have discussed above. In that case, the sign bit is copied into lower bits, for esoteric historical reasons...

For your program, x is indeed of type int. You're starting with the sign bit in the MSB. Based on this snippet of the reference, all you have to do is shift the value to the right until it's scaled right, and the program will automatically extend the sign bit. Looks like it's done for more than historical reasons - with this behavior, we can divide a negative integer by 2N by shifting right by N bits, just like we do for a positive integer. If x were declared as unsigned int, the sign bit wouldn't be extended, and zeroes would shift in at the MSB.

Also - and this might be the best reason - when I tested your code, I found that this line

x = (x & 0b0000001000000000) ? ((x & 0b0000000111111111) - 0b0000001000000000) : (x & 0b0000000111111111); // convert sign :(

didn't have any effect. I got the same correct answers with it, and without it

You might be overthinking this.
Edit: Or maybe not. After reflection, I recall that you want a general solution that doesn't rely on a 16-bit length for an integer. The method shown in this post relies on that, since it relies on the fact that the sign bit is in the MSB. Back to the drawing board for me.

tmd3:
Here's my favorite way to do it:

That's a good solution.

Another approach is to add 512 to the number, then AND with 1023. This converts the number to an unsigned value in the range 0 - 1023. Then convert that to a float, and subtract 512.0 to get back to a signed value.

float f = float((x+512)&1023) - 512.0 ;

the sign bit is copied into lower bits, for esoteric historical reasons

The Arduino reference needs to be changed. What is esoteric or historical about an arithmetic right shift?

Pete

stimmer:
Another approach is to add 512 to the number, then AND with 1023. This converts the number to an unsigned value in the range 0 - 1023. Then convert that to a float, and subtract 512.0 to get back to a signed value.

float f = float((x+512)&1023) - 512.0 ;

Well...that's an extra floating point subtract when the exact same thing will work with integers:

float f = int((x+512)&1023) - int(512);

Rethinking my earlier post about dealing with a left-justified, less-than-16-bit signed integer: you want a general method of managing that value. Specifically, you want something that doesn't rely on the size of an integer for any particular platform. Not a bad idea, if you want to be able to port an application to the Arduino Due, with its 32-bit integers.

If you want that particular generality - if that's not an oxymoron - I don't see a way to avoid examining the sign bit, and then doing something to the data. You can do that as described previously in this post:

tmd3:

I'll note that you can avoid the 6-bit shift by folding the shift operation into the floating-point multiplication, like this:

const long deviceBits = 10;
const long deviceWordLength = 16;
const long deviceMask = -(1 << (deviceWordLength - 1));
const float deviceScaleFactor = 0.25;
int x;
...
    if (x & deviceMask) {
      x |= deviceMask;
    }
    someFloatVariable = x * (deviceScaleFactor * (1.0/(1 << (deviceWordLength-deviceBits))));

It has the same advantages as the version in the post referenced above. I think - but don't know - that it will eliminate the 6-bit shift from the compiled code, because the compiler will recognize the factor in the last statement as being composed entirely of constants, and will do that calculation at compile time. It adds a characteristic of the sensor to the code - 0.25 degrees C per tick - but that was already embedded in the program, and it might as well be at the top where it's easy to find and modify. It also adds the characteristic that the device output is a 16-bit word.

I've tested that code with the characteristics of the device described, and I think it works. I haven't tested it with other characteristics to verify its general-ness.

Using the stimmer-fungus technique described above, the general code might look like this:

const long deviceBits = 10;
const long deviceWordLength = 16;
const long deviceMask = (1 << deviceBits) - 1;
const float deviceScaleFactor = 0.25;
int x;
...
    x >>= (deviceWordLength-deviceBits);
    x += 1 << (deviceBits - 1);
    x &= deviceMask;
    x -= 1 << (deviceBits - 1); 
    someFloatVariable = x * deviceScaleFactor;

That code works in the specific case, too. It's not checked for generality.

For more complete generality, you could add a const someType deviceOffset, to manage gizmos whose output values aren't centered on zero. That might be the input code that corresponds to a zero reading, in which case the constant would be int and added to x, or it might be the reading that a zero input describes, in which case the constant would be float and added to someFloatVariable. Then, with just a couple of value changes to constants, you could get your readings in, say, Kelvin, or - oh joy of joys - Farenheit or Rankine; or, you could get your readings from an analog conversion on something like a 4-20mA temperature transducer, or a single-supply LM35 circuit.

You want to generalise it even more?

typedef long device_t;

const device_t deviceBits = 10;
const device_t deviceWordLength = sizeof(device_t)<<2;
const device_t deviceMask = (1 << deviceBits) - 1;
const float deviceScaleFactor = 0.25;

I don't see how this helps:

typedef long device_t;
...
const device_t deviceWordLength = sizeof(device_t)<<2;

deviceWordLength is a characteristic of the input device. You can call it the length of the device's reply, or you can call it the position of the reply's sign bit. In this code, it depends only on the characteristics of the long data type.

What am I missing?

Oh great. A slow imprecise floating point divide to replace a fast precise bit shift.

You're just trying to turn a 10-bit value into 16 bits to convert to float.

How about, insane as this might seem, load up the 10 bits into a 16-bit int and extend (copy) the sign in bit 9 from bit 10 to bit 15 then convert to float.

Here is my proposal:

(signed short) ((x & 0x0200)?(x|0xfc00):x)

Essentially, left extending the sign.

Here is a generic solution; it is standard conforming as long as T is some variant of int ( signed, short, long...).

template <typename T, unsigned B>
inline T sign_extend(const T x)
{
  struct {T x:B;} s;
  return s.x = x;
}

So for 10-bits extended to 16-bits:

int result = sign_extend< signed int ,10 >(x);

Noting that -1<<10 will generate the correct bit mask to sign-extend the ten-bit quantity to whatever word size… here you can see Bitlash doing the calculation on OS X with a 64 bit word size:

$ bitlash
bitlash here! v2.0 (c) 2012 Bill Roy -type HELP- 1000 bytes free
> print -1<<10:x
FFFFFFFFFFFFFC00

So, dhenry's proposal can be generalized to:

(signed short) ((x & 0x0200)?(x|(-1<<10)):x)

-br

tmd3:
I don't see how this helps:

typedef long device_t;

...
const device_t deviceWordLength = sizeof(device_t)<<2;



**deviceWordLength** is a characteristic of the input device. You can call it the length of the device's reply, or you can call it the position of the reply's sign bit. In this code, it depends only on the characteristics of the *long* data type.

What am I missing?

Sorry, I missed what that bit was doing.

Ignore me, I know nothing :stuck_out_tongue:

A little test code.

> ls
function extend10 {if arg(1)&0x200 return arg(1)|(-1<<10); else return arg(1); };
function px10 {print extend10(arg(1)):x;};
> px10(0x200)
FFFFFFFFFFFFFE00
> px10(0x1ff)
1FF
> px10(0x10)
10

-br

GoForSmoke:
A slow imprecise floating point divide to replace a fast precise bit shift.

I think the poster refers to this line:

    someFloatVariable = x * (deviceScaleFactor * (1.0/(1 << (deviceWordLength-deviceBits))));

Looking at these concerns one by one, in reverse order:

  • floating point divide: Indeed, there's a floating point division explicitly coded in that line. But, the arguments are all constants, along with everything else inside the parentheses. Based on the explorations of compiler's output for other complicated floating point expressions, I believe that the compiler will detect that the expression evaluates to a constant, optimize the internal calculations out of existence, and wind up with a single floating point constant. I don't see that there will be a division in the executed code.
  • imprecise: I don't think so. The operation that's being replaced is a bit shift right, equivalent to division by 2N, for an integer N. The floating point representation of 2-N is a sign bit, an exponent, an implied one, and 24 zeroes. Division will result in a change of exponent only, and won't result in any loss of precision. It's true that, in general, precision is lost in a floating point division, but not in this case, and that's a not consequence of the particular parameters of this calculation, but rather of the very operation that's being performed.
  • slow: Yes, a floating point division is slow. But, because everything involved is a constant, it happens at compile time, rather than at runtime. The Arduino won't execute a floating point division based on this code.

If you're skeptical about the compiler optimizing away the division, you can force it like this:

...
const float deviceScaleFactorComplete = deviceScaleFactor * (1.0/(1 << (deviceWordLength-deviceBits)));
...
    someFloatVariable = x * deviceScaleFactorComplete;

... just trying to turn a 10-bit value into 16 ...

Yes. But, the OP expressed concern about the fact that some of the proposed techniques rely on the fact that an integer has a 16-bit representation - an implementation-dependent size - and wanted to know how to make his code more general. That concern is of more than merely academic interest, because "on the Arduino Due, an int stores a 32-bit (4-byte) value." See it here: http://arduino.cc/en/Reference/Int. It's not at all unlikely that some of us will be porting code we write today to that platform, so there's certainly value in coding for the general case.

... load up the 10 bits into a 16-bit int and extend (copy) the sign in bit 9 from bit 10 to bit 15 ...

It's even easier than that. The device provides a 10-bit signed number, left-justified in a 16-bit word. For a 16-bit platform, it's as simple as loading the value into a signed int, and shifting it six bits to the right; the sign bit is automatically extended. But that's not what the OP asked for: he asked for help in writing code that could easily accommodate a different number of significant bits from the input device, and he expressed concern about algorithms that relied on a 16-bit integer size. Because the issue has practical implications, it's getting a bit of attention. It's certainly made me wonder how deeply I've embedded implementation-dependent parameters into some of my own favorite code.

I put this to some exercise:

#define FLP(pin)  {PORTD |= 1<<1; PORTD &=~1<<1;}

void setup(void) {
  PORTD &=~(1<<1);
  DDRD |= (1<<1);
}

const long deviceBits = 10;
const long deviceWordLength = 16;
const long deviceMask = -(1l << (deviceWordLength - 1));
const float deviceScaleFactor = 0.25;
int x=511;
float someFloatVariable;

void loop(void) {

  FLP(OUT_PIN); FLP(OUT_PIN);  FLP(OUT_PIN); //flip out pin
  if (x & deviceMask) {
    x |= deviceMask;
  }
  someFloatVariable = x * (deviceScaleFactor * (1.0/(1 << (deviceWordLength-deviceBits))));
  FLP(OUT_PIN); FLP(OUT_PIN); //flip out pin
  someFloatVariable = (signed short) ((x & 0x0200)?(x|(-1<<10)):x);
  FLP(OUT_PIN);
  delay(10);
}

Question: how much time does the floating point approach take than the integer shift approach?
1: less time;
2: the same;
3: 8x;
4: all the above.