JS operator single vertical bar "|" What is the use and function of this?

Source: Internet
Author: User
Tags add integer log

The role of the  JS operator single vertical bar

  A lot of friends are "", the Double vertical bar "", very familiar, because this often used. But do you know the single vertical bar? Today, a netizen QQ asked me, my JavaScript practical skills, JS small knowledge, this article inside, JS integer operation using a single vertical bar, ask me what is the meaning of single vertical bar?   I looked at the previous article and only used it, but there was no explanation. OK, I will give you a brief introduction:   Before the article, in the JS integer operation, the equivalent of removing the decimal point, parseint. When a positive number is equivalent to Math.floor (), a negative number is equivalent to Math.ceil () Note:   1. Math.ceil () is used as the rounding up. 2. Math.floor () is used as a downward rounding. 3. Math.Round () The rounded rounding we used in maths.   Console.log (0.60)//0 Console.log (1.10)//1 Console.log (3.655550)//3 console.log (5.999990)//5 Console.log (- 7.7770//-7 Single vertical bar operation rules   Look at the above example, generally know that the single vertical bar can be used to take the whole operation, that is, only the positive part, the fractional part by taking off, but "0", and how to do it, why can "0" to achieve the purpose of rounding? The single vertical bar is not 0 What is the number?   With these questions, let's look at the following example:   Console.log (34); 7 Console.log (//4 console.log);//11 Console.log (5.34.1);//5 Console.log (93455);//3455 seems to have no law to look for? Search online. Http://tool.oschina.net/commons?type=6   This refers to the single vertical bar "" but no JavaScript.   Well, I'm here to announce the answer. In fact, the single vertical bar "" is converted to 2 after the addition of the result. For example, we take a simple example:   34 after the conversion to binary 011100   add to get 111=7   44 converted to binary after the conversion of the 100=4   83 after the conversion to binary 1000 011   Add to get 1011=11 and so on, I'm not here.Listed, the single vertical bar "" Operation is converted to 2 after the addition of the result!

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.