"be consistent" PerlMonks

### bitwise string operator question

 on Aug 07, 2003 at 13:48 UTC Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I was reading the perlop man page when I saw this.

# ASCII-based examples
print "j p \n" ^ " a h"; # prints "JAPH\n"
print "JA" | " ph\n"; # prints "japh\n"
print "japh\nJunk" & '_____'; # prints "JAPH\n";
print 'p N\$' ^ " E<H\n"; # prints "Perl\n";

Can someone please tell me how this works? The man page reports it without much comment. In the first example why is the result all in uppercase etc?

Replies are listed 'Best First'.
Re: bitwise string operator question
by Abigail-II (Bishop) on Aug 07, 2003 at 13:58 UTC
String XOR does a bitwise XOR on a character by character bases. The bitwise representation of j is 01101010. The bitwise representation of a space is 00100000. XORing those gives you 01001010, the bitwise representation of J. And no, this is not a coincidence.

Abigail

Thanks Abigail-II, i just learned something cool for the day:
```perl -le'sub uc{@_[0]^" "};print uc(\$_)for(a..z)'
Can't believe i hadn't picked that up before now ...

DOH! Heh, my original one-liner did the bitwise XOR in a map ... i should know better than that. :P Let's try that again:

```perl -le'sub flip{@_[0]^" "};print flip(\$_)for(a..z)'
There, no ambiguity now. But ... the saddest part is that i learned this trick too late. My C++ Lab students last semester had to write a function that would flip case ... i really wish i could have blown their minds with this one. ;)

jeffa

```L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
```
That calls the buildin uc. You might want to call ::uc instead. But beaware, it doesn't uppercase! It flips the case, uppercase to lowercase, and lowercase to uppercase.

Abigail

Also be aware that, if you're using locales and such, uc() is aware how to transform accented characters and will DTRT.
Re: bitwise string operator question
by DigitalKitty (Parson) on Aug 07, 2003 at 14:33 UTC
Hi all.

Abigail++ (very cool).

Background:

& = bitwise 'and'
| = bitwise 'or'
^ = bitwise 'xor'

The bitwise xor operator only returns '1' when *one* of the operands is different (e.g. 0 ^ 1, 1 ^ 0). If both operands are the same, the result is '0'.

In ACSII, in order to quickly move from uppercase to lowercase (or vice versa), simply add / subtract 32 from the number respectively. The 'space' character, incidentally, has an ASCII value of 32.

Example:
A = 65
a = 97 (65+32 = 97) and (97-32=65).

Hope this helps, Anonymous Monk.
-Katie
Re: bitwise string operator question
by wirrwarr (Monk) on Aug 08, 2003 at 08:44 UTC
```"a" ^  " " flips case.
"a" |  " " sets lower case.
"a" & ~" " sets upper case.
"a" &  " " sets to space. urgs!
Where "a" is used, it means any letter, upper or lower case (like you usually use x in algebra). The first three are correct, but the fourth is only true for lowercase letters - for upper case letters the result of anding with space is null rather than space.
Re: bitwise string operator question
by krisahoch (Deacon) on Aug 07, 2003 at 17:02 UTC

Thanks to DK and Abigail-II for the lowlevel programming operations knowledge. It really helps those of us who have no formal training in programming.

Kristofer Hoch

Re: bitwise string operator question
by beppu (Hermit) on Aug 07, 2003 at 18:33 UTC
I've been coding in perl for 5 years, and I had no idea Perl had bitwise string operators until now. Damn....

Create A New User
Node Status?
node history
Node Type: perlquestion [id://281889]
Approved by valdez
Front-paged by derby
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2017-08-22 15:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (337 votes). Check out past polls.

Notices?