W1015 Bitwise Operations

From Coder Merlin

Prerequisites[edit]

Bitwise Operations[edit]

A bitwise operation operates on one or more bit patterns at the level of their individual bits. It is a fast, simple action supported by the ALU (Arithmetic Logic Unit) and is used to manipulate values for comparisons and calculations. The normal operation is simply applied bit by bit.

NOT[edit]

NOT 0111
————————
  = 1000
WarnIcon.png
Caution
Note that because zero bits are "flipped" to one bits the actual size of the word matters. For example,
NOT 0111
  = 1000

when considering only a four-bit word, but in the case of an eight-bit word the correct answer would be:

  = 1111 1000

As such, it's usually best to left-pad the operand with zeroes before proceeding. For example,

NOT 0000 0111
  = 1111 1000

AND[edit]

The AND operation is useful for masking bits that are not interesting and then checking to see if a particular bit is set.

    0101
AND 0011
————————
  = 0001

OR[edit]

   0101
OR 0011
————————
 = 0111

XOR[edit]

The XOR operation is useful for inverting selected bits:

    0101
XOR 0011
————————
  = 0110

Because of this property, XOR is sometimes used to clear a register (to zero) by XOR’ing it with itself. In some cases this can be much faster than explicitly loading the value of 0.

    1010
XOR 1010
————————
  = 0000

Exercises[edit]

ExercisesIcon.png
Exercises
  •  M1015-10  Complete  Merlin Mission Manager  Mission M1015-10