Arithmetic Bit Shift of Double Variable Data Type in C

Veridian picture Veridian · Aug 10, 2011 · Viewed 8.3k times · Source

I am trying to arithmetic bit shift a double data type in C. I was wondering if this is the correct way to do it:

NOTE: firdelay[ ][ ] is declared in main as double firdelay[8][12]

void function1(double firdelay[][12]) {
    int * shiftptr;

    // Cast address of element of 2D matrix (type double) to integer pointer
    *shiftptr = (int *) (&firdelay[0][5]); 

    // Dereference integer pointer and shift right by 12 bits
    *shiftptr >>= 12; 
}

Answer

Praetorian picture Praetorian · Aug 10, 2011

Bitwise shifting a floating point data type will not give you the result you're looking for.

In Simulink, the Shift Arithmetic block only does bit shifting for integer data types. If you feed it a floating point type it divides the input signal by 2^N where N is the number of bits to shift specified in the mask dialog box.

EDIT:
Since you don't have the capability to perform any floating point math your options are:

  • understand the layout of a floating point single precision number, then figure out how to manipulate it bitwise to achieve division.
  • convert whatever algorithm you're porting to use fixed point data types instead of floating point

I'd recommend option 2, it's a whole lot easier than 1