On 11 Mar 2015 at 23:16, Josh Koffman wrote: > Hi all, >=20 > I've seen a few different ways to do this, and I'm not sure if there > are better or worse ways on this one. I am working in XC8, and I want > to be able to set multiple bits in a register at once. In assembly, > I'd mask off the bits I wanted to effect with an AND, then OR in my > desired value. >=20 > For example, if I had a register called target, and I wanted to modify > bits 2 and 3, I'd first AND target with b'11110011', then take the > data I wanted to put in there (in this case b'11'), and OR target with > b'00001100'. >=20 > Can I combine these commands in C? Or is there a better way? I think I co= uld do: >=20 > target |=3D 0b11110011; > target &=3D 0b00001100; >=20 > But what about: >=20 > target =3D (target | 0b11110011) & 0b00001100; Yes, you are almost perfectly on "target" (excuse the pun) and this is inde= ed the=20 way it's done in C. Small thing though, you say you would do the AND'ing fi= rst and=20 then OR it, which is correct, but your example shows doing it in the opposi= te order.=20 Likely what you meant was: target &=3D 0b00001100; target |=3D 0b11110011; And yes you could do: target =3D (target & 0b11110011) | 0b00001100; Which I think could also be written: (target & 0b11110011) |=3D 0b00001100; --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .