Thread: PCA0MD &= ~0x40; Need some help cyphering

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    6

    PCA0MD &= ~0x40; Need some help cyphering

    so PCAOMD is a register on a micro processor. so usually they have an 8 bit register that you can turn a function on or off by binary code so if it was just PCA0MD = 0x40 that would be 01000000 so the seventh bit would be on, which in the actual case would not be correct the 7th bit needs to be off and i think some of the other bits on, so

    &= is bitwise AND ? what does ~ mean? could you explain and show the results.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,920
    From Bitwise operations in C - Wikipedia

    Bitwise NOT ~ [ones' complement (unary)]
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    763
    Useful macros:
    Code:
    #define BITSET(v,b) { (v) |= (1U << (b)); }
    #define BITRESET(v,b) { (v) &= ~(1U << (b)); }
    #define BITINVERT(v,b) { (v) ^= (1U << (b)); }
    
    /* Example: */
    BITRESET( PCA0MD, 6 );  // reset bit 6 (the 7th bit).
    BITSET( PCA0MD, 0 ); // set bit 0 (the 1st bit).

  4. #4
    Registered User
    Join Date
    Oct 2020
    Posts
    6
    so the first part & is a bitwise of 0x40 01000000 so would the first operand 0x40 be bitwised with the second operand of zeros? that would result in all zeros, then it would be bitwise not? the result would be all 1"s ?


    EDIT i see that now UNARY( one operand) so i am looking at this again
    Last edited by OutThere; 10-11-2020 at 08:52 AM.

  5. #5
    Registered User
    Join Date
    Oct 2020
    Posts
    6
    so would it be a bitwise AND 1's complement then NOT?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    No, it would be bitwise not of 0x40, then bitwise and with that result. The ~0x40 will almost certainly be computed at compile time rather than run time.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Tags for this Thread