By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,491 Members | 3,132 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,491 IT Pros & Developers. It's quick & easy.

bit manipulation macros

P: n/a
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)?
Oct 21 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
(2b|!2b)==? wrote:
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?
I need to write macros that will convert TO/FROM THREE_BYTE_VALUE and float
Why macros?

--
Ian Collins
Oct 21 '08 #2

P: n/a
Ian Collins wrote:
(2b|!2b)==? wrote:
>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)
Do they have a fixed range?
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
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.
Oct 21 '08 #3

P: n/a
(2b|!2b)==? schrieb:
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?
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)?
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
Oct 21 '08 #4

P: n/a
On Oct 21, 12:43*pm, "(2b|!2b)==?" <void-s...@ursa-major.comwrote:
Ian Collins wrote:
(2b|!2b)==? wrote:
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)Do they have a fixed range?
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
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.
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:ja*********@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
Oct 21 '08 #5

P: n/a
On Oct 21, 4:03*pm, "Thomas J. Gritzan" <phygon_antis...@gmx.de>
wrote:
(2b|!2b)==? schrieb:
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?
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)?
Because that would be too easy:-).
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
;-) )
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:ja*********@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
Oct 21 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.