Hi , I am trying to do a couple of bitwise operations on some hex numbers but for some reason i cant compile. Apparently, it wont be possible with my method:

#include <stdio.h>
float tv,lv;
float nv = 0x01FE;
int main() 
{
    tv = 0x7FFC;
    lv = 0x8000 & tv;
    printf("lv = %.02f\n", lv);
    if (lv == 0x8000)
    {
        // number is negative
        lv = 0x0000;
        printf("lv has been changed to zero");
    }
    else
    {
        lv = (nv / tv) * 100;

        printf("number is positve lv  = %.02f%", lv  );
    }
}

i get this error :invalid operands to binary & (have ‘int’ and ‘float’)
Please help

Recommended Answers

All 7 Replies

Bitwise operators don't work with floating point types. Really the best you can do is pun the float into an unsigned char pointer and perform the operation on that. However, this could easily produce results that you're not expecting or that are completely erroneous given the structure of floating point values in binary.

Since floats are 32-bit values, you could pun them to an unsigned 32-bit integer, do the bit-wise operations, and then cast it back to a float. The rest of what decepticon said is correct. If you don't know PRECISELY how the float is represented in the specific bits of the 32-bit word (mantissa, exponent, sign, etc), then you cannot know how the operation will alter the value, including turning it into an invalid value (NAN), resulting in a floating point exception in your program as soon as you try to do anything with it as a floating point number.

Since floats are 32-bit values, you could pun them to an unsigned 32-bit integer, do the bit-wise operations, and then cast it back to a float.

Extremely risky with punning as you may hit a trap representation. Of course, you can safely cast the value to unsigned int, but that truncates the precision.

Thanks everyone for the suggestions : I am going to attempt this :

    #include <stdio.h>


uint32_t tv,lv;
uint32_t nv = 0x01FE;
int main()
{
tv = 0x7FFC;
lv = 0x8000 & tv;
printf("lv = %.02f\n", lv);
if (lv == 0x8000)
{
// number is negative
lv = 0x0000;
printf("lv has been changed to zero");
}
else
{
lv = (nv / tv) * 100;
printf("number is positve lv = %.02f%", lv );
}
}


This could work?

Hi, sorry mine didnt work. can someone please post an example of their suggestions please

What's the purpose of the code?

Th is is a sample code. What is really happening :
- client node reads in light sensor data from sensor . Puts data in packet and sends to server
- server receives packets. Adds it to buf and does some math on them to display the data properly.

Take tv as the received data from part of buffer. The code checks if the content is negative value (starting with 0xFxxx) and if it isn't then convert data content into percentage and display.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.