|
|||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
In computer programming, a bitwise operation operates on one or two bit patterns or binary numerals at the level of their individual bits. On most older microprocessors, bitwise operations are slightly faster than addition and subtraction operations and usually significantly faster than multiplication and division operations. On modern architectures, this is not the case 1: binary operations are generally the same speed as addition (though still faster than multiplication).
Bitwise operatorsNOTThe bitwise NOT, or complement, is a unary operation that performs logical negation on each bit, forming the ones' complement of the given binary value. Digits which were 0 become 1, and vice versa. For example: NOT 0111 (decimal 7) = 1000 (decimal 8) In many programming languages (including those in the C family), the bitwise NOT operator is " ORA 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 inclusive OR operation on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 OR the second bit is 1 (or both), and otherwise the result is 0. For example: 0101 (decimal 5) OR 0011 (decimal 3) = 0111 (decimal 7) In the C programming language family, the bitwise OR operator is " 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: 0010 (decimal 2) can 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 (decimal 2) OR 1000 (decimal 8) = 1010 (decimal 10) This technique is often used to conserve memory in programs dealing with large numbers of Boolean values. XORA bitwise exclusive or 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 0011
= 0110
In the C programming language family, the bitwise XOR operator is " Assembly language programmers sometimes use the XOR operation as a short-cut to set the value of a register to zero. Performing XOR on a value against itself always yields zero, and on many architectures, this operation requires fewer CPU clock cycles than the sequence of operations that may be required to load a zero value and save it to the register. The bitwise XOR may also be used to toggle flags in a set of bits. Given the bit pattern, 0010 the 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
= 1000
This technique may be used to manipulate bit patterns representing sets of Boolean variables. See alsoANDA bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is 0. For example:
0101
AND 0011
= 0001
In the C programming language family, the bitwise AND operator is " The bitwise AND may be used to perform a bit mask operation. This operation may be used to isolate part of a string of bits, or to determine whether a particular bit is 1 or 0. For example, given a bit pattern: 0011 To determine whether the third bit is 1, a bitwise AND is applied to it and another bit pattern containing 1 in the third bit:
0011
AND 0010
= 0010
Since the result is 0010 (non-zero), the third bit in the original pattern was 1. Using bitwise AND in this manner is called bit masking, by analogy to the use of masking tape to cover, or mask, portions that should not be altered, or are not of interest. In this case, the 0 values mask the bits that are not of interest. The bitwise AND can also be combined with the bitwise NOT to clear bits. For example: 0110 The second bit may be cleared (i.e. set to 0) by applying the bitwise AND to this value, along with the complement (i.e. NOT) of another value in which only the second bit is set:
NOT 0100
= 1011
0110
AND 1011
= 0010
Bit shiftsThe bit shifts are sometimes considered bitwise operations, since they operate on the binary representation of an integer instead of its numerical value; however, the bit shifts do not operate on pairs of corresponding bits, and therefore cannot properly be called bit-wise operations. In this operation, the digits are moved, or shifted, to the left or right. Registers in a computer processor have a fixed number of available bits for storing numerals, so some bits will be "shifted out" of the register at one end, while the same number of bits are "shifted in" from the other end; the differences between bit shift operators lie in how they compute the values of those shifted-in bits. Arithmetic shiftIn an arithmetic shift, the bits that are shifted out of either end are discarded. In a left arithmetic shift, zeros are shifted in on the right; in a right arithmetic shift, the sign bit is shifted in on the left, thus preserving the sign of the operand. This example uses an 8-bit register: 00010111 LEFT-SHIFT = 00101110 00010111 RIGHT-SHIFT = 00001011 In the first case, the leftmost digit was shifted past the end of the register, and a new 0 was shifted into the rightmost position. In the second case, the rightmost 1 was shifted out (perhaps into the carry flag), and a new 0 was copied into the leftmost position, preserving the sign of the number. Multiple shifts are sometimes shortened to a single shift by some number of digits. For example: 00010111 LEFT-SHIFT-BY-TWO = 01011100 A left arithmetic shift by n is equivalent to multiplying by 2n (provided the value does not overflow), while a right arithmetic shift by n of a two's complement value is equivalent to dividing by 2n and rounding toward negative infinity. If the binary number is treated as ones' complement, then the same right-shift operation results in division by 2n and rounding toward zero. Logical shiftIn a logical shift, the bits that are shifted out are discarded, and zeros are shifted in (on either end). Therefore, the logical and arithmetic left-shifts are exactly the same operation. However, the logical right-shift inserts bits with value 0 instead of copies of the sign bit. Hence the logical shift is suitable for unsigned binary numbers, while the arithmetic shift is suitable for signed two's complement binary numbers. Rotate no carryAnother form of shift is the circular shift or bit rotation. In this operation, the bits are "rotated" as if the left and right ends of the register were joined. The value that is shifted in on the right during a left-shift is whatever value was shifted out on the left, and vice versa. This operation is useful if it is necessary to retain all the existing bits, and is frequently used in digital cryptography. Rotate through carryRotate through carry is similar to the rotate no carry operation, but the two ends of the register are considered to be separated by the carry flag. The bit that is shifted in (on either end) is the old value of the carry flag, and the bit that is shifted out (on the other end) becomes the new value of the carry flag. A single rotate through carry can simulate a logical or arithmetic shift of one position by setting up the carry flag beforehand. For example, if the carry flag contains 0, then Rotate through carry is especially useful when performing shifts on numbers larger than the processor's native word size, because if a large number is stored in two registers, the bit that is shifted off the end of the first register must come in at the other end of the second. With rotate-through-carry, that bit is "saved" in the carry flag during the first shift, ready to shift in during the second shift without any extra preparation. Shifts in C, C++ and JavaIn C-inspired languages, the left and right shift operators are "
x = y << 2;
assigns x the result of shifting y to the left by two digits. In C and C++, computations with the left operand as an unsigned integer use logical shifts. In C, the results with the left operand as a signed integer are2:
In Java, all integer types are signed, and the " ApplicationsBitwise operations are necessary for much low-level programming, such as writing device drivers, low-level graphics, communications protocol packet assembly and decoding. Although machines often have efficient built-in instructions for performing arithmetic and logical operations, in fact all these operations can be performed just by combining the bitwise operators and zero-testing in various ways. For example, here is a pseudocode example showing how to multiply two arbitrary integers
This implementation of ancient Egyptian multiplication, like most multiplication algorithms, involves bitshifts. See also
References
External links
|
| All Right Reserved © 2007, Designed by Stylish Blog. |