Question

I am using cpp for programming AVR microcontroller.
My issue is:

  • I have a structure of bit field having 2 members, each of 1 bit.
  • A simple structure of Boolean variables.

While transferring the value from Boolean structure member to a bit field member, do I need to typecast bool variable into a bit field?
Would this conversion lead to warnings, as both are 1 bit variables?

Was it helpful?

Solution

Say your two boolean values are workIsFinished and customerIsInsane, and the equivalent bit fields are doneWorking and madUser.

You can simply do something like:

doneWorking = workIsFinished ? 1 : 0;
madUser = customerIsInsane ? 1 : 0;

without having to worry about casting or any of that stuff. Any compiler worth its salt will be able to optimise that pretty thoroughly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top