Go Down

Topic: Signed number grief. (Read 1 time) previous topic - next topic

dhenry

Quote
That's not the mask I wanted.


I think you will find that a generic approach is not as optimal as having a series of specific approaches (one for 10-bits, one for 12-bits, etc.) that you can link in for your specific applications.

If those numbers came from an adc module, many allow left-aligned adcs making the shift approach a lot more appealing.

GoForSmoke

It should be int, not long, then.
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

tmd3

That's not the mask I wanted.  I wanted this one:
Code: [Select]
const long deviceMask = -(1 << (deviceWordLength - 1));The mask isolates the sign bit for testing, and extends the sign bit when the sign bit is set.  It needs to be as long as the type of the input, which is int in this case, but it doesn't need to be longer.

dhenry

Quote
Hmmph.


How about this piece:

Code: [Select]
(1 << deviceBits)

:)

tmd3

Having reflected a bit more on this, I see that in this code:
Code: [Select]
const long deviceBits = 10;
const long deviceWordLength = 16;
const long deviceMask = (1 << deviceBits) - 1;

there's no need for any of the constants to be declared long.  The counting constants, describing length of the significant data and length of the word it resides in, can obviously be byte-sized, at least until we start using 257-bit integers.  The mask doesn't need to be any bigger than the input data word, in this case int

Hmmph.

Go Up