Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

~ in Perl 3

Status
Not open for further replies.

DrSeussFreak

Programmer
Feb 16, 2007
149
US
I am just starting Perl for the first time in my professional life. I am reading "Beginning Perl" published by WROX, and I hit page 50. The 'not' operator or ~ . What the heck is it used for?

~85 = 4294697210
~4294697210 = 85

000000000000000000000000010101010 = 85
111111111111111111111111101010101 = 4294697210

How is that really useful?
 
it exists though, as a basic math operator. Literally switches the bits. I represented the 32-bit version, 64-bit would be larger, 16-bit smaller and so on.
 
It's useful for turning off flag bits.

Code:
$flagsregister  = 0xFF; # 1111 1111
$flagstoturnoff = 0x05; # 0000 0101

$flagsregister &= ~$flagstoturnoff;

 
You can look up perl operators at
Unary "~" performs bitwise negation, i.e., 1's complement. For example, 0666 & ~027 is 0640. (See also "Integer Arithmetic" and "Bitwise String Operators".) Note that the width of the result is platform-dependent: ~0 is 32 bits wide on a 32-bit platform, but 64 bits wide on a 64-bit platform, so if you are expecting a certain bit width, remember to use the & operator to mask off the excess bits.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
perl has some bit oriented handling statements (e.g. [tt]vec[/tt] and [tt]pack[/tt]) and also bit oriented integer arithmetic operators (shift operators). The unary not operator finds its place in this context.

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Thank you for your responses, all I wanted was 1 reason for its use, but I got 3 :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top