Type conversion in JavaScript (binary conversion | bit operation)

Source: Internet
Author: User
Tags bitwise bitwise operators

1:parseint (String): The function is parsed from the beginning of the string, returning an integer

parseint ("123hua");  // output  123parseint ("123");     // output  123parseint ("12.3");    // output  of parseint ("Hua");     // Output  Nan

2: In addition the parseint () method has other modes, that is, binary, octal, hexadecimal, or any other binary string can be converted to an integer

parseint ("ten", +);   // output  of parseint ("Ten", 2);    // Output  2parseint ("10s", 2);    // Output  2parseint ("Ten", 8);    // output  8parseint ("ten");   // output  of parseint ("0x10");     // output  0x 16 representation of the binary

3.parseFloat (): This function is similar to the parseint () method.

Parsefloat ("123hua");  // output  123parsefloat ("123");     // output  123parsefloat ("12.3");    // output  12.3parsefloat ("Hua");     // Output  Nan

4.Number (): Coercion of type conversions, similar to the parseint () and parsefloat () methods, except that it transforms the entire value, not the partial value.

 Number (false ) //  output 0  number (true ) //  Output 1  number (undefined) //  output NaN  Number (null )  output 0  number ("1.2") //  output 1.2  Number ("n") //  output  number ("1.2.3") Span style= "color: #008000;" >//  output NaN  number (new  Object ()) Span style= "color: #008000;" >//  output NaN  

The ToString () method of the 5:number type is special, and it has two modes, the default mode and the base mode.
With the default mode, the ToString () method simply outputs a numeric value (whether integer, floating point, or scientific notation) with the corresponding string.

var iNum = ten; alert (inum.tostring (2));    // output "1010"Alert (inum.tostring (8));    // output "a" alert (inum.tostring ());    // output "A" (). toString ();            // Output "" (a). ToString (2);           // output "1100" "N". toString (2);           // Output ""

The ToString () method of the 6:boolean type simply outputs "true" or "false"

var false ; alert (bfound.tostring ());     // output "false"

7: Bitwise operators operate on the bottom of the number (that is, the 32 digits of the number).
Bitwise arithmetic not is represented by a negative number (~)

 //  var  iNum1 = 25; // 25 equals 00000000000000000000000000011001      var  iNum2 = ~inum1; //         convert to 11111111111111111111111111100110  alert (iNum2); //  output " -26"  //  var  iNum1 = 25;     var  iNum2 =-inum1-1;alert (INUM2);  //  output -26  

Bitwise operation and by the ampersand (&), directly to the binary form of the number of operations, are 1 1, otherwise 0

var iNum1 =;        // 25 equals 0000 0000 0000 0000 0000 0000 0001 1001 var iNum2 = 3;         // 3  equals 0000 0000 0000 0000 0000 0000 0000 0011Alert (iNum2); // output     //1 equals 0000 0000 0000 0000 0000 0000 0000 0001

The bitwise operation or is represented by the symbol (|), and is directly the binary form of the number, all 0 is 0, otherwise 1

var iNum1 =;        // 25 equals 0000 0000 0000 0000 0000 0000 0001 1001 var iNum2 = 3;         // 3  equals 0000 0000 0000 0000 0000 0000 0000 0011Alert (iNum2); // output     //27 equals 0000 0000 0000 0000 0000 0000 0001 1011

Bitwise operation XOR is represented by a symbol (^), and, of course, it is also performed directly on the binary form, with a difference of 1, the same as 0

var iNum1 =;        // 25 equals 0000 0000 0000 0000 0000 0000 0001 1001 var iNum2 = 3;         // 3  equals 0000 0000 0000 0000 0000 0000 0000 0011Alert (iNum2); // output     //26 equals 0000 0000 0000 0000 0000 0000 0001 1010

The left shift operation is represented by a two less than sign (<<). It moves all digits in the number to the left by the specified number.

var iNum1 = 2;            // equals binary ten var iNum2 = iNum1 << 5;    // equals binary 10 million binary

Type conversion in JavaScript (binary conversion | bit operation)

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.