logo image - link to home page

Products | Support | Resources | PurchaseContact  | Basket  | Checkout

  Home
  What's New
  Products
  Distributors
  Purchasing Info
  Developer Resources
  Support
  Upgrades
  Site Index
  Contact Us
  Pb-Free Info (RoHS)
 

 

keyword search:
 


 

Home:
  Developer Resources:

Programming Help
    Sample Programs
    Search the
PICBASIC™ List
    PICBASIC PRO™
Compiler Manual
    PICBASIC™ Compiler
Manual
    Serin2/Serout2 Modes
    ASCII Character Set
    Number Conversion
    Floating Point
Routines
    PBP Debug Monitor
    Articles and Tutorials

Hardware Help
    PICmicro® MCU
Selection Guide
    PIC16F62x Datasheet
    PIC16F84A Datasheet
    PIC16F87x Datasheet
    Parts / Vendor List
    PICPROTO™ Boards
    LAB-X1 Docs
    LAB-X2 Docs
    LAB-X20 Docs
    LAB-X3 Docs
    LAB-X4 Docs
    LAB-XUSB Docs
    LAB-XT Docs
     

 Other Resources
    MicroCode Studio
IDE for Windows
    melabs Loader
HEX files
    Links to other sites

Bitwise (Boolean) logic operators


Because microcontrollers store numbers in binary, it is possible to use a special kind of operator on data.  These operators are described as Bitwise logic, because they obtain results based on the logical relationships of individual bits.  Bitwise logic is also known as Boolean logic or Boolean math.

There are 7 Bitwise operators:

PICBASIC Operator Description

~

Bitwise NOT

&

Bitwise AND

|

Bitwise OR

^

Bitwise Exclusive OR (XOR)

&/

Bitwise NOT AND (NAND)

|/

Bitwise NOT OR (NOR)

^/

Bitwise NOT Exclusive OR (XNOR)

The simplest of these is the NOT (~) operator.  It returns the logical opposite of the tested bit.  This operator is unique in that it only requires 1 bit as input.  All the other operators require 2 bits.


Using Bitwise operators on individual bits

 

NOT (~)

When describing the function of Bitwise operations, we use a diagram call a "truth table".  It shows the input bit (or bits) on the left, and the result on the right.  Since the NOT operator only has one input, the truth table looks like this:

A ~A
0 1
1 0
PICBASIC example:

result = ~ A

 

AND (&)

AND compares 2 bits and returns a logic high only if both input bits are high.

A B A & B
0 0 0
0 1 0
1 0 0
1 1 1
PICBASIC example:

result = A & B

 

OR (|)

OR compares 2 bits and returns a logic high if either or both inputs are high.

A B A | B
0 0 0
0 1 1
1 0 1
1 1 1
PICBASIC example:

result = A | B

XOR (^)

XOR compares 2 bits and returns a logic high only if a single input is high.  If both inputs are high, it returns a logic low

A B A ^ B
0 0 0
0 1 1
1 0 1
1 1 0
PICBASIC example:

result = A ^ B

NAND (&/)

NAND compares 2 bits and returns a logic low if both inputs are high.

A B A &/ B
0 0 1
0 1 1
1 0 1
1 1 0
PICBASIC example:

result = A &/ B

NOR (|/)

NOR compares 2 bits and returns a logic low if either or both inputs are high.

A B A |/ B
0 0 1
0 1 0
1 0 0
1 1 0
PICBASIC example:

result = A |/ B

XNOR (^/)

XNOR compares 2 bits and returns a logic low only if a single input is high.  If both inputs are high, it returns a logic high.

A B A ^/ B
0 0 1
0 1 0
1 0 0
1 1 1
PICBASIC example:

result = A ^/ B


Using Bitwise operators on bytes and words

When you use byte or word sized data as inputs for Bitwise operators, the result can be as long as your longest input.  The operator will perform a comparison of each bit in both input variables, and store the result in the corresponding bit location of the result variable.

A common example is the AND operator used to mask certain bits in a byte. It works as a mask because it always returns 0 when one of the inputs is 0.  Therefore, when we use "bytevar & %00001111", the top 4 bits of the result will always be 0.  The lower 4 bits won't change, because ANDing something with a logic 1 makes the result equal to the input.

Here's a modified truth table showing 8-bits being ANDed all at once.

bit position byte A byte B A & B
7 1 0 0
6 0 0 0
5 1 0 0
4 1 0 0
3 0 1 0
2 1 1 1
1 1 1 1
0 0 1 0
PICBASIC example:

A = %10110110
B = %00001111
result = A & B

'(00000110)

Here are some example equations using the input values above:

%10110110 | %00001111 = %10111111     'use OR to mask bits with logic 1

%10110110 ^ %00001111 = %10111001     'use XOR to invert selected bits

 

Copyright 2008
microEngineering Labs, Inc.
Box 60039
Colorado Springs CO 80960
(719) 520-5323
(719) 520-1867 fax
email: support@melabs.com

Last Updated: 01/04/2008

Home | What's New | Products | Support
Resources
| Purchase | Index Contact

PIC, PICmicro, dsPIC, and  MPLAB are registered trademarks of Microchip Technology Inc. in the USA and other countries.  PICBASIC, PICBASIC PRO, PICPROTO, and EPIC are trademarks of Microchip Technology Inc. in the USA and other countries.   BASIC Stamp is a trademark of Parallax, Inc.

Microchip third-party partner  Microchip Consultant Program Member