Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

bitwise string operator

by Anonymous Monk
on Jun 13, 2013 at 16:42 UTC ( #1038797=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I found this post on-line and I am not clear exactly what is going on.

“…you will get a string of bytes with values zero and one, rather then the characters '0' and '1'. To make this printable just 'or' in a string of zeroes of the right length.”

$dd |= '0' x length $dd

I know what the result of this statement is but I am not sure exactly what (how) it is doing (it). I am comparing 2 strings (^) and when I do

$cmp |= '0' x length($cmp); # See rest of code below.

numbers magically appear and I can then see (print) what is in $cmp. So, what exactly does ‘or’ in a string of zeroes mean? Zeroes appear but so do other digits (“A” ^ “C” → 2). What does |= do? (or |= ‘0’) Is there some substitution occurring?

My code:
$Str1 = "AAAACCCCGGGGTTTT"; $Str2 = "ACGTACGTACGTACGT"; $cmp = $Str1^$Str2; $cmp |= '0' x length($cmp); print "$Str1\n"; print "$Str2\n"; print "$cmp\n";







Replies are listed 'Best First'.
Re: bitwise string operator
by kennethk (Abbot) on Jun 13, 2013 at 17:23 UTC
    You are performing bitwise operations, and using the character 0, not the number zero, to clean things up. If you type ord '0', you will see the zero character is 48, or 30 in hex (or see ASCII). The |= operator is a compound operator, which performs a bitwise-or between the left- and right-hand sides, and then assigns the result to the left-hand argument.

    Your bitwise-xor (^) necessarily sets bits 7 and 8 to false since you only have alphabetics. The bitwise-or (|) sets the 5th and 6th bits to true. 0011xxxx (or xxx1100 if you are a little endian) corresponds to the ASCII column that contains all the numbers.

    So your output is a series of numeric characters, not a number.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thanks kenneethk

      It took me a while to figure it out. I am putting your response in more explicit terms for neophytes like myself.

      $Str1 = "AAAACCCCGGGGTTTT"; $Str2 = "ACGTACGTACGTACGT"; $cmp = $Str1^$Str2; $cmp |= '0' x length($cmp); print "$Str1\n"; print "$Str2\n"; print "$cmp\n";
      # What is ‘|=’ doing? This is bitwise ‘or’. Does ‘or’ on left side and right side (right side value is ‘0’ or 00110000). Puts result of ‘or’ in left side. Originally, the value in $cmp for “A” ^ “G” was 00000110 (= 6) – this is ‘Acknowledgement’ – this is not a printable character.

      By doing the ‘or’ with “0” (00110000 = 48) you convert ‘6’ to ‘6’ + ‘48’ = ‘54’ – this is the value for the character 6. So, $cmp looked empty because all of the values are for non-printable characters.

      A = 01000001
      G = 01000111
      ^ 00000110 = 6 ‘Acknowledgement’

      left 00000110
      ‘0’ 00110000
      | (or) 00110110 = 54 ‘character 6’


      A = 01000001
      T = 01010100
      ^ 00010101 = 21 ‘Negative Acknowledgement’

      left 00010101
      ‘0’ 00110000
      |(or) 00110101 = 53 ‘character 5’

      Thanks again for your help,

      romano (aka KennB)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1038797]
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2018-07-20 01:37 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (422 votes). Check out past polls.