6

I get what typecasting in C is doing at a high level. I get that sometimes this is done implicitly, and sometimes is required to be done explicitly. However I don't know how this is happening in low level:

Suppose that GetSignal returns an enum type.

 uint8 outValue;
 f32_t iValue;
 iValue = (f32_t)GetSignal();
 outValue = (uint8)((i32_t)iValue);

My question is what is going on here. I have no idea how the bits are being reorganised after all of these typecasting.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Sometimes it's not really changing the bits, only telling the compiler "trust me" so it won't complain about what would otherwise be a type mismatch. – Steve Friedl Apr 29 '20 at 19:05
  • But what about suming two diferent types , for example when you try to sum a`float` and an `int` . They are both ordered in their respective registers with diferent standards , if the ALU tries to sum them as would sum 2 integers ,it would return a weird result. So my best guess is that some bit manipulation is being done – Ricardo Mostalac Apr 29 '20 at 19:08
  • 3
    Yes, there are also cases of bit manipulation, and int-to-float (and the other way around) does instruct the compiler to turn int-smelling bits into the same numerical value with float-smelling bits, but this often happens without a cast because the compile can tell it has an int and a float and it needs to add them (as you suggest). – Steve Friedl Apr 29 '20 at 19:11
  • 2
    In your question it is not really possible to be specific without knowing what `f32_t` is an alias for. The name (and the name of the function) would suggest that it is a single precision `float` and not an `enum` as you suggest. That makes a significant difference to what that casting does. Don't obfuscate the question by using `typedef` aliases that we have to guess at - use the actual data types of standard library type aliases. – Clifford Apr 29 '20 at 22:39
  • Your _comment_ in response to @SteveFreidl is a different question than that posed. SO is a Q&A not a discussion forum. Either post a new question or add it to the existing question if it is closely related. – Clifford Apr 29 '20 at 22:49

1 Answers1

4

What a cast does is convert a value of one type to a value of another type. What the representation of this value is for each given type depends on the type.

Suppose GetSignal returns an enum with an underlying value of 1. As a 32 bit integer, its representation looks like this (assuming big-endian byte ordering, i.e. high-to-low):

00000000 00000000 00000000 00000001

This integer value of 1 is then converted via an explicit cast to f32_t. Assuming that this type is represented as an IEEE 754 single precision floating point value, its representation (high-to-low) looks like this:

00111111 10000000 00000000 00000000

And this representation is stored in iValue.

Then iValue is cast to i32_t. So the float value 1 with the above IEEE754 floating point representation is converted to the int value 1 with this representation:

00000000 00000000 00000000 00000001

Which is the same as what GetSignal returned. This value is then cast to type uint8 which has this representation:

00000001

And that representation is stored in outValue.

Regarding your comment about adding a float and an int, the usual arithmetic conversion rules dictate that the value with type int is first implicitly converted to type float, then the two float values can be added and the result has type float.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • re: `(uint8)(i32_t)a_float` - FP->int conversion where the value doesn't fit in the integer type is Undefined Behaviour; going to int32_t first and *then* truncating to uint8_t is well-defined. [Is the behaviour of casting a negative double to unsigned int defined in the C standard? Different behaviour on ARM vs. x86](https://stackoverflow.com/q/10541200) - would also apply to a float larger than `255` which would also not fit in `uint8_t`. So if you have a float and want to truncate the integer part to a narrow type, convert to a wide-enough integer type first. – Peter Cordes Apr 30 '20 at 00:49