Connecting Tech Pros Worldwide Forums | Help | Site Map

bit manipulation macros

(2b|!2b)==?
Guest
 
Posts: n/a
#1: Oct 21 '08
As part of a data storage project, I am storing floats in 3 byte ints as
follows:

* First 21 bits represent number
* Last 3 bits represent the number of decimal places

I need to write macros that will convert TO/FROM THREE_BYTE_VALUE and float

#define FLOAT_TO_TBV(fval,prec) //may need some input checks to make
sure no overflows
#define TBV_TO_FLOAT(bval, prec) // checks needed here to

Any ideas how to implement this (especially with the overflow checks)?

Ian Collins
Guest
 
Posts: n/a
#2: Oct 21 '08

re: bit manipulation macros


(2b|!2b)==? wrote:
Quote:
As part of a data storage project, I am storing floats in 3 byte ints as
follows:
>
* First 21 bits represent number
* Last 3 bits represent the number of decimal places
>
What do you do if they don't fit?

Do they have a fixed range?
Quote:
I need to write macros that will convert TO/FROM THREE_BYTE_VALUE and float
>
Why macros?

--
Ian Collins
(2b|!2b)==?
Guest
 
Posts: n/a
#3: Oct 21 '08

re: bit manipulation macros


Ian Collins wrote:
Quote:
(2b|!2b)==? wrote:
Quote:
>As part of a data storage project, I am storing floats in 3 byte ints as
>follows:
>>
>* First 21 bits represent number
>* Last 3 bits represent the number of decimal places
>>
What do you do if they don't fit?
>
they will fit (see below)
Quote:
Do they have a fixed range?
>
Yes, and the range is covered by the bit representation prescribed
Quote:
Quote:
>I need to write macros that will convert TO/FROM THREE_BYTE_VALUE and float
>>
Why macros?
>
I generellay subscribe to the 'macros are evil' mantra - but in this
case, using macros would be a lot easier (read quicker to implement),
than say using template functions (with specializations) for the data
types to be stored.
Thomas J. Gritzan
Guest
 
Posts: n/a
#4: Oct 21 '08

re: bit manipulation macros


(2b|!2b)==? schrieb:
Quote:
As part of a data storage project, I am storing floats in 3 byte ints as
follows:
>
* First 21 bits represent number
* Last 3 bits represent the number of decimal places
Is that 21 highest bits and 3 lowest bits?
Is that 21 bits mantissa and 3 bits exponent, or what means the number
of decimal places?
Quote:
I need to write macros that will convert TO/FROM THREE_BYTE_VALUE and float
>
#define FLOAT_TO_TBV(fval,prec) //may need some input checks to make
sure no overflows
#define TBV_TO_FLOAT(bval, prec) // checks needed here to
Why not functions like

unsigned int float_to_tbv(float)?
Quote:
Any ideas how to implement this (especially with the overflow checks)?
Write some code. It helps us to understand what you want.
(And it shows that you at least tryed to make your homework ;-) )

--
Thomas
James Kanze
Guest
 
Posts: n/a
#5: Oct 21 '08

re: bit manipulation macros


On Oct 21, 12:43*pm, "(2b|!2b)==?" <void-s...@ursa-major.comwrote:
Quote:
Ian Collins wrote:
Quote:
(2b|!2b)==? wrote:
Quote:
As part of a data storage project, I am storing floats in 3
byte ints as follows:
Quote:
Quote:
Quote:
* First 21 bits represent number
* Last 3 bits represent the number of decimal places
Quote:
Quote:
What do you do if they don't fit?
Quote:
they will fit (see below)Do they have a fixed range?
Quote:
Yes, and the range is covered by the bit representation
prescribed>I need to write macros that will convert TO/FROM
THREE_BYTE_VALUE and float
Quote:
Quote:
Why macros?
Quote:
I generellay subscribe to the 'macros are evil' mantra - but
in this case, using macros would be a lot easier (read quicker
to implement), than say using template functions (with
specializations) for the data types to be stored.
Why would the functions have to be templates? What's wrong with
just a regular function?

Writing a safe macro to do this is relatively tricky, but a
function shouldn't be that hard, using standard functions like
ldexp and frexp. Something like:

unsigned int
toExternalRepresentation(
float value )
{
int exp ;
value = frexpf( value, &exp ) ;
return static_cast< int >( value * 2097152 )
| (static_cast< unsigned >( exp ) << 21) ;
}

This obviously requires some additional error checking, and may
contain an off by one error---I'm two lazy to verify whether the
results of frexpf are in the range [0.5,1.0) or [1.0,2.0). But
you get the idea.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
James Kanze
Guest
 
Posts: n/a
#6: Oct 21 '08

re: bit manipulation macros


On Oct 21, 4:03*pm, "Thomas J. Gritzan" <phygon_antis...@gmx.de>
wrote:
Quote:
(2b|!2b)==? schrieb:
Quote:
Quote:
As part of a data storage project, I am storing floats in 3
byte ints as follows:
Quote:
Quote:
* First 21 bits represent number
* Last 3 bits represent the number of decimal places
Quote:
Is that 21 highest bits and 3 lowest bits?
Is that 21 bits mantissa and 3 bits exponent, or what means the number
of decimal places?
Quote:
Quote:
I need to write macros that will convert TO/FROM
THREE_BYTE_VALUE and float
Quote:
Quote:
#define FLOAT_TO_TBV(fval,prec) *//may need some input checks to make
sure no overflows
#define TBV_TO_FLOAT(bval, prec) // checks needed here to
Quote:
Why not functions like
Quote:
unsigned int float_to_tbv(float)?
Because that would be too easy:-).
Quote:
Quote:
Any ideas how to implement this (especially with the
overflow checks)?
Quote:
Write some code. It helps us to understand what you want.
(And it shows that you at least tryed to make your homework
;-) )
I doubt it's homework; this sort of problem occurs often enough
in real life, and isn't the sort of thing I'd expect to see in a
course. But you're right that the problem isn't well specified.
(On the other hand, my suggestion in another post isn't really
fully fleshed out either, and should be adaptable to any of the
meanings of his specification.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Closed Thread