lucid.dreamer
lucid.dreamer

Reputation: 51

What does the single & in the following statement mean?

strcat(b, ((x & z) == z) ? "1" : "0");

I understand strcat() function and the conditional (ternary) operator. But I don't know what (x & z) == z means.

Upvotes: 1

Views: 788

Answers (3)

kumar
kumar

Reputation: 2580

(x & z) == z means in 'x', ALL the bits which are SET in 'z' is SET.

'x' must contain all the bits set OR additionally some more bits may also be set. Then this condition becomes true.

Upvotes: 1

Vagish
Vagish

Reputation: 2547

The means:

Concatenate "1" to string b if bitwise And of x and z is same as z,else concatenate "0" to string b.

x&z means bitwise anding of x and z.

The result is then compared with z.

If the result is same as z then condition evaluates to be TRUE otherwise FALSE.

Upvotes: 2

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

& is the bitwise AND operator.

here, (x & z) == z means, perform a bitwise AND of x and z and if that value equals to z, then....

Ref: Chapter 6.5.10, C11 standard, "Bitwise AND operator"

The result of the binary & operator is the bitwise AND of the operands (that is, each bit in the result is set if and only if each of the corresponding bits in the converted operands is set).

Upvotes: 4

Related Questions