Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
Per Westermark
08/25/12 11:04
Read: 713 times

Msg Score: +1
 +1 Good Answer/Helpful
#188166 - Clears 7 low bits but protects unknown # of high bits
Responding to: Raghunathan's previous message
~0x7f is inverting all bits.

So for an 8-bit number, you get 0x80.
For a 16-bit number, you get 0xff80.
For a 32-bit number, you get 0xffffff80.

And &= is a bit-and. It can never turn on bits, but can clear bits.

In this case, it will clear the 7 low bits.

If you knew you had a 8-bit wide port, you could have written:

P1MDIN &= 0x80;

But if the register was 16-bit wide, you had instead to write:

P1MDIN &= 0xFF80;

In short - going that extra route with ~ to invert the bits, you make sure that the & operation will handle whatever number of bits that the register happens to be wide.

List of 6 messages in thread
A simple doubt in C      Raghunathan      08/25/12 09:58      
   Clears 7 low bits but protects unknown # of high bits        Per Westermark      08/25/12 11:04      
      Pesrpective...      Raghunathan      08/25/12 21:18      
   Setting & clearing bits        Andy Neil      08/25/12 11:18      
      Concept is very clear now...      Raghunathan      08/25/12 21:08      
         Practical C Programming      Andy Peters      09/04/12 15:57      

Back to Subject List