What does the operator do?

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

#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; } 
+25
c cryptography
Oct 17 '10 at 5:17
source share
3 answers

The ~ operator in C ++ (and other C-like languages ​​such as C and Java) performs a bitwise NOT operation - all 1 bits in the operand are set to 0 and all 0 bits in the operand are 1. In other words, it adds to source number.

For example:

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

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

The bitwise NOT operator has an interesting property, which when applied to numbers represented by two additions , changes the sign of the number and then subtracts one (as you can see in the above example).

You might want to familiarize yourself with the various operators of the C ++ language , since it is difficult to find operators in search engines. Even better, you can get a good C ++ book that tells you about C ++ statements.

+46
Oct 17 '10 at 5:18
source share

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

+11
Oct 17 '10 at 5:19
source share

This is a bitwise padding operator. Given input

010011101

returns the result:

101100010

+8
Oct. 17 '10 at 5:19
source share



All Articles