是什么〜运算符吗? [英] What does ~ operator do?

查看:161
本文介绍了是什么〜运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近看到一个code以上的经营者,我用Google搜索,但发现nothing.The code是。请到描述实际上这是否运营商做什么?

 #包括LT&;&stdio.h中GT;
诠释的main()
{
    unsigned long int类型I = 0;
     焦炭CH;
    字符名称1 [20],NAME2 [20];
    FILE * FP,*英尺;
    的printf(ENTER源文件:);
    得到(名称1);
    的printf(ENTER目标文件:);
    得到(名2);
    FP = FOPEN(名称1,R);
    FT = FOPEN(名2,W);
    如果(FP == NULL)
    {
        的printf(CAN,T打开文件);
    }
    而(!的feof(FP))
    {
         CH = GETC(FP);
         CH =〜((CH ^ⅰ)); / *&下; - 在这里* /
        I + = 2;
        如果(我== 100000)
        {
             I = 0;
        }
     putc将(CH,英尺);
    }
    FCLOSE(FP);
    FCLOSE(英尺);
    返回0;
}


解决方案

在C中的运算符++(和C和Java其他类似C语言)执行< A HREF =htt​​p://en.wikipedia.org/wiki/Bitwise_operators#NOT>按位非运算 - 中的所有操作数的1位被设置为0,所有的操作数中的0位被置为1。换言之,它创建的的原始数

例如:

  10101000 11101001 //原件(二进制为-22,295 16位二进制补码)
01010111 00010110 //〜原件(二进制为22294的16位二进制补码)

在你的榜样, CH =〜((CH ^ I))执行按位非上的 CH >和 I 然后将结果赋给 CH

按位NOT运算符有一个有趣的特性是,当对数字进行重组以 psented $ P $ ,它改变了数字的符号,然后减去一(你可以在上面的例子中看到)。

您可能需要熟悉 C ++语言的不同的运营商,因为它是难以搜索对于搜索引擎运营商。更妙的是,你可以得到一个很好的C ++的书它会告诉你关于C ++运算符。

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;
}       

解决方案

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.

这篇关于是什么〜运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆