-1

I'm reading a C ++ code, the code has a line like this:

for(;;) 
{
    if(~theArray[i] & anotherCondition)
    {
        DoSomeThing();
    }
}

For some values i code goes back to the beginning of the loop, what exactly does this expression ~ on array, do?

Can anybody help?

Barmar
  • 669,327
  • 51
  • 454
  • 560
MD128
  • 493
  • 4
  • 12

2 Answers2

0

Sign '~' means negation, NOT. I guess if the array element is for example 00101001 in binary, then it will be changed to 11010110 for the operations inside 'if'.

WojciechCode
  • 9
  • 1
  • 4
0

~ operator indicates bitwise complement.

Bitwise complement operator is an unary operator (works on only one operand). It changes 1 to 0 and 0 to 1.

For example:

35 = 00100011 (In Binary)

complement of 35 is

~ 00100011 = 11011100 which is equal to 220 (In decimal)

Please check below resources to learn more about it

Bitwise Operators

Bitwise Complement Operator

463035818_is_not_a_number
  • 88,680
  • 9
  • 76
  • 150
Md. Faisal Habib
  • 823
  • 1
  • 5
  • 13