CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Rarely Used Operators:

 



Coderifous
journeyman

Mar 20, 2002, 5:58 AM

Post #1 of 9 (2449 views)
Rarely Used Operators: Can't Post

Not sure what the coffee cup is for, but it sounds good right now. Anyways:

These three operators (two related, one not):

>> << ~

I sort of understand the bit shift operators (>> and <<), in that they shift binary values like: 2 << 3 makes "000010" become "010000" or 2 becomes 16. I get that. But can anyone give me some practical uses of it in code examples? And how does "print <<EOF;" or "$this = <<END;" make use of this operator? I understand how they work in this context, but I don't understand why... is it basically a totally unrelated operator in this context?

And the ~ operator is a logical 'or-ing' operator? so on a 32 bit machine:

print ~1;

will output: 4294967294

which is the basically taking: 00000000000000000000000000000001

and turning it into: 11111111111111111111111111111110

But what would you use it for? What practical applications could it serve?

I've seen Marcus use it as a cool way to test against the length of a hash:

print "it's 7" if ~~%hash == 7;

but aside from forcing things into the context of numbers by double-or-ing them, what would you use that operator for?

Seeking enlightenment,

Jim


yapp
User

Mar 20, 2002, 6:42 AM

Post #2 of 9 (2444 views)
Re: [Coderifous] Rarely Used Operators: [In reply to] Can't Post

All those operators can be very use full, but they are not so used in higher programming languages. However, sometimes bit swapping/shifting/rotating can be usefull... However that's more used in assembler. But there are some side effects. Did you mention that shifting bits can perform the same as using a square function? What do you think about multiplying? in assembler, there is no * operator!! In Perl, I don't have a nice example for you.

When you're working with flags:
[perl]
use Fcntl;

my $flags = O_CREAT|O_RDONLY;

my $has_O_CREAT = ($flags & O_CREAT) = O_CREAT;


# to remove O_CREAT:
$flags = ($flags & ~O_CREAT);

# to toggle O_CREAT:
for(0..20)
{
$flags = ($flags ~ O_CREAT);
}


sysopen(FH, $file, $flags) or die ....;
[/perl]

Put some print statements into the code somewhere (that print a binary 0101010 string)


btw, the << operator has got nothing to do with the <<HERE_BLOCK; operator. Perl always looks at the context wherein an operator is used.

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


mhx
Enthusiast / Moderator

Mar 20, 2002, 7:05 AM

Post #3 of 9 (2441 views)
Re: [Coderifous] Rarely Used Operators: [In reply to] Can't Post


In Reply To
I sort of understand the bit shift operators (>> and <<), in that they shift binary values like: 2 << 3 makes "000010" become "010000" or 2 becomes 16. I get that. But can anyone give me some practical uses of it in code examples? And how does "print <<EOF;" or "$this = <<END;" make use of this operator? I understand how they work in this context, but I don't understand why... is it basically a totally unrelated operator in this context?


The heredoc operator is totally unrelated to the bitshift operators, although it looks exactly like the shift-left-operator.


In Reply To
And the ~ operator is a logical 'or-ing' operator? so on a 32 bit machine:

print ~1;

will output: 4294967294

which is the basically taking: 00000000000000000000000000000001

and turning it into: 11111111111111111111111111111110


Nope, it's not a logical OR (this would be '||' or 'or'), it's a binary NOT (negation) operator. It will just complement each bit, as your example above shows.


In Reply To
But what would you use it for? What practical applications could it serve?


You use it, well, basically when you need the complement of an integer...
I usually use it when I need to clear bits:


Code
$var = 0xdead; 
$bit = 3; # counting starts at zero
$var |= 1<<$bit; # set a bit
$var &= ~(1<<$bit); # clear a bit


(Don't blame me for the above, I would rarely use such code in Perl, and in C I would hide it inside some macros...)


Code
I've seen Marcus use it as a cool way to test against the length of a hash:  

print "it's 7" if ~~%hash == 7;

but aside from forcing things into the context of numbers by double-or-ing them, what would you use that operator for?


I didn't write the above piece of code, or did I? If so, please point me to the post, I must have been really sick then...
I have, however, used ~~ as a short way of writing int, although you could also use it in some cases as a short form of scalar. In the above code line, the ~~ doesn't make any sense, because %hash is already forced into scalar context by the == operator.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Coderifous
journeyman

Mar 20, 2002, 7:24 AM

Post #4 of 9 (2438 views)
Re: [mhx] Rarely Used Operators: [In reply to] Can't Post

PirateNo, that's definitely not the exact code you used, but in a perl-golfing session (which is gone now --???) you used that operator to force a hash into the context you wanted. Don't worry about your health, it was truly a clever manifestation of something you said you picked up while (?:talking to|reading from) Damian Conway. I seem to remember that you went to an expo or something where he was a speaker --- it was a while ago that the post was created, my memory may be imaginationCrazy.

So I didn't mean to insult you if that is what I have done. You have helped me and taught me much for quite a while now - since a while before the site went T.U. I bow to your perl divinity. And am humbled by the fact that I make no sense.Blush


mhx
Enthusiast / Moderator

Mar 20, 2002, 8:57 AM

Post #5 of 9 (2435 views)
Re: [Coderifous] Rarely Used Operators: [In reply to] Can't Post


In Reply To
So I didn't mean to insult you if that is what I have done.


No, you didn't. Wink
I hope I didn't sound upset. If I did, please excuse me.
I just was a little bit surprised to see the ~~ used in a place where it doesn't make much sense (except perhaps to obfuscate the code).


In Reply To
you picked up while (?:talking to|reading from) Damian Conway


(?:talking to|reading from) doesn't match unfortunately, I was listening to him. Tongue And it was extreme fun!!!

Cheers,
-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



mhx
Enthusiast / Moderator

Mar 20, 2002, 9:04 AM

Post #6 of 9 (2433 views)
Re: [Coderifous] Rarely Used Operators: [In reply to] Can't Post


In Reply To
but in a perl-golfing session (which is gone now --???)


[url=http://perlguru.com/gforum.cgi?post=14275#14275]Got it!

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Coderifous
journeyman

Mar 20, 2002, 10:07 AM

Post #7 of 9 (2431 views)
Re: [mhx] Rarely Used Operators: [In reply to] Can't Post

Ahhhh...[relief] Although my memory and imagination have proven to be indistinguishable, atleast there was some sort of foggy base to what I was saying.

Thankyou, you are voonderbah !

(I don't know how it's spelled, but I think I know how it sounds...)


mhx
Enthusiast / Moderator

Mar 20, 2002, 10:20 AM

Post #8 of 9 (2429 views)
Re: [Coderifous] Rarely Used Operators: [In reply to] Can't Post


In Reply To
Thankyou, you are voonderbah !


CoolCoolCoolCool
Hey, you know german? It's spelled 'wunderbar', but it's pronounced exactly as you wrote it down!

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Coderifous
journeyman

Mar 20, 2002, 10:38 AM

Post #9 of 9 (2427 views)
Re: [mhx] Rarely Used Operators: [In reply to] Can't Post

Shy-zah, Ish no speck-in-zee doy-ch.

LOL - I grew up in a little village called schrollbach and then on Ramstein AFB. Grew up is a little exaggeration. I lived there for several years, leaving for the US at after the 2cnd grade.

I attended kindergarten where we were to pick up some German. I have forgotten virtually all of it. The most useable one was "Ish no speck-in-zee doy-ch" which made the local baker laugh. I don't know why. I recall the people there being very friendly. I would like to take my family there someday and visit the house where I lived. I have very good memories of that place.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives