2

I am trying to cast float to bitwise int in C. Here's my code snippet:

write_eeprom(INDEX_CONFIG_TEMPERATURE_OFFSET_HIGH_INT, (unsigned int) (temperature_offset>>16));
write_eeprom(INDEX_CONFIG_TEMPERATURE_OFFSET_LOW_INT, (unsigned int) (temperature_offset));

I am getting the following error:

error: invalid operands to binary >> (have 'float' and 'int'),

while trying to compile. Here, temperature_offset is a float type and I am trying to cast it as high int and low int as I am trying to save data in EEPROM in 16-bit chunks (as I am using a 16-bit microcontroller). I know that the '>>' does not apply to float types. How can I fix this issue?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Holebas
  • 21
  • 2

1 Answers1

3

Create a separate unsigned int variable and memcpy the float into it, then work on that.

unsigned int temperature_offset_int;
memcpy(&temperature_offset_int, &temperature_offset, sizeof(temperature_offset));
write_eeprom(INDEX_CONFIG_TEMPERATURE_OFFSET_HIGH_INT, temperature_offset_int >> 16);
write_eeprom(INDEX_CONFIG_TEMPERATURE_OFFSET_LOW_INT, temperature_offset_int & 0xffff);
dbush
  • 205,898
  • 23
  • 218
  • 273