I'm working with embedded C for the first time. Although my C is rusty, I can read the code but I don't really have a grasp on why certain lines are the way the are. For example, I want to know if a variable is true or false and send it back to another application. Rather than setting the variable to 1 or 0, the original implementor chose 0xFF.
Is he trying to set it to an address space? or else why set a boolean variable to be 255?
Well, 0xff is the hexadecimal number FF which has a integer value of 255. And the binary representation of FF is 00000000000000000000000011111111 (under the 32-bit integer). The & operator performs a bitwise AND operation.
Embedded C is an extension of C language and it is used to develop micro-controller based applications. The extensions in the Embedded C language from normal C Programming Language is the I/O Hardware Addressing, fixed-point arithmetic operations, accessing address spaces, etc.
0xFF
sets all the bits in a char.
The original implementer probably decided that the standard 0
and 1
wasn't good enough and decided that if all bits off is false then all bits on is true.
That works because in C any value other than 0 is true. Though this will set all bytes in a char, it will also work for any other variable type, since any one bit being set in a variable makes it true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With