gpt4 book ai didi

c++ - 字符的位表示 C++

转载 作者:太空宇宙 更新时间:2023-11-03 10:28:07 26 4
gpt4 key购买 nike

这是我的字符位表示程序。但我不知道它向我展示的是正确还是错误的表示?有可疑单位(红色)。

enter image description here

你能解释一下这是什么(如果它是正确的)或者如果这些单位不应该是我的代码有什么问题吗?谢谢

#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表以了解您获得的输出:

enter image description here

  • a十进制为97,97二进制为01100001
  • b十进制为98,97二进制为01100010

等等。

关于c++ - 字符的位表示 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26636261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com