• Why use bit level operators?

    To control hardware

    One example:

    Microchips have pins that send signals to hardware (e.g. LED lights, USB cables)

    A set of pins can be represented by a sequence of digits:

  • One's Complement Operator

    What is the one's complement operator?:

    • ~
    • the one's complement operator changes:
      • 1s to 0s
      • 0s to 1s

    Why one's complement operator important?:

    Toggling pins on and off is fundamental to controlling hardware

    How to use one's complement operator?:

    ones_complement.c:

      Things to note:
    • all bits are flipped
  • Logical Bitwise Operators

    What are the logical bitwise operators?:

    • bitwise and: &
      • if bit1 AND bit2 are 1, then the result is 1
      • otherwise the result is 0
    • bitwise or: |
      • if bit1 OR bit2 is 1, then the result is 1
      • otherwise the result is 0
    • bitwise exclusive or: ^
      • if ONLY bit1 OR bit2 is 1, then the result is 1
      • otherwise the result is 0
      • common notation: bit1 XOR bit2
    • summary:
      Bitwise Logical Operators
      bit1bit2bit1 & bit2bit1 | bit2bit1 ^ bit2
      11110
      10011
      01011
      00000

    Why are logical bitwise operators important?:

    they are the building blocks of comparisons

    How to use logical bitwise operators?:

    logical_bitwise.c:

      Things to note:
    • the comparison is made on a bit by bit basis
    • nth bit is compared with nth bit

    How to mask with logical bitwise operators?

    mask.c:

      Things to note:
    • masks work like filters
    • masks affect certain bit position and leave other bit positions unaffected
    • the concept of masks can be applied to many applications, i.e. mask a matrix to only show a portion of an image, subnet mask for IP addresses in a network
  • Shift Operators

    What are the shift operators?:

    • left shift: <<
      • shifts the bits a certain number of bits over to the left
      • a certain number of bits that are furthest to the left will be lost
    • right shift: >>
      • shifts the bits a certain number of bits over to the right
      • a certain number of bits that are furthest to the right will be lost

    Why are shift operators important?:

    How to use shift operators?:

    shift.c:

      Things to note:
    • some bits will be lost
  • Bitwise Operation with Lights:

    Blinking Light:

    Running Light: