bit manipulation macros

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • (2b|!2b)==?

    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)?
  • Ian Collins

    #2
    Re: bit manipulation macros

    (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

    Comment

    • (2b|!2b)==?

      #3
      Re: bit manipulation macros

      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.

      Comment

      • Thomas J. Gritzan

        #4
        Re: bit manipulation macros

        (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

        Comment

        • James Kanze

          #5
          Re: bit manipulation macros

          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:james.kan ze@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

          Comment

          • James Kanze

            #6
            Re: bit manipulation macros

            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:james.kan ze@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

            Comment

            Working...