• PIC24 Topics
  • error: invalid operands to binary >> (have 'float' and 'int')
2020/11/18 20:51:02
chowam01
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? I tried using a separate unsigned int variable called temperature_offset_int and then I used memcpy to cast the float into it. However, my MPLAB IDE displays an error message 'unexpected token' when I use the following:

memcpy(&temperature_offset_int, &temperature_offset, sizeof temperature_offset);

Does anyone know a solution to this problem?
2020/11/18 22:59:18
ric
You're casting in the wrong place.
This:
(unsigned int) (temperature_offset>>16))
says "shift temperature_offset right sixteen bits, and THEN cast to an unsigned integer".
You need to cast BEFORE the shift, so:
((unsigned int) temperature_offset>>16))
2020/11/18 23:46:34
1and0
ric
You need to cast BEFORE the shift, so:
((unsigned int) temperature_offset>>16))

Shifting a 16-bit int right 16 bits is not going to work as expected. ;)
 
2020/11/18 23:52:18
1and0
chowam01
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? I tried using a separate unsigned int variable called temperature_offset_int and then I used memcpy to cast the float into it. However, my MPLAB IDE displays an error message 'unexpected token' when I use the following:

memcpy(&temperature_offset_int, &temperature_offset, sizeof temperature_offset);

Does anyone know a solution to this problem?

I believe you want to save the 32-bit floating point number and not just the integer part of the number. If so, use this:

    (*(unsigned long *)&temperature_offset) >> 16;
    (*(unsigned long *)&temperature_offset) & 0xFFFF;

2020/11/19 00:08:18
ric
1and0
ricYou need to cast BEFORE the shift, so:((unsigned int) temperature_offset>>16))

Shifting a 16-bit int right 16 bits is not going to work as expected. ;) 

Good point. I didn't notice which sub forum this was in, and assumed 32 bit ints.
I wish everyone shifted to stdint definitions.
2020/11/19 05:10:52
1and0
ric
I wish everyone shifted to stdint definitions.

Agreed.
 
This should be more efficient than my Post #4:

    (*((unsigned int *)&temperature_offset + 1));
    (*(unsigned int *)&temperature_offset);

2020/11/19 12:23:17
ric
Is that going to work at all when the variable is a float that is being cast to an integer???
 
2020/11/19 13:17:35
1and0
ric
Is that going to work at all when the variable is a float that is being cast to an integer???

You mean this?
float foo;
int32_t foo_int;
uint16_t hiword, loword;
 
    foo = 123456.789;  // 0x47F12065
    foo_int = (int32_t) foo;  // 123456 = 0x0001E240

    hiword = (*((uint16_t *)&foo_int + 1));  // 0x0001
    loword = (*(uint16_t *)&foo_int);        // 0xE240

 
 
© 2021 APG vNext Commercial Version 4.5

Use My Existing Forum Account