How to use dguihub package ?

Paul Backus snarwin at gmail.com
Tue Jan 19 16:52:18 UTC 2021


On Tuesday, 19 January 2021 at 16:22:35 UTC, Vinod K Chandran 
wrote:
> Anyhow, I am planning to study this from beginning.
> "b ? (tbinfo.fsState |= TBSTATE_ENABLED) : (tbinfo.fsState &= 
> ~TBSTATE_ENABLED);"
> This is the problem line in that property. "b" is a boolean 
> parameter. But I dont know what this "|="sign means in D.

`a |= b` is shorthand for `a = a | b`. Similarly, `a &= b` is 
shorthand for `a = a & b`. The `|` symbol is the "bitwise or" 
operator, the `&` symbol is the "bitwise and" operator, and the 
`~` symbols is the "bitwise not" operator. [1]

The way they're being used here is to implement a bit field 
[2]--a data structure in which each bit of an integer is treated 
as a boolean "flag", where 1 is true and 0 is false. In this 
usage, the expression

     field |= FLAG

...means "take all of the bits that are set to 1 in FLAG and set 
them to 1 in field"; or, in other words, "set FLAG to true in 
field." And the expression

     field &= ~FLAG

...means "take all of the bits that are set to 0 in FLAG and set 
them to 1 in field"; or, in other words, "set FLAG to false in 
field".

You may want to take a minute with pen and paper to convince 
yourself that these particular bitwise operations actually do 
what I'm claiming they do, since it's not really obvious just 
from looking at them.

Armed with this knowledge, we can now understand the line of code 
you quoted in your message:

     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."

[1] 
https://en.wikipedia.org/wiki/Bitwise_operation#Bitwise_operators
[2] https://en.wikipedia.org/wiki/Bit_field


More information about the Digitalmars-d-learn mailing list