代码之家  ›  专栏  ›  技术社区  ›  snakile

如何将一个结构与两个无符号的short视为一个无符号的int?(C)

  •  2
  • snakile  · 技术社区  · 14 年前

    我创建了一个表示定点正数的结构。我希望小数点两边的数字由2个字节组成。

    typedef struct Fixed_t {
        unsigned short floor; //left side of the decimal point
        unsigned short fraction; //right side of the decimal point
    } Fixed;
    

    现在我想加上两个定点数, Fixed x Fixed y . 为了做到这一点,我将它们视为整数并相加。

    (Fixed) ( (int)x + (int)y );
    

    但正如我的Visual Studio 2010编译器所说,我无法在 Fixed int .

    正确的方法是什么?

    编辑:我没有承诺 {short floor, short fraction} 执行固定。

    9 回复  |  直到 14 年前
        1
  •  3
  •   R.. GitHub STOP HELPING ICE    14 年前

    typedef union Fixed {
        uint16_t w[2];
        uint32_t d;
    } Fixed;
    #define Floor w[((Fixed){1}).d==1]
    #define Fraction w[((Fixed){1}).d!=1]
    

    • short int
    • Floor Fraction floor() foo.Floor foo.Fraction

    uint16_t w[2]; uint32_t d;

    (Fixed){1} (Fixed){{1,0}} {1,0} ((Fixed){1}).d ((Fixed){1}).d==1 ((Fixed){1}).d!=1

    w[1] w[0]

        2
  •  6
  •   Steve Jessop    14 年前

    floor fraction

    short

    unsigned int val = (x.floor << 16) + x.fraction;
    

    Fixed int

    unsigned int result = (((uint64_t)x) * y) >> 16

    unsigned int val;
    assert(sizeof(Fixed) == sizeof(unsigned int))              // could be a static test
    assert(2 * sizeof(unsigned short) == sizeof(unsigned int)) // could be a static test
    memcpy(&val, &x, sizeof(unsigned int));
    

        3
  •  5
  •   CashCow    14 年前

    typedef struct Fixed_t {
       union { 
            struct { unsigned short floor; unsigned short fraction }; 
            unsigned int whole;
             };
    } Fixed;
    

        4
  •  2
  •   Fred Foo    14 年前

    Fixed int Fixed add(Fixed a, Fixed b)

        5
  •  1
  •   Mark Elliot    14 年前

    FRAC_MAX

     // c = a + b
     void fixed_add( Fixed* a, Fixed* b, Fixed* c){
         unsigned short carry = 0;
         if((int)(a->floor) + (int)(b->floor) > FRAC_MAX){
             carry = 1;
             c->fraction = a->floor + b->floor - FRAC_MAX; 
         }
         c->floor = a->floor + b->floor + carry;
     }
    

    void fixed_add( Fixed* a, Fixed *b, Fixed *c){
        int ia = a->floor << 16 + a->fraction;
        int ib = b->floor << 16 + b->fraction;
        int ic = ia + ib;
        c->floor = ic >> 16;
        c->fraction = ic - c->floor;
    }
    
        6
  •  0
  •   Bernd    14 年前

    typedef union {
        struct Fixed_t {
            unsigned short floor; //left side of the decimal point
            unsigned short fraction; //right side of the decimal point
        } Fixed;
        int Fixed_int;
    }
    
        7
  •  0
  •   Benoit Thiery    14 年前

        8
  •  -1
  •   Andy    14 年前
    // add two Fixed 
    Fixed operator+( Fixed a, Fixed b ) 
    {   
    ...
    }
    
    //add Fixed and int
    Fixed operator+( Fixed a, int b ) 
    {   
    ...
    }
    
        9
  •  -1
  •   Vovanium    14 年前

    *(newtype *)&var
    
    推荐文章