The perverted bit arithmetic of javascript

Source: Internet
Author: User
Tags bitwise bitwise operators string to number

The perverted bit arithmetic of javascript

var a = "10" | 0;
alert (a);
Alert (typeof a);
The result is 10,number.

This means that the statement can convert the string to number.

If:
var a = "SSS" | 0;
alert (a);
The result is 0.
parseint ("SSS"), it returns Nan.
This is too powerful, so that whatever it is can be converted to number ...

Like what:
(function () {}) | 0;
({})| 0;
([])| 0;

I got a go ... What a pervert.

This thing is absolutely not unreasonable.

Bitwise operations are computed by binary values of the contents of the operator.
As for string, is the binary of the string "1" and the value 1 the same? No chance!!
The function and the object are even more impossible to explain.

I got a go ... Let's get out of Java and look at it.


public Class AAA {

public string tobinary (String str) {
char[] Strchar = Str.tochararray ();
String result = "";
for (int i = 0; i < strchar.length; i++) {
Result + = Integer.tobinarystring (Strchar[i]);
}
return result;
}

public static void Main (string[] args) {
String s = "1";
int n = 1;
System.out.println (integer.tobinarystring (n));
SYSTEM.OUT.PRINTLN (New AAA (). Tobinary (s));
}
Output Result:
1010
110001 110000

The binary value of note 1 is 1010, and "1" is 110001110000.

It is clear that they are converted to binary values in a bitwise OR operation, completely different.
The result will inevitably be different after the run is restored back to number again.
How is the JS kernel implemented?
This is only a guess, possibly for the following ways:

public Class AAA {
private int _or_ (string s1, string s2) {
int result1,result2;
try {
RESULT1 = Integer.parseint (S1);
}catch (Exception e) {
RESULT1 = 0;
}
try {
RESULT2 = integer.parseint (s2);
}catch (Exception e) {
RESULT2 = 0;
}
return RESULT1 | RESULT2;
}
private int _or_ (String s1, int s2) {
int result1,result2;
try {
RESULT1 = Integer.parseint (S1);
}catch (Exception e) {
RESULT1 = 0;
}
return RESULT1 | S2;
}
private int _or_ (int s1, String s2) {
int result2;
try {
RESULT2 = integer.parseint (s2);
}catch (Exception e) {
RESULT2 = 0;
}
return S1 | RESULT2;
}
private int _or_ (int n1, int n2) {
return N1 | N2;
}
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN (New AAA () _or_ ("10", "1"));
}
}

Because in strongly typed languages, bitwise operators can only be used in int and long types.
If the guess is correct, JS is a weak type, all content in-place operation before the JS engine needs to be unified into an int or long.
The default value is 0 if the conversion is not possible.
Finally, the operation is done by the bitwise operation.

This "abnormal" phenomenon has also been produced. I have a go to ...

The perverted bit arithmetic of javascript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.