在C / C ++中读取/写入半字节(无位字段) [英] Reading/Writing Nibbles (without bit fields) in C/C++
本文介绍了在C / C ++中读取/写入半字节(无位字段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种简单的方法可以在不使用位域的情况下读取/写入字节半字节?
我将始终需要读取两个半字节,但需要分别写入每个半字节。
Is there an easy way to read/write a nibble in a byte without using bit fields? I'll always need to read both nibbles, but will need to write each nibble individually.
谢谢!
推荐答案
使用掩码:
char byte;
byte = (byte & 0xF0) | (nibble1 & 0xF); // write low quartet
byte = (byte & 0x0F) | ((nibble2 & 0xF) << 4); // write high quartet
您可能希望将其放入宏中。
You may want to put this inside macros.
这篇关于在C / C ++中读取/写入半字节(无位字段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文