I never really thought of the fact that NAMING the bit fields would make the code more readable. I'm not a veteran "C" programmer, so I'm not used to such constructs. Thanks . --=20 Larry Bradley =20 Orleans (Ottawa) Canada=20 On Thu, 2014-05-01 at 21:36 -0400, John J. McDonough wrote: > On Thu, 2014-05-01 at 17:11 -0400, Larry Bradley wrote: > > Thanks. Somewhere I read that the bitfield stuff was not efficient. > >=20 > > I tried it. The code using bitfields was the same size as using a mask. > > And the mask code is a bit (no pun intended) clearer! >=20 > I'm kind of surprised nobody compared the generated code. The code > produced by XC16 is identical: >=20 > if ( myBits & 0x0002 ) > myTestWord =3D 87; >=20 > if (myBitfield.b1) > myTestWord =3D 83; >=20 > 53: if ( myBits & 0x0002 ) > 00029A 804000 MOV myBits, W0 > 00029C 600062 AND W0, #0x2, W0 > 00029E 500FE0 SUB W0, #0x0, [W15] > 0002A0 320002 BRA Z, 0x2A6 > 54: myTestWord =3D 87; > 0002A2 200570 MOV #0x57, W0 > 0002A4 884020 MOV W0, myTestWord > 55: =20 > 56: if (myBitfield.b1) > 0002A6 BFC802 MOV.B myBitfield, WREG > 0002A8 604062 AND.B W0, #0x2, W0 > 0002AA 504FE0 SUB.B W0, #0x0, [W15] > 0002AC 320002 BRA Z, 0x2B2 > 57: myTestWord =3D 83; > 0002AE 200530 MOV #0x53, W0 > 0002B0 884020 MOV W0, myTestWord >=20 > The real question is, does the optimizer have better luck with one over > the other. Problem with that is that it is real hard to come up with > bogus code the optimizer won't simply optimize out of existence. >=20 > I would argue that the mask code is only clearer if > 1) You are doing some bogus example like this or > 2) You are an assembler programmer >=20 > With the bitfield you can give the bits names, so for example you could > say something like: >=20 > if ( LCDstatus.LCDisBusy ) >=20 > rather than >=20 > if ( LCDstatus & 0b0000000000100000 ) >=20 > Granted, you could say >=20 > #define LCDBUSY 0x0020 >=20 > then >=20 > if ( LCDstatus & LCDBUSY ) >=20 > but I still don't find that as clear as the bitfield example, which is > probably why Microchip goes to the trouble of naming all the bits in the > status words. You can even give the bit a macro name, making the code > even more clear. Something Microchip does with many of the SFR bits. > So with a define I could simply say >=20 > if ( BUSYBIT ) >=20 > far more readable than >=20 > if ( LCDstatus & 0b0000000000100000 ) >=20 >=20 > --McD >=20 >=20 --=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 .