-
Notifications
You must be signed in to change notification settings - Fork 4
Bitwise Operations
A bitwise operation is an operation to process 2 input values to produce one output value. However, some special cases that only take one input value exist too. Bitwise operations are the most fundamental kind of operations a computer has to process values. Different bitwise operations can be combined to do more complex operations like addition, subtraction, multiplication, division and many more. In game hacking these can be used to manipulate only certain bits of a value or use bit masks to extract certain bit states.
The NOT operator turns any true
input value and negative and vice versa. In higher programming languages this operator is represented as an exclamation mark (!). It can be used on boolean values and integers. NOTing false
becomes true
, NOTing true
becomes false
. NOTing any integer that is not 0 becomes 0, NOTing an integer of 0 becomes 1.
! | |
0 | 1 |
1 | 0 |
The AND operator processes two input values and produces one output value. The output value is only 1, if both input values are 1. Otherwise the output is always 0. In programming the AND operator is represented as an ampersand (&).
& | 0 | 1 |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
The OR operator processes two input values and produces one output value. The output value is only 1, if at least one input value is 1. Otherwise the output is always 0. In programming the OR-operator is represented as a pipe character, aka vertical line (|).
| | 0 | 1 |
---|---|---|
0 | 0 | 1 |
1 | 1 | 1 |
The NAND operator processes two input values and produces one output value. The output value is only 1, if not both input values are 1. Otherwise the output is always 0. In programming the NAND operator is a combination of NOT and AND.
!& | 0 | 1 |
---|---|---|
0 | 1 | 1 |
1 | 1 | 0 |
The NOR operator processes two input values and produces one output value. The output value is only 1, if both input values are 0. Otherwise the output is always 0. In programming the NOR operator is a combination of NOT and OR.
!| | 0 | 1 |
---|---|---|
0 | 1 | 0 |
1 | 0 | 0 |
The XOR operator processes two input values and produces one output value. The output value is only 1, if both input values are not equal. Otherwise the output is always 0. In programming the XOR operator is represented as a circumflex (^).
^ | 0 | 1 |
---|---|---|
0 | 0 | 1 |
1 | 1 | 0 |
The XNOR operator processes two input values and produces one output value. The output value is only 1, if both input values are equal. Otherwise the output is always 0. In programming the XNOR operator is a combination of NOT and XOR (!^).
!^ | 0 | 1 |
---|---|---|
0 | 1 | 0 |
1 | 0 | 1 |
A negation turns all bits of a value to their opposite. In programming the negation operator is represented by a tilde (~).
Example:
~ b00110101 = b11001010
Bit-Shift operators move all bits of a value to either left or right by the amount of places defined by the right-hand operant. Bits set to 1 that move out of the value will be lost while bits on the other side are set to 0. There exist 2 kinds of bit-shifts: Left bit-shift (<<) and right bit-shift (>>).
Examples:
b10011010 << 4 = b10100000
b10011010 >> 4 = b00001001
Bitwise Rotate Same as bit-shift, but the lost bits will be restored on the opposite side of the value. These don't have a binary operator but many programming languages offer functions to do so.
Examples:
b10011010 rot_l 4 = b10101001
b10011010 rot_r 4 = b10101001