类不一致

发布于 10-10 19:18 字数 3319 浏览 10 评论 0原文

我已经制作了一个半字节类,它似乎可以工作,但是当我多次使用它时,结果是不同的。下面的代码应该可以说明问题。

#include <cstdlib>
#include <iostream>
using namespace std;
class nibble{
public:
    nibble(){}
    nibble(int n){
        for (int x=0;x<4;x++){
            b[x]=bool(n%2);
            //cout<< x<<"b[x]="<<b[x]<<endl;
            n/=2;
        }
    }
    nibble(char h){
        char* end;
        int n = strtol(&h,&end,16);
        for (int x=0;x<4;x++){
            b[x]=n%2;
            n/=2;
        }
    }
    bool bit(int n){
        n-=2;// this should only have to be n--
        return b[n];
    }
    void set(int n,bool bl ){
        n-=2;//this should only have to be n-- but n-- doesn't work.
        b[(n)]=bl;
    }
    string bin(){
        string out;
        if (b[0]){out+='1';}
        else{out+='0';}
        if (b[1]){out+='1';}
        else{out+='0';}
        if (b[2]){out+='1';}
        else{out+='0';}
        if (b[3]){out+='1';}
        else{out+='0';}

        out+='b';//cout<<'b'<<endl;;
        return out;
    }
    char hex(){
        int out=0;
        for (int x=3; x>-1; x--){
            out *=2;
            out+=b[x];
        }
        //cout<< "hex() out="<<out<<endl;
        switch (out){
            case 0:
            return '0';
            break;  
            case 1:
            return '1';
            break;  
            case 2:
            return '2';
            break;  
            case 3:
            return '3';
            break;  
            case 4:
            return '4';
            break;  
            case 5:
            return '5';
            break;  
            case 6:
            return '6';
            break;  
            case 7:
            return '7';
            break;  
            case 8:
            return '8';
            break;  
            case 9:
            return '9';
            break;  
            case 10:
            return 'A';
            break;  
            case 11:
            return 'B';
            break;  
            case 12:
            return 'C';
            break;  
            case 13:
            return 'D';
            break;  
            case 14:
            return 'E';
            break;  
            case 15:
            return 'F';
            break;  
        }
}
    int val(){
        int out=0;
        for (int x=3; x>-1; x--){
            out *=2;
            out+=b[x];
        }
        return out;
    }

private:
    bool b[3];
};
/**
nibble operator~(nibble in);
nibble operator|(nibble in1, nibble in2);
nibble operator&(nibble in1, nibble in2);
*/
int main(){
char c;
nibble a(10);
cout<<a.bin()<<" "<<a.hex()<<" "<<a.val()<<endl;
cout<<a.bit(4)<<' '<<a.bit(3)<<' '<<a.bit(2)<<' '<<a.bit(1)<<'b'<<endl;

/*** when this code is inserted, the above code gives a different result. I have no clue why this happens

cin>>c;
nibble b('A');
cout<<b.bin()<<" "<<b.hex()<<" "<<b.val()<<endl;
cout<<b.bit(4)<<' '<<b.bit(3)<<' '<<b.bit(2)<<' '<<b.bit(1)<<'b'<<endl;/*
***/
}

我觉得这一定是非常愚蠢的事情,但我已经研究了一段时间,但找不到问题所在。提前致谢。

I have made a nibble class that appears to work, but when I use it more than once, the result is different. the following code should illustrate the problems.

#include <cstdlib>
#include <iostream>
using namespace std;
class nibble{
public:
    nibble(){}
    nibble(int n){
        for (int x=0;x<4;x++){
            b[x]=bool(n%2);
            //cout<< x<<"b[x]="<<b[x]<<endl;
            n/=2;
        }
    }
    nibble(char h){
        char* end;
        int n = strtol(&h,&end,16);
        for (int x=0;x<4;x++){
            b[x]=n%2;
            n/=2;
        }
    }
    bool bit(int n){
        n-=2;// this should only have to be n--
        return b[n];
    }
    void set(int n,bool bl ){
        n-=2;//this should only have to be n-- but n-- doesn't work.
        b[(n)]=bl;
    }
    string bin(){
        string out;
        if (b[0]){out+='1';}
        else{out+='0';}
        if (b[1]){out+='1';}
        else{out+='0';}
        if (b[2]){out+='1';}
        else{out+='0';}
        if (b[3]){out+='1';}
        else{out+='0';}

        out+='b';//cout<<'b'<<endl;;
        return out;
    }
    char hex(){
        int out=0;
        for (int x=3; x>-1; x--){
            out *=2;
            out+=b[x];
        }
        //cout<< "hex() out="<<out<<endl;
        switch (out){
            case 0:
            return '0';
            break;  
            case 1:
            return '1';
            break;  
            case 2:
            return '2';
            break;  
            case 3:
            return '3';
            break;  
            case 4:
            return '4';
            break;  
            case 5:
            return '5';
            break;  
            case 6:
            return '6';
            break;  
            case 7:
            return '7';
            break;  
            case 8:
            return '8';
            break;  
            case 9:
            return '9';
            break;  
            case 10:
            return 'A';
            break;  
            case 11:
            return 'B';
            break;  
            case 12:
            return 'C';
            break;  
            case 13:
            return 'D';
            break;  
            case 14:
            return 'E';
            break;  
            case 15:
            return 'F';
            break;  
        }
}
    int val(){
        int out=0;
        for (int x=3; x>-1; x--){
            out *=2;
            out+=b[x];
        }
        return out;
    }

private:
    bool b[3];
};
/**
nibble operator~(nibble in);
nibble operator|(nibble in1, nibble in2);
nibble operator&(nibble in1, nibble in2);
*/
int main(){
char c;
nibble a(10);
cout<<a.bin()<<" "<<a.hex()<<" "<<a.val()<<endl;
cout<<a.bit(4)<<' '<<a.bit(3)<<' '<<a.bit(2)<<' '<<a.bit(1)<<'b'<<endl;

/*** when this code is inserted, the above code gives a different result. I have no clue why this happens

cin>>c;
nibble b('A');
cout<<b.bin()<<" "<<b.hex()<<" "<<b.val()<<endl;
cout<<b.bit(4)<<' '<<b.bit(3)<<' '<<b.bit(2)<<' '<<b.bit(1)<<'b'<<endl;/*
***/
}

I feel like it must be something really dumb, but I have worked on it for a while, and cannot find the problem. thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事2024-10-17 19:18:41

进行以下更改:

    bool bit(int n){
        return b[n-1];
    }

    void set(int n,bool bl ){
        b[n-1]=bl;
    }

最后(你是怎么错过这个的?)

private:
    bool b[4]; //you had b[3]

Do following changes:

    bool bit(int n){
        return b[n-1];
    }

    void set(int n,bool bl ){
        b[n-1]=bl;
    }

Finally (how did you miss this?)

private:
    bool b[4]; //you had b[3]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文