473,839 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_VALU E and float

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

Any ideas how to implement this (especially with the overflow checks)?
Oct 21 '08 #1
5 4470
(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_VALU E 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_VALU E 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_VALU E and float

#define FLOAT_TO_TBV(fv al,prec) //may need some input checks to make
sure no overflows
#define TBV_TO_FLOAT(bv al, prec) // checks needed here to
Why not functions like

unsigned int float_to_tbv(fl oat)?
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_VALU E 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
toExternalRepre sentation(
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 objektorientier ter Datenverarbeitu ng
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_VALU E and float
#define FLOAT_TO_TBV(fv al,prec) *//may need some input checks to make
sure no overflows
#define TBV_TO_FLOAT(bv al, prec) // checks needed here to
Why not functions like
unsigned int float_to_tbv(fl oat)?
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 objektorientier ter Datenverarbeitu ng
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
2962
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, amongst others, has a table with fields for product description, quantity at locationx, etc. The problem is that very often I need to update it after making updates in an old back office application called Uniplus. I am, however, capable of...
10
4026
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. For example with values expressed in binary:
4
4414
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; variable &= BIT_MASK
33
8915
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 write type generic functions macros? #define CUBE(I) ( (I) * (I) * (I) ) Is there some more concise set of things where inline functions should be used instead of macros? Multi-statement macros, for example?
2
627
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" variables that are non-standard size (like one, two, seven and so on - bits). What is the best way to work with them? They MUST be "composed" into 32-bit words before writing them into the hardware.
6
3573
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 EL(1) EL(2) out of ELEMENTS? Thanks for your help.
11
1991
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 lines of STORE_LOWWORD(storage, value) GET_LOWWORD(storage) /* returns value */
0
2592
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 Manipulation Profiling Tools Set is a set of advanced tools that help Serious Traders analyze the market direction, market manipulative behavior and predicting the change of trend.
0
2356
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 new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market Manipulation Profiling Tools Set is a set of
0
10907
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10586
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7828
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7017
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4484
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3133
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.