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.

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.

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.

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

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.

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.

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);

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

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:

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:

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.

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.