function bitRead does not work on data type float

function bitRead(x,n) does not work on data type float also bitWrite(x,n,b)
x = number
n = bit position
b = 1 or 0
Arduino Revision used On PC: 16

Example using:

void Test(){
float z = 1.0;
long w = 123456789;

bitRead(z,0);
}

The compile error is using the function Test(); with bitRead on a float is:
In function 'void Test()': error: invalid operands of types 'float' and 'int' to binary 'operator>>'

Note: Using bitread() on data type "long" compiles OK.

Note: The reason why I need the bitRead() function is to convert data type floats into 4 data bytes
to transmit data packets to another Arduino. The most efficient way to transmit is hex bytes as opposed to ASCII.
Converting integers and words, I use the highByte() and lowByte() functions to convert to 2 bytes.
Converting a long to 4 data bytes, I use the bitRead() & bitWrite() with index counters to convert to 4 data bytes.

Is there a workaround function for the bitRead() on a float?
I need an algorithm to convert the data type "float" to 4 bytes.
Any help is appreciated.

Try this…

typedef union
{
  float asFloat;
  long asLong;
  short asWord[2];
  byte asByte[4];
}
float_cracker_t;

void setup( void )
{
  float_cracker_t cracker;
  
  cracker.asFloat = 13.13;
  
  for ( int8_t i=0; i < 4; ++i )
  {
    byte byteToSend = cracker.asByte[i];
  }
}

void loop( void )
{
}

Good luck,
Brian

I agree… this is a simpler algorithm:

  float fout = 3.14159;
  char* p = (char*) &fout;
  char send[4];
  
  for (int i = 0; i < 4; i++){
   send[i] = p[i]; 
   Serial.print (send[i]);
  }

wow
You encoded 3.14159 float into only two hex bytes of 0x68 0x02.
How would I decode these hex bytes on the other end of communications? I thought a float took up 4 data bytes.

You encoded 3.14159 float into only two hex bytes of 0x68 0x02.

That seems pretty unlikely.

I think it's "correct behavior" that bitread() doesn't work with floats; after all the internal representation of a float is unspecified, so you don't even really know what it SHOULD do.

For Arduino to arduino transfer of data, you can use code like pwillard posted, which accesses the float as a series of bytes, but you should be aware that this can become very dangerous if you start communicating with something OTHER than another system with the same architecture. It's almost certain to fail copying floats from Arduino to PC over the USB link, for instance, because the internal format of floats are very different. This is one of the reasons that it's so common to use ascii ("3.14159e12") for this sort of communications. It's significantly less efficient, but at least it's very "standardized."

I thought thats why they created a Standard for Binary Floating-Point Arithmetic (IEEE 754-1985) ? :slight_smile:

IEEE754 is insufficient, since it doesn't insist on a particular endianness.

With advances Arduino hardware with more I/O resources and memory (Mega & 328P)then the “Arduino Team” should carefully look at a standard float format which can be easily decoded by Pc’s/Mac’s and other Arudinos. Having to transfers chars in a non fixed format between computers, that represents the data type float, is very inefficent.
:frowning: :frowning: :frowning:

Floats on 8bit architecture is generally inefficent.

Maybe you can convert your code to use integers instead?

Ok, here is an example. I spend $45 on the Sensirion SHT71 sensor. It produces three measurements of temperature, humidity and dewpoint in the data type float. You want me to reduce my code to integers so I clip off the (12 14 bit) precision decimal positions of the measurements so I can easily transmit it in efficient data packets? I not only shot one foot but both feet by doing this workaround. Hey, thanks for the suggestion away.
:-/ :-/ :-/

You want me to reduce my code to integers

I don't think that was what was suggested - you could use fixed-point instead.

so I can easily transmit it in efficient data packets

According to the datasheet, it can take 320mS to make a 14 bit reading - why are you so worried about efficiency of the serial line?
At 9600bps, you could transmit over 300 bytes in that time.

Quote: why are you so worried about efficiency of the serial line?

Good question...The big picture. I am constructing an extremely easy to use network protocol and hardware for the Arduino. Using the modified automotive LIN bus and my protocol would contain up to 1 Master and 15 Slaves. Efficient data packet transfer is priority one. Transferring bytes,words,longs and floats would have to be very efficient in a 32 byte data transmit/receive packet. Can you imagine, using characters in the 32 byte packet to represent only one float? The whole data packet could be used up transferring this one float. If all 15 Arduino slave nodes are in the endless loop() receiving data packets you are also tying up their CPU processing with inefficient data packet transfers between nodes.
:slight_smile: :slight_smile: :slight_smile:

Can you imagine, using characters in the 32 byte packet to represent only one float?

No, I can't imagine using an average of one byte to represent one bit.

Can you imagine, using characters in the 32 byte packet to represent only one float?

No, I can't imagine using an average of one byte to represent one bit.

I bet you both use the boolean datatype. :wink:

I do not mean to be an idiot, just had to say. Thought it was funny, actually. :slight_smile:
No offence intended.

I was hoping, since the data type float takes up 4 bytes, be able to encode/decode these four bytes in the transmit/receive data packet. Maybe I was expecting too much.

Why are you so hung up on floats?
If you've only got at most fourteen bits of source data, then the next most convenient data size is sixteen bits - a short.

Quote: I bet you both use the boolean datatype

The only hard part about using the boolean data type is
knowing the difference between 1 & I and 0 & O.
People who use the other data types have trouble with this difference.
:slight_smile: :slight_smile: :slight_smile:

Quote: I bet you both use the boolean datatype

The only hard part about using the boolean data type is
knowing the difference between 1 & I and 0 & O.
People who use the other data types have trouble with difference.
:slight_smile: :slight_smile: :slight_smile:

I know, but the bites per info is exactly the same when using a boolean to represent true / false, as when using '32 bytes to represent a float' :wink:

So, it is not as crazy as it may seem.

quote: "If you've only got at most fourteen bits of source data, then the next most convenient data size is sixteen bits - a short."

Are you referring to my reference to the Sensirion SHT71?
If so, a short will not do. Like I said "I am constructing a hardware/software solution to network many Arduinos. And I need to transfer in short packets of generic data which includes bytes, integers, longs and floats. :slight_smile: :slight_smile: :slight_smile: