简体   繁体   English

使用按位运算符从单个字节中形成多字节值

[英]using bitwise operators to form multi-byte value from individual bytes

A VGA monitor programming involves writing 16-bit values to certain memory locations for getting a character printed on the screen. VGA监视器编程涉及将16位值写入某些内存位置,以使字符打印在屏幕上。 This is how the different bits in this 16-bit values translate into the character being printed on the screen : 这就是这16位值中的不同位如何转换为要在屏幕上打印的字符的方式:

在此处输入图片说明

I used enums to represent different background/foreground colors : 我用枚举来代表不同的背景/前景颜色:

typedef enum
{ 
  eBlack , 
  eBlue , 
  eGreen ,
  eCyan,
  eRed,
  eMagenta,
  eBrown,
  eLightGrey,
  eDarkGrey,
  eLightBlue,
  eLightGreen,
  eLightCyan,
  eLightRed,
  eLightMagenta,
  eLightBrown,
  eWhite

} textColor ;

I wrote this function to create this 16-bit value based on three things, character user wants to print, foreground and background color he wants : 我编写此函数是基于三件事创建此16位值的:字符用户想要打印,他想要的前景色和背景色:

Assumptions : On my platform, int 32 bit, unsigned short 16 bit, char 8 bit 假设:在我的平台上,int 32位,unsigned short 16位,char 8位

void printCharacterOnScreen ( char c , textColor bgColor, textColor fgColor ) 
{
    unsigned char higherByte = ( bgColor << 4 ) | (fgColor ) ;
    unsigned char lowerByte  = c  ;

    unsigned short higherByteAs16bitValue = higherByte & 0 ;
    higherByteAs16bitValue = higherByteAs16bitValue << 8 ;

    unsigned short lowerByteAs16bitValue = lowerByte & 0 ;

    unsigned short complete16bitValue = higherByteAs16bitValue & lowerByteAs16bitValue ;

    // Then write this value at the special address for VGA devices.
}

Q. Is the code correct and is it the write way to create such a value ? :代码是否正确?创建此值的写方法是否正确? Is there some standard way to do this kind of operation ? 有某种标准的方法可以进行这种操作吗?

Q. Will my approach be platform independent ? 问:我的方法会独立于平台吗? Any other comments on the code ? 对代码还有其他意见吗?

It is very close - but the line 很近-但是线

unsigned short lowerByteAs16bitValue = lowerByte & 0 ;

will clear the lower byte. 将清除低位字节。 You probably want 你可能想要

unsigned short lowerByteAs16bitValue = lowerByte;

You don't really need the lowerByte , and could use c directly - but your code is more readable. 您实际上并不需要lowerByte ,可以直接使用c但是您的代码更具可读性。

Next problem: 下一个问题:

unsigned short complete16bitValue = higherByteAs16bitValue & lowerByteAs16bitValue ;

Again, you are using the & operator. 同样,您正在使用&运算符。 I think you mean to use | 我认为您的意思是使用| (OR) as you did a few lines earlier. (OR),就像您之前所做的几行一样。

While this now looks "portable", you still need to be careful when writing to the hardware since low-endian and high-endian platforms may switch the byte around - so while your code works up to the point of returning the correct 16 bit word, you may need to pay attention to how that word gets written to the VGA card. 尽管现在看起来“可移植”,但在写入硬件时仍需要小心,因为低端和高端平台可能会切换字节-因此,在代码工作到返回正确的16位字的时候,您可能需要注意该单词如何写入VGA卡。 That's the line you had commented out... but it is critically important! 那是您注释掉的那一行……但这非常重要!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM