Bit-String Flicking

Bit-String Flicking Worksheet #1, Bit-String Flicking Worksheet #2, Bit-String Flicking Worksheet #3, Bit-String Flicking Worksheet #4

A bit string is a sequence of bits (1's and 0's). Bit strings are used for various reasons by computer scientists.

Bit-string flicking is the process of operating on bit strings with logical operators, shifts, and circulates.

I. Logical Operators

The logical operators that are used in bit string flicking are:

AND, OR, XOR, and NOT

Note that these Boolean, logical operators are often written in upper-case letters. Sometimes, we use the following symbols in place instead of these operators.

AND is represented as &
OR is represented as | (known as the vertical pipe)
XOR is represented as (in C++, it is represented as the caret, ^)
NOT is represented as ~ (known as a tilde)

You apply these logical operators on a "bit by bit" basis. Of course, the bit 1 is evaluated as TRUE and the bit 0 is evaluated as FALSE. The logical operators apply just like they do in Boolean logic. Remember that AND is similar to mathematical multiplication and that OR is like addition.

Some real easy examples:

1 & 1 = 1         1 | 0 = 1         0 | 0 = 0         1 & 0 = 0         1  0 = 1

More complicated examples:

101 & 110 = 100             1100 | 0100 = 1100         101  110 = 011

The color coding in the examples above depict the fact that the logical operators work on a bit by bit basis since the like colored bits are evaluated 2 at a time.

The NOT operator applies just like the negative sign in mathematics except, of course, the "negative" of 1 is 0 in Boolean logic and vice versa.

Examples:

~1 = 0        ~10111 = 01000        ~(111 & 100) = ~100 = 011

Note that in the case of bit strings, you may not disregard leading 0's as you would in mathematics or even binary arithmetic. That is, in binary arithmetic one can subtract the binary numbers

         111
   -    101
        -----
         010

and simplify that answer to simply 10

but in bit string flicking you must NEVER drop off a leading 0 bit because every bit has meaning usually. Since bit strings are often used as a set of flags or since the lead bit may indicate a number being negative or positive, you cannot disregard the leading 0 if there is one.

Another logical operator is the XOR. It is sometimes called the "eXclusive OR " operator. It works as the phrase "...either....or....but not both " is defined in the English language. For example,

a XOR b is true if either bit a is TRUE or bit b is TRUE but not both. So,

1 XOR 0 = 1    and 0 XOR 1 = 1    and 1 XOR 1 = 0     and 0 0 = 0

10110 XOR 00101 = 10011 (remember to evaluate the expression on a bit by bit basis)

Yet another logical operator is XNOR. It is sometimes called the eXclusive NOR. It is true if both of its arguments are the same (either 1 or 0). It is the opposite of XOR and is equivalent to ~(A XOR B).

For example, 1011 XNOR 1110 = 1010

You should memorize or be able to reproduce the following truth table that summarizes the AND, OR, XOR, and NOT logical operations.

A
B
A AND B
A OR B
A XOR B
A XNOR B
NOT A
 
0
0
0
0
0
1
1
0
1
0
1
1
0
1
1
0
0
1
1
0
0
1
1
1
1
0
1
0


II. Shifting

Another set of operations that can be performed on bit strings is "logical shifting." Shifting "ripples" the bit string to the right or left. Bits are shifted out of the bit string entirely and zero bits are shifted in from the other end. Be careful never to change the overall size of a bit string. One 0 shifts into the bit string to take the place of each bit that is shifted out.

Examples:

LSHIFT-3 10001 = 01000

The bit string "rippled" to the left losing 3 original bits (the blue ones) and three trailing 0 bits (the black ones) were added.

RSHIFT-1 1011 = 0101

III. Circulating

Finally, there is another similar bit string operation known as the circulate. Circulates do just that, they circulate the bits off of one end of a bit string into the other end. No zeros are introduced as in the shifts. No bits are lost they are just rearranged. If however a bit string circulates by the same amount as its length, it will have circulated back to its original state!

Examples:

RCIRC-3    10111 = 11110         RCIRC-1   101 = 110

LCIRC-5    111100101 = 010111110        LCIRC-4  1101 = 1101

Of course, these operations can be compounded into larger expressions. So you must be careful to evaluate the expressions according to the order of operations which is (from highest to lowest precedence):

  1. NOT
  2. SHIFT and CIRC (evaluate from right to left in the expression)
  3. AND (which is similar to multiplication in normal mathematics)
  4. XOR and XNOR (evaluate from left to right in the expression)
  5. OR (which is similar to addition in normal mathematics)

The C++ textbook that we use in Honors/AP Comp Science provides an excellent tutorial for "bitwize"operators.


ACSL Home Page | Mr. Minich's Wyo Home Page | Minich.com Web Design