0

I'm working on a driver, and I'm having trouble finding a way to manipulate the bits I want to on a digital register.

The register has 20 bits (0-19) and I'm only concerned with writing over the last two (19,18). These bits are part of a hex representing that section, so to visualize, the #'s are what I want to change:

Hex 5 B 0 F

[##00 0101 1011 0000 1111]

All of the other bits must be kept as they are—the example above is arbitrary, so the masking has to handle any combination of bits.

I've tried creating a mask with any combination of & and | with 0xFFFFF and also 0x00000 but neither change just the first two bits of the hex that will be written.

Any ideas to get this to function would be great; and if you could explain how the solution works, even better.

IronMage
  • 41
  • 7
  • What is the expected result? Should the ## be always 0? or always 1? or some arbitrary combination? – Abhinav Jun 03 '15 at 19:34
  • 1
    Since my answer was cut short when the question was singlehandedly marked as duplicate and the linked answer only gives general hints about bit-fiddling: You need `a = (a & ~(3 << 18)) | (i << 18);` Here, `a` is the original value and `i` is a value from 0 to 3 that you wish to set bits 18 and 19 to. – M Oehm Jun 03 '15 at 19:57

0 Answers0