为什么我的电运营商(^)不工作? [英] Why is my power operator (^) not working?

查看:120
本文介绍了为什么我的电运营商(^)不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "stdio.h"
#include "math.h"

void main(void)
{
    int a;
    int result;
    int sum=0;
    printf("Enter a Number : ");
    scanf("%d",&a);
    for(int i=1;i<=4;i++)
    {
        result = a^i;

        sum =sum+result;
    }
    printf("%d\n",sum);
}

我不知道为什么会这样'^'不是作为电源工作。请帮助。

I don't know why this '^' is not working as power. Please help.

推荐答案

嗯,首先,在 ^ 运营商在C / C ++是逐位XOR。它无关,与权力。

Well, first off, the ^ operator in C/C++ is the bit-wise XOR. It has nothing to do with powers.

现在,随着使用 POW()功能,<关于你的问题href=\"http://social.msdn.microsoft.com/forums/en-US/vcgeneral/thread/ce3c5584-a2fc-4955-9f9c-d8b53ed1ed07\">some google搜索表明,铸造的参数之一翻番帮助:

Now, regarding your problem with using the pow() function, some googling shows that casting one of the arguments to double helps:

result = (int) pow((double) a,i);

请注意,我也投了结果 INT 所有 POW()重载双倍返还,而不是 INT 。我没有可用MS编译器,所以我可以不检查上面的code,虽然。

Note that I also cast the result to int as all pow() overloads return double, not int. I don't have a MS compiler available so I couldn't check the code above, though.

这篇关于为什么我的电运营商(^)不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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