In JavaScript, when a string is used with a binary operator, it is first converted to a number. The following are relevant parts of the ECMAScript specification to explain how this works.
Bitwise Operators :
The product A: A @B, where @ is one of the bitwise operators in the above products, is estimated as follows:
- Let lref be the result of A.
- Let lval be GetValue (lref).
- Let rref be the result of B.
- Let rval be GetValue (rref).
- Let lnum be ToInt32 (lval).
- Let rnum be ToInt32 (rval).
- Return the result of applying the bitwise operator @ to lnum and rnum. The result is a signed 32-bit integer.
ToInt32 :
The abstract operation ToInt32 converts its argument to one of two values 32 in the range from -231 to 231-1 inclusive. This abstract operation works as follows:
- Let the number be the result of calling ToNumber on the input argument.
- If the number is NaN, +0, -0, + β or -β, return +0.
- Let posInt be the sign (number) * gender (abs (number)).
- Let int32bit be posInt modulo 2 32 ; those. a finite integer k of type Number with a positive sign and less than 2 32 in magnitude, so that the mathematical difference of posInt and k is mathematically an integer multiple of 2 32 .
- If int32bit is greater than or equal to 2 31, return int32bit - 2 32 otherwise return int32bit.
The internal ToNumber function returns NaN for any string that cannot be analyzed as a number, and ToInt32 (NaN) will give 0. Thus, in the code example, all bitwise operators with letters as operands will be evaluated as 0 | 0 0 | 0 , which explains why only 0 is printed.
Note that something like '7' | '8' '7' | '8' will be rated as 7 | 8 7 | 8 , because in this case the strings used as operands can be successfully converted to numbers.
Regarding why the behavior in Python is different, there is no implicit type conversion in Python, so an error is expected for any type that does not implement binary operators (using __or__ , __and__ , etc.), and strings do not implement these binary operators.
Perl does something completely different, bitwise operators are implemented for strings , and it will essentially execute a bitwise operator for the corresponding bytes from each string.
If you want to use JavaScript and get the same result as Perl, you first need to convert the characters to their code points using str.charCodeAt , execute the bitwise operator in the resulting integers, and then use String.fromCodePoint to convert the resulting numeric values into characters.
Andrew Clark
source share