Left Shift Swimming Type

get a compatibility error when trying to make

float_val=float_val<<1; 

The error message "Error C2296: '<<: illegal, the left operand is of type' float 'is displayed.

Can't v leave the shear float vars? Why is this so?

+6
source share
7 answers

You cannot leave shift float variables because (a) your FPU will not have a switch barrel at all that is open to you, so it is physically impossible to create code for this, and (b) what would it mean? The basic representation of bits consists of several fields with different values, do you really want these bits to merge with each other?

If you want to multiply the number held in this variable by two, you should just do it.

If you want to reinterpret the float as some type that a left shift makes sense (for example, a suitable large unsigned integer type) for some horrible bit hacking, such as the square root of Carmack, well, you can do it too, but on modern hardware it's unlikely that you really need to: seriously think if there is a better way to do what you want.

+13
source

Pay attention to the standard ldexpf function if you want to quickly multiply or divide the float by a power of 2. A little obscure, obviously :-).

https://linux.die.net/man/3/ldexpf

+4
source

Floating-point numbers do not have bits at the level of representation of values, so you cannot apply bitwise operations to them.

See this answer for more details.

+1
source

Offset floats is meaningless, as it is represented as a concatenation of the sign bit, exponent and mantissa. Since the shift operation is associated with the shift of bits, this implies a shift of bits from the mantissa to the exponent and / or for writing bits.

+1
source

Since the left shift operator is defined as multiplication by a power of 2, it makes sense for floating point types. However, the C language does not define its use, so instead you should use the scalbn function or similar.

+1
source

You cannot leave shift objects of type float .

C says that the operands of bitwise shift operators must be of integer type.

0
source

You will have to convert the float to something else first. For instance:

 float f = 128; f = (float) (((int) f) << 1 ); 

And in the above case, the value of f should be 256.0.

Now, obviously, this is problematic if you start with 128.4, since the cast will be reset .4. You might not want to use float in the first place.

-1
source

Source: https://habr.com/ru/post/908644/


All Articles