从16位寄存器中获取值

问题描述 投票:0回答:1

我有一个16位寄存器

  • 在位0,该值为0
  • 在位1 ... 2处,值为3
  • 在第3位,值为1
  • 在第4位,值为1

所以最后我的寄存器中的书面值是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  

我做错了什么?

bit-manipulation bit bit-shift cpu-registers bit-fields
1个回答
1
投票
(30 & 2) / 2 = 1 (this is wrong, it should be 3)

这是错的。要得到2位,你必须将3位移位。在这种情况下,你and3 << 1 = 6

(30 & 6) / 2 = 3
© www.soinside.com 2019 - 2024. All rights reserved.