Home arrow Perl Programming arrow More Perl Bits

More Perl Bits

In the last article, I talked about the need to work at the bit level in Perl and in other languages. In order to understand bits, we first took a look at how binary numbers are represented in Perl, and then we took a look at two bitwise operators, AND and OR. In looking at the operators, we looked at their most common uses in programming. We also began to look at an algorithm for finding prime numbers, but we stopped upon seeing that there is no easy implementation, at least not without a knowledge of bits.

TABLE OF CONTENTS:
  1. More Perl Bits
  2. Bit shift operators
  3. Bit vectors
  4. The vec function
  5. Finding prime numbers
By: Peyton McCullough
Rating: starstarstarstarstar / 1
September 14, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In this article, we'll finish learning about bits. We'll take a look at a few more operators and an important Perl function for working with bits. Finally, we'll implement the algorithm we looked at. 

You may want to read the previous article if you haven't (or review it if you have) before you dive into this one.

The bitwise XOR and NOT operators 

Maybe you recall that, from the last article, I said that the bitwise OR operator would set a resulting bit to 1 if one or both of the corresponding bits in the operands were 1. The key thing to notice here are that if both of the bits are 1, then the result is 1. The OR operator may be more appropriately termed an inclusive OR.

However, there is another operator worth mentioning, the exclusive OR, more commonly known as XOR. The bitwise XOR operator is represented by a carat symbol (^). The bitwise XOR operator will set a resulting bit to 1 if and only if one of the operands features a 1 as the corresponding bit. 

For example, say that we have these two operands:

 

1010

0000

 

If we apply the bitwise XOR, then we get this result:

 

1010

 

However, say we have these two operands:

 

1010

1111

 

If we apply the bitwise XOR to these operands, we get this result:

 

0000

 

That's because, for the first and third bits, both operands feature a 1. The bitwise XOR operator requires that only one of the operands have a 1. 

Remember how the bitwise AND operator can be used with a bit mask to check the value of a particular bit or set of bits? The bitwise XOR operator can also be used with a bit mask, but for a different purpose. Using the bitwise XOR operator and the appropriate bit mask, it is possible to toggle a specific bit or set of bits. For example, consider this number:

 

1100

 

If we want to toggle the second bit, we could use the XOR operator with the appropriate bit mask (0100) to get this result:

 

1000

 

Or, if we wanted to toggle the third bit, we could use the XOR operator with a different bit mask (0010):

 

1110

 

Again, this is interesting and very useful behavior. 

In order to see how this looks in Perl, let's recreate what we just did, but in Perl this time. These Perl lines apply the operations we just looked at, and then print the results:

 

my $bits = 0b1100;

 

printf "%bn", $bits ^ 0b0100;

printf "%bn", $bits ^ 0b0010;

 

Finally, there is the bitwise NOT operator (also called the bitwise negation operator, among other names), represented in Perl by a tilde symbol (~). If we oversimplify it, it's very easy to understand. The bitwise NOT operator, at an oversimplified level, “flips” each bit in an operand (it only takes one operand), turning each 1 into a 0 and each 0 into a 1. 

In reality, however, the NOT operator is a bit more complex. The NOT operator returns a result based on your machine's integer size. So, on my machine, if I apply the operator on this operand:

 

0111

 

Then I get this result:

 

1111111111111111111111111111111111111111111111111111111111111000

 

Looking at the operand, you would expect the bitwise NOT operator to return 1000, and it does—but the result comes after a string of 1s. On my 64-bit machine, integers are 64 bits, so the four bits we want come after a string of sixty 1s. 

Thankfully, it's easy to fix this using something you already know. In the above case, we want to extract the last four bits. Does this sort of thing sound familiar? We can use the bitwise AND operator and a bit mask to trim off all of the extra bits:

 

my $bits = ~0b1111;

$bits = $bits & 0b1111;

 

printf "%bn", $bits;

 

As you can see, it's not very difficult to get the behavior you want. 



 
 
>>> More Perl Programming Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: