Bit-String Flicking
videos: https://www.youtube.com/watch?v=XNBcO25mgCw
https://www.youtube.com/watch?v=IeMsD3harrE (Basic) https://www.youtube.com/watch?v=jbKw8oYJPs4 (Advanced)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators
Bit strings (strings of binary digits) are frequently manipulated using logical operators, shifts, and circulates. Mastering this topic is essential for systems programming, programming in assembly language, and optimizing code.
A typical use of bit strings is to maintain a set of flags. Suppose that associated with a data structure in a program, there are 8 options each of which can be either “on” or “off”. One could maintain this information using an array of size 8, or one could use a single variable (if it is internally stored using 8 bits or 1 byte, which is usually the case) and use 8 bits to record each option. In addition to saving space, the program is often cleaner if a single variable is involved rather than an array. Incidentally, bit strings are usually used to maintain a set where values are either in the set or not. Many languages such as C++ and Python also support bit-wise operators.
The logical operators which will be used are: AND (&), OR (|), XOR (⊕), and NOT (~). These operators examine the operand(s) on a bit by bit basis. For example, (10110 AND 01111) has a value of 00110. The AND, OR, and XOR are binary operators; the NOT is a unary operator. The category description of Boolean Algebra/Digital Electronics has a complete description of each logical function. The following chart summarizes this information:
p q p AND q p OR q p XOR q NOT p 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 0
Logical shifts (LSHIFT-x and RSHIFT-x) “ripple” the bit string x positions in the indicated direction. Bits shifted out are lost; zeros are shifted in at the other end. Circulates (RCIRC-x and LCIRC-x) “ripple” the bit string x positions in the specified direction. As each bit is shifted out one end, it is shifted in at the other end. Thus, for this category, the size of a bit string is fixed; it cannot be lengthened or shortened by any of the logical operators, shifts, or circulates. If any bit strings are initially of different lengths, all shorter ones are padded with zeros in the left bits until all strings are of the same length. The following table gives some examples of these operations:
p LSHIFT-2 p RSHIFT-2 p LCIRC-3 p RCIRC-3 p 01101 10100 00011 01011 10101 10 00 00 01 01 1110 1000 0011 0111 1101 1011011 1101100 0010110 1011101 0111011
The order of precedence (from highest to lowest) is: NOT; SHIFT and CIRC; AND; XOR; and finally, OR. In other words, all unary operators are performed on a single operator first. Operators with equal precedence are evaluated left to right; all operators bind from right to left.
Sample Problems
Problem: Evalue the following expression:
(RSHIFT-1 (LCIRC-4 (RCIRC-2 01101)))