Arithmetic bit shift of a binary variable Data type in C

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

NOTE. firdelay [] [] is declared primarily as a 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; } 
+4
source share
5 answers

Bitwise floating point data type will not give you the result you are looking for.

In Simulink, the Shift Arithmetic block only performs bit offsets for integer data types. If you supply its floating point type, it divides the input signal by 2^N , where N is the number of bits to be shifted specified in the mask dialog box.

EDIT:
Since you are not able to do floating point math, your options are:

  • understand the layout of a floating-point number with one precision, and then figure out how to manipulate it bitwise to achieve division.
  • convert any algorithm you carry to use fixed point data types instead of floating point

I would recommend option 2, it is much easier than 1

+4
source

A bit shift of a floating-point data type (reinterpreted as int) will give you gibberish (look at the binary representation diagrams here to see why).

If you want to multiply / divide by 2, then you must do this explicitly.

+3
source

According to poorly formulated and very unclear documentation, it seems that the โ€œbit-shiftโ€ in Simulink takes two arguments for floating point values โ€‹โ€‹and has the effect of multiplying the floating point value by two arguments raised to the difference.

You can use ldexp(double_number, bits_to_pseudo_shift) to get this behavior. The ldexp function is located in <math.h> .

+1
source

There is no right way to do this. Both operands << must have some integer type.

What you do is interpret ("type-punning") the double object as if it were an int object, and then offset the resulting int value. Even if double and int are the same size, this is unlikely to help do anything useful. (And even if this is useful, it makes sense to shift unsigned values โ€‹โ€‹rather than signed values).

0
source

One potential use case for this is to capture the mantissa bits, exponent bits, and the sign bit if that is interesting. For this you can use union:

 union doubleBits { double d; long l; }; 

You can install your double and install it in a union:

 union doubleBits myUnion; myUnion.d = myDouble; 

And the bit shifts the long part of the union after extracting the bits like this:

 myUnion.l >>= 1; 

Since the number of bits for each part of the double is determined, this is one way to extract the representation of the base bit. This is one use case where you could get raw base bits. I am not familiar with Simulink, but if it is possible, why the double was replaced in the first place, this may be the way to achieve this behavior in C. The fact that it was always 12 bits makes me think differently, but just in case I thought that it is worth pointing to others who stumble on this question.

0
source

All Articles