Convert JavaScript decimal to binary, convert binary to decimal

Source: Internet
Author: User
Tags binary to decimal decimal to binary

<Br/> alert (parseint ("101101", 2) <br/>

RunCode

<Br/> function Tobin (intnum) {<br/> var answer = ""; <br/> If (/\ D + /. test (intnum) {<br/> while (intnum! = 0) {<br/> answer = math. ABS (intnum % 2) + answer; <br/> intnum = parseint (intnum/2); <br/>}< br/> If (answer. length = 0) <br/> answer = "0"; <br/> return answer; <br/>} else {<br/> return 0; <br/>}< br/> alert (Tobin (45) <br/>

Run code

<Br/> function Tobin (DEC) {<br/> var bits = []; <br/> var dividend = Dec; <br/> var remainder = 0; <br/> while (dividend> = 2) {<br/> remainder = dividend % 2; <br/> bits. push (remainder); <br/> dividend = (dividend-remainder)/2; <br/>}< br/> bits. push (dividend); <br/> bits. reverse (); <br/> return bits. join (""); <br/>}< br/> alert (Tobin (45) <br/>

Run code

Alert (2 .. tostring (2) // if an error occurs at a certain point, the engine cannot determine whether it is a decimal point or ". "operator, or here a" shift-shift confilt "alert (2.0.tostring (2) Alert (2) is generated ). tostring (2 ))

The word "shift-shift confilt" comes from the compilation principle. I directly read the e-version at the time. I don't know how to translate it. It roughly means that during lalr syntax analysis, when you need to execute the action of popping up a token from the input sequence and pushing it into the syntax stack, this token has two or more different semantics.

Correspondingly, syntax analysis usually has two actions: Shift and reduce. In LR (1), shift-Reduce conflit is acceptable, but shift-shift conflit is not acceptable.
This is roughly the meaning.

 
0 0 0 1 0 0 0 // the first digit is 3rd, which is 2 ^ (3-1) = 4 if the first 1st digits of 1 are 5th bits, then 2 ^ (5-1) = 16 is used in combination with 4 + 16 = 20.
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.