| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 455 人关注过本帖
标题:怪了,位操作时的错误
只看楼主 加入收藏
无名可用
Rank: 4
等 级:业余侠客
帖 子:79
专家分:259
注 册:2010-7-27
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
怪了,位操作时的错误
程序代码:
// Note:Your choice is C++ IDE
#include <iostream>
using namespace std;
void BuildTreeArray(int* &a,int* &c,int len);
int LowBit(int x);
void Show(const int* a,int len);
int main()
{
    int* a;
    int* c;
    int len;
    cin>>len;
    a=new int[len+1];
    c=new int[len+1];
    c[0]=0;
    for(int i=0;i<=len;i++)
    {
        a[i]=i;
    }
    BuildTreeArray(a,c,len);
    Show(c,len);
    return 0;
}

void BuildTreeArray(int* &a,int* &c,int len)
{
    c[1]=a[1];
    for(int k=2;k<=len;k++)
    {
        c[k]=0;
        for(int i=k;i>k-LowBit(k);i--)
        {
            c[k]+=a[i];
        }
    }
}

int LowBit(int x)
{
    int i=1;
    while(x&1)
    {
        x>>1;
        cout<<"*****"<<endl;
        i++;
    }
    return i*2;
    //return x&(-x);
}

void Show(const int* a,int len)
{
    for(int i=1;i<=len;i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
}
这是我自己建的树状数组,其中LowBit()函数是用的我自己的方法,但编译程序时会有一条警告,warning:'>>'
operator has no effect:expected operator has side-effect.运行时就出现死循环了..请大家看一下..谢了
搜索更多相关主题的帖子: color include 
2010-10-08 20:04
tornador
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:34
专家分:118
注 册:2010-10-4
收藏
得分:20 
是这样的,在LowBit函数中你用的x>>1没有产生效力,因为对x的移位并没有对x的值产生影响,所以会出现警告,而且因为x的值没有改变,所以会出现死循环。
要写成这种形式x=x>>1;才有用。
另外,有一点我想提醒你:在编程的时候,并不是写了一些比较古怪的表达式就显示自己学的比较好,编程要朝精简、通俗易懂以便修改维护的方向去写,别写
这样的语句,在软件工程中,这些可以被认为是垃圾语句的。
希望对你有帮助。
2010-10-09 13:40
无名可用
Rank: 4
等 级:业余侠客
帖 子:79
专家分:259
注 册:2010-7-27
收藏
得分:0 
谢谢
2010-10-09 21:29
快速回复:怪了,位操作时的错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016069 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved