Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • IO on the ELEV-8 Flight Controller

IO on the ELEV-8 Flight Controller

AND and OR

AND and OR

Since binary digits represent “On” and “Off”, we can use operators with them that reflect real-world operations – think about switches and light bulbs.

If we have two switches and a light bulb to tell us if something is on or off, we can represent concepts like AND and OR.  Let’s start with AND.

Look carefully at the diagram below.  In order for the light to come on, BOTH switches have to be turned on:

If we AND to binary numbers, a bit has to be 1 in both numbers for the bit to be a 1 in the new number.

The symbol for AND’ing two numbers is “&“.  Here is an example of two binary numbers being AND’ed together:

The only digits that remain as 1’s are the digits where there was a 1 in both numbers.

OR can be used similarly.  Take a look at the diagram below.  Only one of the switches needs to be on to turn on the light:

If we OR two binary numbers, only one of the bits has to be a 1 for the bit to be a 1 in the new number.

The symbol for OR’ing two numbers is “|“.  Here is an example of two binary numbers being OR’ed together:

Try this

  • Open a new PropellerC project in SimpleIDE.
  • Copy the following code and paste it into the main window:
/*
AND and OR operators example
*/

#include "simpletools.h"                           // Include simpletools library

unsigned int numberA;                              // Declare variables
unsigned int numberB;
unsigned int numberC;

int main()                                         // Main function
{
  while(1)
  {
    // Prompt user to enter two binary numbers    
    print("Enter a binary number (up to 8 digits): ");
    scan("%b\n", &numberA);                      
    print("Enter another binary number (up to 8 digits): ");
    scan("%b\n", &numberB);                      

    // Display the results AND'ed together
    print("\n\nAND example:\n");
    print("\t  %08b\n\t& %08b\n\t__________\n", numberA, numberB);

    numberC = numberA & numberB;                   // AND the numbers together

    print("\t= %08b", numberC);

    // Display the results OR'ed together
    print("\n\nOR example:\n");
    print("\t  %08b\n\t| %08b\n\t__________\n", numberA, numberB);

    numberC = numberA | numberB;                   // OR the numbers together

    print("\t= %08b\n\n", numberC);                
  }
}
  • Click “Program > Run With Terminal”

You should se something like this:

  • Try different combinations of numbers until you have a better understanding of how the AND and OR operations work with binary numbers.

Printer-friendly version
IO Parts and Prep
Prev
Pin Control Registers
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress