tags:

views:

206

answers:

8

i came across this line of code in an app im revising:

substr($sometext1 ^ $sometext2, 0, 512);

thank you

+3  A: 

It's a bitwise operator.

Example:

"hallo" ^ "hello"

Outputs the ascii values #0 #4 #0 #0 #0 ('a' ^ 'e' = #4)

Darin Dimitrov
im having tough time understanding about the ascii values, if we take a and e as binary equivalents like so:a=01100101e=01100001---------xor=00000100is this right?
chicane
+4  A: 

XOR (Exclusive OR)

$a ^ $b means Bits that are set in $a or $b but not both are set.

http://php.net/manual/en/language.operators.bitwise.php

webdestroya
+2  A: 

It's the XOR (exclusive-or) operator. For strings it's used as simple encryption.

Daniel DiPaolo
+2  A: 

In PHP, ^ is mean 'bitwise XOR'. Your code there xor's together two strings, then returns at most the first 512 characters.

In other words it does this:

return (at most the first 512 characters of (someText1 XOR someText2))
Cam
Did you mean 512 or 12?
webdestroya
@webdestroya: 512, of course :) - it was a typo; thanks. +1 :)
Cam
@incrediman I figured, thought I'd point it out
webdestroya
+2  A: 

That's the bitwise OR operator - in PHP, it also applies to strings.

Michael Borgwardt
+4  A: 

^ is the bitwise exclusive OR operator. For each bit in a value, it looks to see if that bit is the same in the other value; if it is the same, a 0 is output in its place, otherwise a 1 is output. For example:

  00001111
^ 01010101
  --------
  01011010
mipadi
A: 

^ Matches the starting position within the string. In line-based tools, it matches the starting position of any line.

huh
that would be a regular expression
Nils
which it would do since the first character that does not match is now XOR'd and will show up as a 1. I guess it would depend on what the original code was trying to accomplish - based on this example of "what does this do" .. we would of course need to know more information - as opposed to "what does this "^" character do.
huh
A: 

Its the Exclusive OR Operator, used when you need to know the difference between two characters. Like "bat" ^ "bed" will return "0", "4", "16"

Starx