Difference between revisions of "Bitwise Operations"
From Coder Merlin
m (Merlin moved page W1014 Bitwise Operations to W1015 Bitwise Operations: Improved navigation) |
|||
Line 2: | Line 2: | ||
* [[W1013 Boolean Algebra]] | * [[W1013 Boolean Algebra]] | ||
== | == Bitwise Operations == | ||
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. | 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. | ||
Revision as of 17:43, 16 July 2019
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
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 0111 ———————— = 1000
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
0101 OR 0011 ———————— = 0111
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