从 16 位寄存器获取值

Getting value from a 16-bit register

我有一个16位的寄存器

所以最后我寄存器中的写入值为 30 因为:

(0 x 1) + (3 x 2) + (1 x 8) + (1 x 16) = 30  

现在我想做反向操作以获取位中写入的值:

(30 & 1) / 1 = 0  
(30 & 2) / 2 = 1 (this is wrong, it should be 3)  
(30 & 8) / 8 = 1  
(30 & 16) / 16 = 1  

我做错了什么?

(30 & 2) / 2 = 1 (this is wrong, it should be 3)

这是错误的。要获得 2 位,您必须将 3 位移动其位位置。在这种情况下,您 and3 << 1 = 6

(30 & 6) / 2 = 3