这是我的字符位表示程序。但是我不知道这代表我是对还是错?有可疑单位(红色)。
您能解释一下这是什么(如果是正确的话),或者如果这些单位不正确,我的代码有什么问题。谢谢
#include "stdafx.h"
#include "iostream"
using namespace std;
struct byte {
unsigned int a:1;
unsigned int b:1;
unsigned int c:1;
unsigned int d:1;
unsigned int e:1;
unsigned int f:1;
unsigned int g:1;
unsigned int h:1;
};
union SYMBOL {
char letter;
struct byte bitfields;
};
int main() {
union SYMBOL ch;
cout << "Enter your char: ";
while(true) {
ch.letter = getchar();
if(ch.letter == '\n') break;
cout << "You typed: " << ch.letter << endl;
cout << "Bite form = ";
cout << ch.bitfields.h;
cout << ch.bitfields.g;
cout << ch.bitfields.f;
cout << ch.bitfields.e;
cout << ch.bitfields.d;
cout << ch.bitfields.c;
cout << ch.bitfields.b;
cout << ch.bitfields.a;
cout << endl << endl;
}
}
最佳答案
请参阅ASCII表以了解所获得的输出:
a
的十进制值为97,二进制b
的十进制值为98,二进制等等。
关于c++ - 字符C++的位表示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26636261/