gpt4 book ai didi

c++ - 这个结构的定义有什么问题?

转载 作者:行者123 更新时间:2023-11-28 00:37:04 28 4
gpt4 key购买 nike

我已经用如下结构定义了屏幕:

struct
{
private:

//data and attributes
char character : 8;
unsigned short int foreground : 3;
unsigned short int intensity : 1;
unsigned short int background : 3;
unsigned short int blink : 1;

public:

unsigned short int row;
unsigned short int col;

//a function which gets row, column, data and attributes then sets that pixel of the screen in text mode view with the data given
void setData (unsigned short int arg_row,
unsigned short int arg_col,
char arg_character,
unsigned short int arg_foreground,
unsigned short int arg_itensity,
unsigned short int arg_background,
unsigned short int arg_blink)
{
//a pointer which points to the first block of the screen in text mode view
int far *SCREEN = (int far *) 0xB8000000;

row = arg_row;
col = arg_col;
character = arg_character;
foreground = arg_foreground;
intensity = arg_itensity;
background = arg_background;
blink = arg_blink;

*(SCREEN + row * 80 + col) = (blink *32768) + (background * 4096) + (intensity * 2048) + (foreground * 256) + character;
}
} SCREEN;

但是当我在使用这个结构时使用超过'128' ASCII 码的字符时,数据会崩溃。我用 8 位定义了字符域。那么这个定义有什么问题呢?

最佳答案

在 C++ 编译器中,您显然使用 char 是有符号的,因此在一个 8 位字符中,您适合从 -128127 的值(假设使用负值的补码表示)。如果你想保证能够适应大于或等于 128 的值,请使用 unsigned char。

关于c++ - 这个结构的定义有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20470796/

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