Perl Programming Page 4 - Perl, Bit by Bit |
Let's start by looking at the bitwise AND operator, represented by the ampersand symbol (&). This operator takes two operands and compares corresponding bits. If both of the bits are 1, then the result is 1. If not, then the result is 0. For example, if we used these two numbers as operands: 1000 1001 The result would be this: 1000 Only the first bit in each number is 1, so in the result, only the first bit will be 1. This is how the operation would look in Perl: printf "%bn", 0b1000 & 0b1001; The above line simply prints out the result. The bitwise AND operator is very useful for picking out the values of certain bits. You can apply a bit mask to a number to determine the value of a particular bit. For example, consider this binary number: 1011 Say that we need to determine whether the first bit is 1 or 0. We can do this by using the bitwise AND operator and another binary number, in which only the first bit (the bit we're looking at) is set to 1: 1000 The operation yields the following result: 1000 If the first bit had not been 1, then the result would have been zero. So, in order to check if the first bit is 1, we simply need to check if the result is nonzero. The following code snippet demonstrates this: # A nybble (or nibble) is four bits # (Half of a byte) my $nybble = 0b1011;
if ($nybble & 0b1000) { print "The first bit is 1.n"; } else { print "The first bit is 0.n"; } Next is the bitwise OR operator, represented by the pipe symbol (|). You can probably guess the functionality of this operator. It takes two operands and examines corresponding bits, just as with the bitwise AND operator. If one or both of the bits is 1, then the corresponding bit in the result will also be 1. So, if we took these two operands: 1010 0110 And applied the bitwise OR operator, then we would get this result: 1110 This is confirmed by the following line of Perl code, which performs the above operation and then prints out the result: printf "%bn", 0b1010 | 0b0110; The bitwise OR operator is useful when working with flags. A flag is simply a bit or a set of multiple bits that indicate some sort of behavior. For example, say you're developing a library that manipulates text, and you need to give developers the option to specify whether text will be italicized, bold, or both. You could create a function that takes an argument for each of these properties. Or, you could use bit flags to condense it into a single argument. This would involve using one bit to determine whether text should be italicized, and another bit to determine whether text should be bold. This only requires two bits. We'll assume that the first bit is for italics, and the second is for bold text. So, to italicize text, this binary number would be passed into the function: 10 To make text bold, this binary number would be passed into the function: 01 Finally, to make text bold and italicized, this binary number would be passed in: 11 In order to make this work, you'd want to create two constants, one with the bit set for italics, and one with the bit set for bold text: use constant { ITALICS => 0b10, BOLD => 0b01, }; In order to call a formatting function, specifying that we want italicized text, we'd do this: doSomething(ITALICS); In order to specify bold text, we'd call the function like this: doSomething(BOLD); Finally, in order to specify that we want both formatting options, we'd use the bitwise OR operator like this: doSomething(ITALICS | BOLD); We can check the values using the bitwise AND operator. Let's make the doSomething subroutine print out messages indicating what formatting options have been selected: sub doSomething { my $formatting = shift;
print "Italicized.n" if ($formatting & ITALICS); print "Bold.n" if ($formatting & BOLD); } Neat, huh? You'll see this done often in the programming world. The bitwise AND and OR operators are very important in working with bits in any language, but there a few more very important operators available. In the next article, we'll take a look at them.
blog comments powered by Disqus |
|
|
|
|
|
|
|