-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
  • 741,623
  • 53
  • 500
  • 612
MD128
  • 501
  • 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
  • 32
  • 1
  • 6
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_an_ai
  • 109,796
  • 11
  • 89
  • 185
Md. Faisal Habib
  • 1,014
  • 1
  • 6
  • 14