-3

Does the statement return a single bit or concatenation of bits.

if(mask[i] & groupbit) {
    //...
}

with:

  • i = an integer
  • mask[i] = an element of integer pointer
  • groupbit = An integer
Shoe
  • 74,840
  • 36
  • 166
  • 272
slin6174
  • 108
  • 2
  • 10

2 Answers2

2

It will result in an entire integer. When you use the bitwise and, each bit of the two values are and'ed together, and each bit in the result is set accordingly. The result will be the same number of bits as the values being and'ed together.

This is assuming you're using two integer variables.

Alex P
  • 530
  • 1
  • 6
  • 13
  • I thought so too, but what does it mean to evaluate an integer inside an if statement? I thought if statement evaluate only true/false. – slin6174 Jun 27 '14 at 23:55
  • 2
    Any integer that is non zero will evaluate to true. What that means in the context of your code is this : if the two values you are comparing have one or more bits in the same position set, then it will evaluate to true. – Alex P Jun 27 '14 at 23:56
0

Assuming mask is a pointer to an integer type the compiler will do the following:

  1. Access the i-th element of the mask "array" as an integer
  2. make sure both operands are of the same size (this may involve bit-expansion or truncation)
  3. do a bit-wise AND
  4. if there was a single bit set in the result from the previous AND operation the value will be treated as true, otherwise as false(if every single bit of the result is 0)
YePhIcK
  • 5,816
  • 2
  • 27
  • 52