get a complier error while trying to do
float_val=float_val<<1;
It gives out a error saying "error C2296: '<<' : illegal, left operand has type 'float '"
Can't v left shift float vars? Why is this so?
get a complier error while trying to do
float_val=float_val<<1;
It gives out a error saying "error C2296: '<<' : illegal, left operand has type 'float '"
Can't v left shift float vars? Why is this so?
Shifting floats makes no sense since it's represented as the concatenation of a sign bit, an exponent and a mantissa. Since shifting operation is about shifting bits, it would imply shifting bits from mantissa to exponent and / or to sign bit.
Floating point numbers don't have bits at the level of value-representation, which is why you can't apply bitwise operations to them.
See this answer for more information.
You'd have to transform the float to something else first. Such as:
And in the above, f should be 256.0.
Now obviously this is problematic if you start with 128.4 as the cast will drop the .4. You may not want to be using a float in first place.
Since the left shift operator is defined as multiplication by a power of 2, it makes perfect sense for floating point types. However, the C language does not define its use, so instead you have to use the
scalbn
function or similar.You cannot left shift objects of
float
type.C says the operands of the bitwise shift operators shall have integer type.
Check out the standard function ldexpf if you want to quickly multiply or divide a float by a power of 2. A bit obscure obviously :-).
https://linux.die.net/man/3/ldexpf