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 | [reply] |
|
|
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---
(the triplet paradiddle with high-hat)
| [reply] [d/l] [select] |
|
|
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
| [reply] |
|
|
Also be aware that, if you're using locales and such, uc() is aware how to transform accented characters and will DTRT.
| [reply] |
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 | [reply] |
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!
| [reply] [d/l] |
|
|
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.
| [reply] |
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
| [reply] |
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....
| [reply] |