 |
|
 |
| Parallax Forums > Public Forums > Propeller Chip > Best way to address bits in a byte? | Forum Quick Jump
|
 |  Mike Green Registered Member

       Date Joined Oct 2004 Total Posts : 15579 | Posted 3/28/2008 6:25 AM (GMT -7) |   | byteVar &= !Mask ' To set a bit off byteVar |= Mask ' To set a bit on byteVar ^= Mask ' To toggle a bit if byteVar & Mask ' To test if a bit is on where CON Mask = %10000000 ' or something similar | | Back to Top | | |
 |  Rayman Registered Member
        Date Joined Jul 2007 Total Posts : 3127 | Posted 3/28/2008 6:27 AM (GMT -7) |   | Usually, I'd do something like
Mask:=1<<bit
to make a mask | | Back to Top | | |
 |  tekochip Registered Member
        Date Joined Feb 2007 Total Posts : 56 | Posted 3/28/2008 6:33 AM (GMT -7) |   | | Here's some examples with functions to do the same:
CON #0, Serve, Dispense, Flash 'Declare some bits
PUB Demo |coils
OnBit(@coils, Serve) ToggleBit(@coils, Flash)
if TestBit(@coils, Flash) OffBit(@coils, Serve)
PUB OnBit(x, bit) {{Turn a bit on}} long[x]|=1<<bit
PUB OffBit(x, bit) {{Turn a bit off}} long[x]&= !(1<<bit)
PUB ToggleBit(x, bit) {{Toggle a bit}} long[x]^=1<<bit
PUB TestBit(x, bit) {{Return true if a bit is on}} return long[x] & (1<<bit) | | Back to Top | | |
 |  RichardF Registered Member
        Date Joined Mar 2007 Total Posts : 154 | Posted 3/28/2008 7:41 AM (GMT -7) |   | Thanks everyone. Makes me appreciate pBasic (smile) Richard | | Back to Top | | |
   |  RichardF Registered Member
        Date Joined Mar 2007 Total Posts : 154 | Posted 3/28/2008 9:40 AM (GMT -7) |   | Two more great ideas, thank you. Ariba, your use of the b registers is really cool. I have always assumed they were not addressable, since there are no associated pins. | | Back to Top | | |
  |  Rayman Registered Member
        Date Joined Jul 2007 Total Posts : 3127 | Posted 3/28/2008 10:43 AM (GMT -7) |   | Ariba, Beau,
I like this idea! I rarely use frqa, frqb, phsa, or phsb in the main cog. So, this is a very nice way to access bits! | | Back to Top | | |
 |  Javalin Got a Propeller, need some SPIN?
        Date Joined Jul 2004 Total Posts : 768 | Posted 3/28/2008 12:09 PM (GMT -7) |   | Chaps,
Surely:
if byteVar & Mask ' To test if a bit is on where 'CON Mask = %10000000 ' or something similar
should be
if byteVar & Mask == Mask ' to test if a bit is on
so - if byteVar is %00001111 & %00001000 would leave %00001000 so surely wouldn't equate to TRUE/FALSE or 1 or 0? ?? James Post Edited (Javalin) : 3/28/2008 7:37:55 PM GMT | | Back to Top | | |
 |  Rayman Registered Member
        Date Joined Jul 2007 Total Posts : 3127 | Posted 3/28/2008 1:10 PM (GMT -7) |   | | I think zero == false and non-zero == true... | | Back to Top | | |
  |  Javalin Got a Propeller, need some SPIN?
        Date Joined Jul 2004 Total Posts : 768 | Posted 3/29/2008 2:12 AM (GMT -7) |   | | | |
| Forum Information | Currently it is Thursday, July 29, 2010 5:17 PM (GMT -7) There are a total of 462,440 posts in 62,066 threads. In the last 3 days there were 90 new threads and 803 reply posts. View Active Threads
| | Who's Online | This forum has 20143 registered members. Please welcome our newest member, ME01. 55 Guest(s), 9 Registered Member(s) are currently online. Details John Abshier, Rayman, BradC, prof_braino, Sapieha, Gene Bonin, laser-vector, localroger, Nick McClick |
Forum powered by dotNetBB v2.42EC SP2.02 dotNetBB © 2000-2010 |
|
|