Since each bit of a number (see bits and bytes) can hold a truth value (1 = true, 0 = false), it is possible to use indi-vidual bits to specify particular conditions in a system, and to compare individual pairs of bits using special operators that are available in many programming languages.
Bitwise operators consist of logical operators and shift operators. The logical operators, like Boolean operators in general (see Boolean operators), perform logical compar-isons. However, as the name suggests, bitwise logical opera-tors do a bit-for-bit comparison rather than comparing the overall value of the bytes. They compare the corresponding bits in two bytes (called source bits) and write result bits based on the type of comparison.
The AND operator compares corresponding bits and sets the bit in the result to one if both are one. Otherwise, it sets it to zero.
Example: 10110010 AND 10101011 = 10100010
The OR operator compares corresponding bits and sets the bit in the result to one if either or both of the bits are ones.
Example: 10110110 OR 10010011 = 10110111
The XOR (“exclusive OR”) operator works like OR except that it sets the result bit to one only if either (not both) of the source bits are ones.
Example: 10110110 XOR 10010011 = 00100101
The COMPLEMENT operator switches all the bits to their opposites (ones for zeroes and zeroes for ones).
Example: COMPLEMENT 11100101 = 00011010
Measurement Number of Bytes examples of Use
byte 1 small integer, character
kilobyte 210 1,024 RAM (PCs in the 1980s)
megabyte 220 1,048,576 hard drive (PCs to mid-1990s)
RAM (modern PCs)
gigabyte 230 1,073,741,824 hard drive (modern PCs)
RAM (latest PCs)
terabyte 240 1,099,511,627,776 large drive arrays
The shift operators simply shift all the bits left (LEFT SHIFT) or right (RIGHT SHIFT) by the number of places specified after the operator. Thus
00001011 LEFT SHIFT 2 = 00101100
and
00001011 RIGHT SHIFT 2 = 00000010 (bits that shift off the end of the byte simply “drop off” and are replaced with zeroes).
While we have used words in our general description of these operators, actual programming languages often use special symbols that vary somewhat with the language. The operators used in the C language are typical:
& AND
| OR
^ Exclusive OR
~ Complement
>> Right Shift
<< Left Shift
Masking
There are a number of programming tasks where the contents of individual bits must be read or manipulated. Operating systems and network protocols often have data structures where several separate pieces of information are stored in a single byte in order to save space. (For exam-ple, in IBM architecture PC’s interrupts are often enabled or disabled by setting particular bits in a mask register.) Operations using bitmapped images can also involve bit manipulation.
Suppose the right three bits of a byte contain a desired piece of information. The byte is ANDed with a prepared byte called a mask in which the desired bits are set to one and the rest of the bits are zero: in this case it would be 00000111. Thus if the byte contains 11010110:
11010110 AND 00000111 = 000000110
The result contains only the value of the right three bits. Similarly, if one wants to set a particular bit to zero, one simply ANDs the byte with a byte that has a zero in that position and ones in the rest of the byte. Thus to “zero out” the second bit from the left in 11010110:
11010110 AND 10111111 = 10010110
No comments:
Post a Comment