| Index: > A B C D E F G H I J K L M N O P Q R S T U V W X Y Z |
|
|||||
| First Prev [ 1 2 3 ] Next Last |
A bitwise NOT or complement is a unary operation which performs logical negation on each bit. 0 digits become 1, and vice versa. For example:
NOT 0111 = 1000In the C and C++ programming languages, the NOT operator is "~" (tilde). For example:
assigns x the result of "NOT y". This is different from the C and C++ logical "not" operator, "!" (exclamation point), which treats the entire numeral as a single Boolean value. For example:
assigns x a Boolean value of "true" if y is "false", or "false" if y is "true". In C and C++, a numerical value is interpreted as "true" if it is non-zero. The logical "not" is not normally considered a bitwise operation, since it does not operate at the bit level.
Bitwise NOT is useful in finding the one's complement of a binary numeral, and may be an intermediate step in finding the two's complement of the same numeral.
A bitwise OR takes two bit patterns of equal length, and produces another one of the same length by matching up corresponding bits (the first of each; the second of each; and so on) and performing the logical OR operation on each pair of corresponding bits. The result in each position is 0 if both are 0, and 1 otherwise. For example:
0101 OR 0011 = 0111In C/C++, the bitwise OR operator is "|" (pipe). For example:
assigns x the result of "y OR z". This is different from the C and C++ logical "or" operator, "||" (two pipes), which treats its operands as Boolean values, and results in "true" or "false".
The bitwise OR may be used in situations where a set of bits are used as flags; the bits in a single binary numeral may each represent a distinct Boolean variable. Applying the bitwise OR operation to the numeral along with a bit pattern containing 1 in some positions will result in a new numeral with those bits set. For example:
0010can be considered as a set of four flags. The first, second, and fourth flags are not set (0); the third flag is set (1). The first flag may be set by applying the bitwise OR to this value, along with another value in which only the first flag is set:
0010 OR 1000 = 1010This technique may be employed by programmers who are working under restrictions of memory space; one bit pattern can represent the states of several independent variables at once.
A bitwise XOR takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same. For example:
0101 XOR 0111 = 0010In C/C++, the bitwise XOR operator is "^" (caret). For example:
assigns x the result of "y XOR z".
Some assembly language programmers have used the XOR operation as a short-cut to set the value of a registerIn computer architecture, a processor register is a small amount of very fast computer memory used to speed the execution of computer programs by providing quick access to commonly used values—typically, the values being in the midst of a calculation at a to zero. On many architectures, the XOR operation requires fewer CPUThe central processing unit (CPU is the part of a computer that interprets and carries out the instructions contained in the software. In most CPUs, this task is divided between a control unit that directs program flow and one or more execution units that clock cycles than the sequence of operations that may be required to load a zero value and save it to the register. Using a given value as input to both sides of the bitwise XOR operation always results in an output of zero; by XORing a register with itself, that register can be easily set to zero.
The bitwise XOR may also be used to toggle flags in a set of bits. Given a bit pattern:
0010The first and third bits may be toggled simultaneously by a bitwise XOR with another bit pattern containing 1 in the first and third positions:
0010 XOR 1010 = 1000This technique may be used to manipulate bit patterns representing sets of Boolean variables.