On Fri, 2 May 2014, Dwayne Reid wrote: > At 07:36 PM 5/1/2014, John J. McDonough wrote: > >> I'm kind of surprised nobody compared the generated code. The code >> produced by XC16 is identical: >> >> if ( myBits & 0x0002 ) >> myTestWord =3D 87; >> >> if (myBitfield.b1) >> myTestWord =3D 83; >> >> 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 >> >> With the bitfield you can give the bits names, so for example you could >> say something like: >> >> if ( LCDstatus.LCDisBusy ) >> >> rather than >> >> if ( LCDstatus & 0b0000000000100000 ) > > This has been an informative thread for me. I'm a diehard assembler > programmer and my brain simply refuses to grok "C". But I keep > poking at it, hoping that the AH HA moment will occur. > > One of the many things I learned from PIC assembler guru Andrew > Warren was to define all of my bit fields with names that are used > later in my program. Your example above would be done as: > > #define LCDisBusy LCDstatus,1 > where LCDstatus has previously been defined in exactly the same > way. In use, I simply reference the defined name: > btfsc LCDisBusy > do something > > One of the things that really bugs me about C programs is that there > didn't appear to be any easy way to do this simple function. This > thread has shown me that there is something relatively close. > > Many thanks! Something else you might consider is using the absolute address of the=20 bit. In your example this would become: const int LCDisBusy =3D (&LCDstatus << 3) | 1; if ((*(char *)(LCDisBusy >> 3) & (1 << (LCDisBusy & 7))) !=3D 0) { do something } A good C compiler should compile this to: btfsc LCDstatus,1 goto lab1 do something lab1 Although the high level benefits are not immediately obvious consider what= =20 happens when the constant is used across functions calls :-) I use the above "absolute bit addressing" in the XCSB libraries and the=20 compiler is able to track the use of constants and optimise to "btfsc" and "bsf" etc., even through function calls. E.g. const LED1 =3D (&PORTA << 3) | 1 const LED2 =3D (&PORTB << 3) | 2 const RELAY =3D (&PORTC << 3) | 6 proc TURNON(int port) *(char *)(port >> 3) |=3D 1 << (port & 7) end proc proc main() TURNON(LED1) TURNON(LED2) TURNON(RELAY) end proc This compiles to: BSF PORTA,1 BSF PORTB,2 BSF PORTC,6 BTW note the lack of semicolons in the example XCSB code. Regards Sergio Masci --=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 .