我想读,并根据下表写从/到unsigned char类型一个unsigned char型的特定位: 如何读取/写入
例如我有以下变数:
unsigned char hsi_div = 0x01; /* HSI/2 */
unsigned char cpu_div = 0x05; /* Fmaster/32 */
我想写hsi_div
对位4,3和cpu_div要位2,1,0(想象整个焦炭名为CLK_DIVR):
CLK_DIVR |= hsi_div << 4; //not correct!
CLK_DIVR |= cpu_div << 2; //not correct!
并让说我想读回寄存器,以确保我做到了正确:
if(((CLK_DIVR << 4) - 1) & hsi_div)) { /* SET OK */ }
if(((CLK_DIVR << 2) - 1) & cpu_div)) { /* SET OK */ }
我的按位操作有什么问题!?我没有得到正确的行为。
你想要使用8位的uint8_t。无符号字符*至少为8位*,但可能更多。 –
使用固定宽度类型(请参阅'stdiont.h'),而不是标准整数类型。并选择你使用的语言。 C和C++是不同的语言! – Olaf
@ThomasMatthews对于我使用的编译器和平台无关紧要 –