On Wednesday, 20 January 2021 at 19:05:29 UTC, Vinod K Chandran
wrote:
On Tuesday, 19 January 2021 at 16:52:18 UTC, Paul Backus wrote:
On Tuesday, 19 January 2021 at 16:22:35 UTC, Vinod K Chandran
wrote:
b ? (tbinfo.fsState |= TBSTATE_ENABLED) : (tbinfo.fsState
&= ~TBSTATE_ENABLED);
This means, "if b is true, set the TBSTATE_ENABLED flag to
true; otherwise, set it to false."
Hi Paul Backus,
Thanks for the detailed reply. After reading your reply, I got
the idea. But think there is one silly mistake in your reply.
Forgive me if I am wrong.
Instead of
"if b is true, set the TBSTATE_ENABLED flag to true; otherwise,
set it to false."
This is the meaning of that code.
if (b == true) {tbinfo.fsState = true ; } else {tbinfo.fsState
= false;}
Because, TBSTATE_ENABLED is a manifest constant and I cannot
modify it.
What about simply writing this --
"tbinfo.fsState = b ; " This also worked.
Not quite. If you print out TBSTATE_ENABLED in binary, you will
see that it is an integer with exactly one bit set to 1, and all
others set to 0. You can do this with the following line of code:
writefln("%032b", TBSTATE_ENABLED);
By "the TBSTATE_ENABLED flag", I do not mean "the constant
TBSTATE_ENABLED". What I mean is "the bit in tbinfo.fsState at
the same position as the 1 bit in TBSTATE_ENABLED". To be
clearer, I should have said something like "the 'enabled' flag in
tbinfo.fsState".
If tbinfo.fsState were a struct instead of a bitfield:
struct FsState
{
bool enabled;
// other flags...
}
...then the meaning of the code would be:
if (b == true) {
tbinfo.fsState.enabled = true;
} else {
tbinfo.fsState.enabled = false;
}
Or more concisely:
tbinfo.fsState.enabled = b;
Of course, in reality, tbinfo.fsState is a bitfield, not a
struct, so we cannot access the individual flags with syntax like
`.enabled`. Instead, we have to use bitwise operators. But
conceptually, it's the same thing.
By contrast, in your proposed version:
tbinfo.fsState = b;
...you are overwriting *all* of the flags at once, rather than
just one of them. Even if this happens to work by coincidence
(because "enabled" is the first flag), it will certainly cause
you problems later on.