Open main menu

CDOT Wiki β

Changes

Bitwise Operations

246 bytes added, 14:23, 18 September 2023
OR
[[Category:Computer Architecture]]
''Bitwise Operations'' are [[Operation|operations]] that are performed on [[Word#Bit|bit-by-bit]] on one or two operands (input values) to produce an output value.
== Logical Bitwise Operations ==
=== OR ===
The ''OR'' logical operation takes two inputs, labelled A and B, and produces an output. Written out as a text description, '''OR''' means:
If A ''OR'' B are true, then the OUTPUT is true.
Therefore the bitwise ''OR'' is used to '''set bits''' to a value of 1.
For example, bits 4-:7 can be set to 1 while preserving the remaining bits in a byte by ORing a mask of 11110000 (0xF0):
{|cellspacing="0" cellpadding="5" border="1"
|Mask ||bgcolor="yellow"|1||bgcolor="yellow"|1||bgcolor="yellow"|1||bgcolor="yellow"|1||0||0||0||0
|-align="center"
|Output||bgcolor="yellow"|1||bgcolor="yellow"|1||bgcolor="yellow"|1||bgcolor="yellow"|1||bgcolor="yellow"|10||bgcolor="yellow"|01||bgcolor="yellow"|10||bgcolor="yellow"|01
|}
Therefore the bitwise ''AND'' is used to '''clears bits''' to a value of 0.
For example, bits 0-:3 can be cleared to 0 while preserving the remaining bits in a byte by ANDing a mask of 11110000 (0xF0):
{|cellspacing="0" cellpadding="5" border="1"
|Output||bgcolor="yellow"|0||bgcolor="yellow"|1||bgcolor="yellow"|0||bgcolor="yellow"|1||bgcolor="yellow"|0||bgcolor="yellow"|0||bgcolor="yellow"|0||bgcolor="yellow"|0
|}
 
=== XOR ===
Therefore the bitwise ''XOR'' is used to '''flip (invert) bits''' from 0 to 1 and vice-versa.
For example, bits 0-3 4:7 can be flipped while preserving the remaining bits in a byte by XORing a mask of 11110000 (0xF0):
{|cellspacing="0" cellpadding="5" border="1"
|Output||bgcolor="yellow"|1||bgcolor="yellow"|0||bgcolor="yellow"|1||bgcolor="yellow"|0||bgcolor="yellow"|0||bgcolor="yellow"|1||bgcolor="yellow"|0||bgcolor="yellow"|1
|}
 
=== NOT ===
A rotate-left by 1 bit performed on a byte will move bit 0 to bit 1, bit 1 to bit 2, bit 2 to bit 3, and so forth. Bit 7 will move to bit 0.
 
Note that in some processors, a status register bit is included in the rotation, allowing rotates to be strung together in multi-byte sequences.
=== Shift ===