Jump to content

n00b tutorial - AND; OR and NOT

Recommended Posts

I'm a total n00b, and from what I have seen, there are a lot of us here so I figured I'd share what I've been learning and maybe help some others along the way! While running demos and looking at the code that makes them tick you will come across things like this:



P1OUT &= ~BIT6


And to a n00b this looks like gibberish! Lets break it down and learn! - as taught to me by my mentor & friend Sven (Thanks bub!).


There are three basic functions in play here AND; OR and NOT. Lets start with NOT.


1101 1010 <- binary representation of a byte (8bits)


What NOT does is INVERT the bits:



1101 1010

0010 0101 <- RESULT


What was ON is now OFF (and vice versa)


Now, lets look at OR. While NOT operates on ONE value, OR (as well as AND) operate on TWO values and combines them in a way to create a result. OR sets all the bits that are in either (a) OR (B).


1100 0000 (a)


0001 0001 (B)


1101 0001 <- RESULT


Another example:

1100 001 (a)


1101 000 (B)


1101 001 <- RESULT (same as the first result)


If a bit at a certain position is set to 1 in either the first (a) OR the second (B) var, the bit in the result will be set to 1 also.


Now, lets look at AND. The difference between OR and AND is that the bit in the result will be set only if BOTH the first (a) and the second (B) var are set.


1100 0001 (a)


1000 0100 (B)


1000 0000 <-RESULT


Only the topmost bit (BIT7) is set in (a) AND (B), so only the topmost bit is set in the result.


SO - NOW WHAT!? well, in C we don't write OR, AND and NOT...we use symbols.


& is AND

| is OR

~ is NOT


Lets look at a line of code:




What this line of code does is MASK bit number 6. It takes what is currently in P1OUT then does an AND with EVERYTHING except BIT6 and writes it back to P1OUT. Whatever was written to P1OUT still be there EXCEPT for BIT6, which will be set to zero. We can make this line shorter:


P1OUT &= ~BIT6


On a LaunchPad it turns off the Green LED located at 1.6 - ISN'T THAT GREAT TO KNOW!?


When you want to change a bit in a port, you CANNOT access just that one bit. You can only write a WHOLE byte to memory, which effects all 8 port bits. So, since you do not want to change the other bits you need to read the current state, leaving all the other bits unchanged by using AND with ALL bits except the one you want to clear (BIT6).


Lets say that P1OUT looks like this:


1101 0001


BIT0, BIT4, BIT6 and BIT7 are set (Zero is rightmost Seven is topmost). Now you create your mask by doing a NOT on a single BIT (BIT6). BIT6 Looks like this:


0100 0000


NOT BIT6 (~BIT6) Looks like this:


1011 1111


Now, if you do an AND operation with this MASK all bits will remain unchanged except BIT6 which will always result in zero. You write the result back to the port byte and you have only affected ONE bit (BIT6) while all the others remain the unchanged.




You just OR the port with the desired bit:




If BIT6 is already set, there is no change. If BIT6 was not set it is now. On a Launchpad the green LED would turn ON or STAY ON.


Just for fun:


P1OUT &= ~(BIT6|BIT0)


This line of code will turn OFF BOTH LED's on a Launchpad.


Happy coding!


Link to post
Share on other sites
  • 7 months later...

You have on Idea how much I want to thank you. I took C++ and was never taught what any of these operations were. I bought the Launchpad kit because I have been interested in learning micro-controllers for a while now and the price just made it difficult to resist. I have literally spent days trying to figure out what all those symbols meant in the example code. Thank you.

Link to post
Share on other sites
  • 2 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...