Skip to main content
Engineering LibreTexts

2.7: Boolean Logical and Bitwise Operators

  • Page ID
    83021
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    2.7.1 Boolean Operators

    Boolean operators are operators which are designed to operate on Boolean or binary data. They take in one or more input values of 0/1 and combine those bits to create an output value which is either 0/1. This text will only deal with the most common Boolean operators, the unary operator NOT (or inverse) and the binary operators AND, OR, NAND, NOR, and XOR. These operators are usually characterized by their truth tables, and two truth tables are given below for these operators.

    A NOT
    0 1
    1 0

    Table 1-6: Truth table for NOT operator

    Input

    Output

    A

    B

    AND

    OR

    NAND

    NOR

    XOR

    0

    0

    0

    0

    1

    1

    0

    0

    1

    0

    1

    1

    0

    1

    1 0 0 1 1 0 1
    1 1 1 1 0 0 0

    Table 1-7: Truth table for AND, OR, NAND, NOR, and XOR

    Chapter 2.7.2 Logical Boolean Operators

    There are two kinds of Boolean operators implemented in many programming languages. They are logical operators and bitwise operators. Logical operators perform Boolean operations to obtain a single value at the end. For example, in Java a programmer might write:

          if (x != 0)  {
              //do something
          }
    

    The purpose of an if statement is generally to decide whether or not to enter the statement or code block associated with the if test. The purpose of this statement is just to derive a single logical answer, either true or false. This is the classical example of a binary value being a logical value.

    Because logical expressions only care about whether the result is true or false, it is possible to implement expressions that short-circuit. In a short-circuit expression the expression is evaluated until a valid answer is determined. Once the answer is determined, any other parts of the expression are skipped, or short-circuited. As we will see later, this is only possible for Boolean operations that are used for logical expressions and not true for general Boolean operations.

    To see how this short-circuit behavior can be used, consider the following expression:

          if ((x != 0) && (y = z / x));
    

    Note that a “;” is placed after this if test, which means that there is nothing to do if the expression is true. So, what is the purpose of this expression? Note that if \(x == 0\) the first part of the expression is false, so the second part of the expression \(“(y = z / x)”\) is not executed. This code fragment with the if statement is protecting the division operation from a zero divide.

    Note that logical operations are looking to resolve to a single bit and so short circuiting is possible and enforced in many (if not most) HLL’s.

    The important take away from this is that logical operators are Boolean operations that act only on one binary value and are short circuiting operators.

    Chapter 2.7.3 Bitwise Boolean Operators

    On the other hand, bit-wise operators are not short circuiting. Consider the following problem. A programmer wants to write a toLower method which will convert an upper case letter to a lower case letter. In chapter 1.3 it was pointed out that the difference between an upper case letter and a lower case letter is that in a lower case letter the bit 0x20 (\(00100000_2\)) is 1, whereas in the upper case letter it is zero. So, to convert from an upper case letter to a lower case letter, it is only necessary to OR the upper case letter with 0x20, turning on this bit. In pseudo code this could be implemented as follows:

          char toLower(char c) {
              return (c | 0x20)
          }
    

    In this case the bitwise OR operator, |, needs to operate on every bit in the variables. Therefore, the | operator is not short circuiting, it will process every bit regardless of whether or not a previous bit would cause the operation to fail. It is not seeking a logical result, but to apply the Boolean (not logical) operator to all of the bits.

    It is possible to use bitwise operators in place of logical operators, but it is usually incorrect to do so. For example, in the previous if statement, if a bitwise operator had been used, no short circuiting would have occurred and the zero divide could occur.

          if ((x != 0) & (y / x > 4))
    

    Many languages such as C/C++, Java, C#, etc, have both logical (short circuiting) and bitwise operators. In most cases the single character operator is a bit wise operator (e.g., &, |) and the double character operator is the logical operator (e.g., &&, ||).

    Now to make things more confusing, in MIPS only bitwise operations are implemented, and they are called logical operators. Yes, this is confusing to new assembly language programmers, and there is no good way to reconcile this, so the user is cautioned to read the material and programs carefully.


    2.7: Boolean Logical and Bitwise Operators is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?