To set or clear a bit within a byte variable based upon a bit in the same or other byte variable try:
(byte2 &= ~BITMASK2), (byte1 & BITMASK1) ? (byte2 |= BITMASK2) : 0;
Which could be encapsulated into a macro:
#define setmask(b1, m1, b2, m2) (((b2) &= ~(m2)), ((b1) & (m1)) ? ((b2) |= (m2)) : 0)
which you would use thusly:
setmask(byte1, BITMASK1, byte2, BITMASK2);
The result for the PIC is:
bcf _byte2,3
btfsc _byte1,2
bsf _byte2,3
Which is very effecient. If you don't want to clear the bit first (e.g. if it was a port pin) then it would be:
(!(byte1 & BITMASK1) ? (byte2 &= ~BITMASK2) : 0), (byte1 & BITMASK1) ? (byte2 |= BITMASK2) : 0;
PIC - Bit operations@
| file: /techref/language/ccpp/bittwid.htm, 1KB, , updated: 2008/3/19 02:23, local time: 2008/7/4 22:11,
38.103.63.16:LOG IN
|
| ©2008 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? Please DO link to this page! Digg it! <A HREF="http://www.piclist.com/techref/language/ccpp/bittwid.htm"> Efficient bit twiddeling in C</A> |
| Did you find what you needed? |
|
o List host: MIT, Site host massmind.org, Top posters @20080704 Apptech, Jinx, Xiaofan Chen, David VanHorn, Alan B. Pearce, Bob Axtell, William \Chops\ Westfield, Cedric Chang, Gerhard Fiedler, bean, * Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Shultz Electronics, Larry Williams, David VanHorn, Bryan Whitehouse, Timothy Weber, David Challis. Peter Todd. on-going support is MOST appreciated! * Contributors: Neil Narwani, David Cary, Elemer AM Nyiry, Philip J Taylor, Gus Calabrese of Omegadogs.com, Gautama Venegas, Patrick B. Murphy, William Chops Westfield, Peter Todd, Leslie Ellis |
|
.