[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

bit manipulation macros

(2b|!2b)==?

10/21/2008 9:28:00 AM

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)?
8 Answers

Ian Collins

10/21/2008 9:32:00 AM

0

(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

(2b|!2b)==?

10/21/2008 10:43:00 AM

0

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.

Thomas J. Gritzan

10/21/2008 2:03:00 PM

0

(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

James Kanze

10/21/2008 9:54:00 PM

0

On Oct 21, 12:43 pm, "(2b|!2b)==?" <void-s...@ursa-major.com> wrote:
> 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: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

10/21/2008 9:58:00 PM

0

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: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

Claire

1/19/2013 2:38:00 PM

0

On Jan 19, 2:16 pm, Mitchell Holman <nomailverizon.net> wrote:
> Claire <claireonuse...@googlemail.com> wrote innews:2a43553c-6216-4f8b-a266-954610bf430e@n9g2000yqf.googlegroups.com:
>
>
>
>
>
>
>
>
>
> > On Jan 19, 11:03 am, "Syd M." <pauldavidwri...@yahoo.com> wrote:
> >> On Jan 18, 7:06 pm, abelard <abela...@abelard.org> wrote:
>
> >> > On Fri, 18 Jan 2013 15:54:08 -0800, Christopher A. Lee
>
> >> > <chrislee95...@comcast.net> wrote:
> >> > >On Fri, 18 Jan 2013 16:38:27 -0700, Virgil <vir...@ligriv.com>
> >> > >wrote:
>
> >> > >>In article <shijf8hcmrnlb80alpg8msbpkc1uqfv...@4ax.com>,
> >> > >> J <jdyou...@ymail.com> wrote:
>
> >> > >>> Marriage is a union of one
> >> > >>> man and one woman. Anything else is a sham.
>
> >> > >>Marriage as a religious ritual may well be so limited, and least
> >> > >>in s
> > ome
> >> > >>religions, but marriage, at least in some countries, is a civil
> >> > >>contr
> > act
> >> > >>between two persons establishing legal rights and legal
> >> > >>obligations different from those between two persons not married
> >> > >>to each other.
>
> >> > >>And to deny all these rights and obligations because of the
> >> > >>genders o
> > f
> >> > >>those applying for them is a great evil.
>
> >> > >And it's none of the priests' business.
>
> >> > none of yours either mister dumbo
>
> >> He doesn't try to tell people how to live, control freak.
>
> >> PDW
>
> > It would seem though that some in the gay rights movement, as well as
> > those erroneously calling themselves secularists now want to control
> > the church and tell Christians how to live and what to believe though.
>
>      Funny I have Mormons and Jehovahs Witnesses and
> Baptists and Lutherans come to my door trying to tell
> me what to believe. But never any gays. Or atheists.
>
>      Do explain.

It does not feel as though they are telling you what to believe
because you already believe them. ;-) The issue here for me is not
people's right to a different opinion but the State’s encroaching on
what is a matter of individual religious belief and conscience.

Mitchell Holman

1/19/2013 3:02:00 PM

0

Claire <claireonusenet@googlemail.com> wrote in
news:b81c3f5c-830b-474f-9fc1-cfe2f2ad3795@hq4g2000vbb.googlegroups.com:

> On Jan 19, 2:29?pm, Christopher A. Lee <chrislee95...@comcast.net>
> wrote:
>> On Sat, 19 Jan 2013 08:16:43 -0600, Mitchell Holman
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> <nomailverizon.net> wrote:
>> >Claire <claireonuse...@googlemail.com> wrote in
>> >news:2a43553c-6216-4f8b-a266-954610bf430e@n9g2000yqf.googlegroups.com
>> >:
>>
>> >> On Jan 19, 11:03 am, "Syd M." <pauldavidwri...@yahoo.com> wrote:
>> >>> On Jan 18, 7:06 pm, abelard <abela...@abelard.org> wrote:
>>
>> >>> > On Fri, 18 Jan 2013 15:54:08 -0800, Christopher A. Lee
>>
>> >>> > <chrislee95...@comcast.net> wrote:
>> >>> > >On Fri, 18 Jan 2013 16:38:27 -0700, Virgil <vir...@ligriv.com>
>> >>> > >wrote:
>>
>> >>> > >>In article <shijf8hcmrnlb80alpg8msbpkc1uqfv...@4ax.com>,
>> >>> > >> J <jdyou...@ymail.com> wrote:
>>
>> >>> > >>> Marriage is a union of one
>> >>> > >>> man and one woman. Anything else is a sham.
>>
>> >>> > >>Marriage as a religious ritual may well be so limited, and
>> >>> > >>least in s
>> >> ome
>> >>> > >>religions, but marriage, at least in some countries, is a
>> >>> > >>civil contr
>> >> act
>> >>> > >>between two persons establishing legal rights and legal
>> >>> > >>obligations different from those between two persons not
>> >>> > >>married to each other.
>>
>> >>> > >>And to deny all these rights and obligations because of the
>> >>> > >>genders o
>> >> f
>> >>> > >>those applying for them is a great evil.
>>
>> >>> > >And it's none of the priests' business.
>>
>> >>> > none of yours either mister dumbo
>>
>> >>> He doesn't try to tell people how to live, control freak.
>>
>> >>> PDW
>>
>> >> It would seem though that some in the gay rights movement, as well
>> >> as those erroneously calling themselves secularists now want to
>> >> control the church and tell Christians how to live and what to
>> >> believe though.
>>
>> > ? ? Funny I have Mormons and Jehovahs Witnesses and
>> >Baptists and Lutherans come to my door trying to tell
>> >me what to believe. But never any gays. Or atheists.
>>
>> > ? ? Do explain.
>>
>> Mormons, JWs, Baptists and Lutherans imagine their religious beliefs
>> apply to everybody else.
>>
>> Like the idiots here who talk about "truth" and cite the Bible to try
>> and back it up.
>>
>> Funny how this goes hand in glove with intolerance.
>
> Oh-ho-ho! Rich to hear you talk of all people about intolerance!!



So far I don't see gays and atheists trying to
limit my birth control options or marriage options
or assisted suicide options or sunday shopping
options.

Can you say the same about Christians?





Mitchell Holman

1/19/2013 3:03:00 PM

0

Claire <claireonusenet@googlemail.com> wrote in
news:542b4cb9-b237-40a6-ab8a-46a70b2b51ef@h6g2000vbp.googlegroups.com:

> On Jan 19, 2:16?pm, Mitchell Holman <nomailverizon.net> wrote:
>> Claire <claireonuse...@googlemail.com> wrote
>> innews:2a43553c-6216-4f8b-a2
> 66-954610bf430e@n9g2000yqf.googlegroups.com:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> > On Jan 19, 11:03 am, "Syd M." <pauldavidwri...@yahoo.com> wrote:
>> >> On Jan 18, 7:06 pm, abelard <abela...@abelard.org> wrote:
>>
>> >> > On Fri, 18 Jan 2013 15:54:08 -0800, Christopher A. Lee
>>
>> >> > <chrislee95...@comcast.net> wrote:
>> >> > >On Fri, 18 Jan 2013 16:38:27 -0700, Virgil <vir...@ligriv.com>
>> >> > >wrote:
>>
>> >> > >>In article <shijf8hcmrnlb80alpg8msbpkc1uqfv...@4ax.com>,
>> >> > >> J <jdyou...@ymail.com> wrote:
>>
>> >> > >>> Marriage is a union of one
>> >> > >>> man and one woman. Anything else is a sham.
>>
>> >> > >>Marriage as a religious ritual may well be so limited, and
>> >> > >>least in s
>> > ome
>> >> > >>religions, but marriage, at least in some countries, is a
>> >> > >>civil contr
>> > act
>> >> > >>between two persons establishing legal rights and legal
>> >> > >>obligations different from those between two persons not
>> >> > >>married to each other.
>>
>> >> > >>And to deny all these rights and obligations because of the
>> >> > >>genders o
>> > f
>> >> > >>those applying for them is a great evil.
>>
>> >> > >And it's none of the priests' business.
>>
>> >> > none of yours either mister dumbo
>>
>> >> He doesn't try to tell people how to live, control freak.
>>
>> >> PDW
>>
>> > It would seem though that some in the gay rights movement, as well
>> > as those erroneously calling themselves secularists now want to
>> > control the church and tell Christians how to live and what to
>> > believe though.
>>
>> ? ? ?Funny I have Mormons and Jehovahs Witnesses and
>> Baptists and Lutherans come to my door trying to tell
>> me what to believe. But never any gays. Or atheists.
>>
>> ? ? ?Do explain.
>
> It does not feel as though they are telling you what to believe
> because you already believe them. ;-) The issue here for me is not
> people's right to a different opinion but the State?s encroaching on
> what is a matter of individual religious belief and conscience.


What "state encroachments" are those?