Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded C: what does var = 0xFF; do?

Tags:

c

embedded

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?

like image 684
Dan Avatar asked Oct 13 '08 14:10

Dan


People also ask

What is 0xff in Arduino?

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.

How does embedded C work?

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.


1 Answers

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.

like image 193
Peter Kühne Avatar answered Sep 18 '22 12:09

Peter Kühne