473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

bit manipulation macros

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
5 4441
(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
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
(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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Thomas | last post by:
Hi everyone Do any of you know if this is possible in Access either by using the import tool or by writing a module in VB: I have a database containing information for a retail chain which,...
10
by: ruroma | last post by:
Hello, I need some help with the following: 1) I need to split a 16bit INT into two 8bit characters, and then be able to join these two characters to form again the original 16bit integer....
4
by: senthilrag | last post by:
Provided a unsigned integer say (32bits b31-b0 bits) and its required to get (b19-b10) . One approach is have a mask and right shift it #define BIT_MASK 0x000FFC00 uint32 variable; ...
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
2
by: Al | last post by:
HI, I'm an hardware designer that HAS to define the software structure for accessing "my" hardware, so please be patient with me. Let me put my problem first: I have "a lot" of "hardware"...
6
by: Adrian Hawryluk | last post by:
Does anyone know of a way to manipulate a macro list to remove either the beginning or the end element? I.e. Given: #define ELEMENTS EL(1) EL(2) EL(3) Is there any manipulation to get just...
11
by: Bartholomew Simpson | last post by:
I need to store two float values (each of which requires less 16 bit storage), in a double data type. Can anyone give me some macros to retrive/store values in the double ? something along the...
0
by: L'eau Prosper Research | last post by:
Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market...
0
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.