70

I recently saw the above operator in a code,I googled for it but found nothing.The code is below.Please describe what actually does this operator do?

#include<stdio.h>
int main()
{
    unsigned long int i=0;
     char ch;
    char name1[20],name2[20];
    FILE *fp,*ft;
    printf("ENTER THE SOURCE FILE:");
    gets(name1);
    printf("ENTER THE DESTINATION FILE:");
    gets(name2);
    fp=fopen(name1,"r");
    ft=fopen(name2,"w");
    if(fp==NULL)
    {
        printf("CAN,T OPEN THE FILE");
    }
    while(!feof(fp))
    {
         ch=getc(fp);
         ch=~((ch^i));/*<--Here*/
        i+=2;
        if(i==100000)
        {
             i=0;
        }
     putc(ch,ft);
    }
    fclose(fp);
    fclose(ft);
    return 0;
}       
Prince
  • 20,353
  • 6
  • 39
  • 59
  • 4
    searching ~ in google ? ~, which is known as the tilde or synonym operator for google search. if you search ~search surprisingly it gives yahoo :-) you need to search tidle – yadab Oct 21 '10 at 04:27
  • 1
    Related - [How does the bitwise complement (~) operator work?](https://stackoverflow.com/questions/791328/how-does-the-bitwise-complement-operator-work) – Bernhard Barker Sep 23 '17 at 19:17
  • Not related to your question, but you might want to read [Why is while(!feof(fp)) always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Gerhardh Jan 22 '20 at 15:28

3 Answers3

104

The ~ operator in C++ (and other C-like languages like C and Java) performs a bitwise NOT operation - all the 1 bits in the operand are set to 0 and all the 0 bits in the operand are set to 1. In other words, it creates the complement of the original number.

For example:

10101000 11101001 // Original  (Binary for -22,295 in 16-bit two's complement)
01010111 00010110 // ~Original (Binary for  22,294 in 16-bit two's complement)

In your example, ch=~((ch^i)) performs a bitwise NOT on the bitwise XOR of ch and i then assigns the result to ch.

The bitwise NOT operator has an interesting property that when applied on numbers represented by two's complement, it changes the number's sign and then subtracts one (as you can see in the above example).

You may want become familiar with the different operators of the C++ language since it is difficult to search for operators on search engines. Better yet, you can get a good C++ book which will tell you about the C++ operators.

Community
  • 1
  • 1
In silico
  • 51,091
  • 10
  • 150
  • 143
  • Is there any search engine that allows searching for symbols? – AlcubierreDrive Oct 17 '10 at 05:29
  • @Jon Rodriguez: None that I know of. If you need to look up a symbol or operators for a particular programming languages, it's best to simply search for the language (like "C++ programming language"). – In silico Oct 17 '10 at 05:30
  • 3
    There's [Google Code Search](http://www.google.com/codesearch) but I can't vouch for its helpfulness in finding out *about* symbols so much as for finding out their use cases ;) – BoltClock Oct 17 '10 at 05:31
  • 1
    @Jon: It helps if you know the name of the glyph Googl'ing "C code tilde" will yield an answer. But then again so would have "C programming language operators" - sure you'd get *all* the operators, but that is narrow enough to find an answer I suggest! – Clifford Oct 17 '10 at 07:27
  • @Insilico Although the nut-and-bolt details of your answer are correct, a *not* (`!`) operation is not the same as an *invert* (`~`) operation. *not* (`!`) is used on logical expressions to invert the logic resultant, whereas *invert* (`~`) is used to flip a variable's bits between 0 and 1. The usage of *not* (`!`) is also hardware dependent, so I personally don't recommend its usage. Another way to think of using the the *invert* (`~`) operator is that it is functionally the same as doing an *exclusive-or* (`^`) of your variable against the one's compliment of zero: `~0x81 = 0x81 ^ 0xFF` – Jim Fell May 01 '15 at 19:26
21

The ~ operator inverts all the bits. So 10000001 becomes 01111110.

JoshD
  • 12,490
  • 3
  • 42
  • 53
14

It is the bitwise complement operator. Given the input

010011101

returns the output:

101100010

Erkan Haspulat
  • 12,032
  • 6
  • 39
  • 45