About JavaScript | How to use the single vertical bar operator _ Basics

Source: Internet
Author: User

JS operator single vertical bar "|" The role

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 the Math.ceil () Note:

1. Math.ceil () is used for rounding up.
2. Math.floor () is used as a downward rounding.
3. Math.Round () The rounded rounding we used in maths.

Console.log (0.6|0)//0
console.log (1.1|0)//1 console.log
(3.65555|0)//3 console.log
(5.99999|0)//5
Console.log ( -7.777|0)//-7

Note: We often use parseint (), parsefloat (), toFixed () and toprecision () and so on, in addition to the math's three methods for handling numbers. Simple explanation:

The Tofixed method uses the following:

100.456001.toFixed (2); 100.47

100.456001.toFixed (3);//100.456

Number.prototype.toFixed.call (100.456001,2);//100.47

Disadvantage: After use, it will become a string.

Toprecision usage is as follows:

99.456001.toPrecision (5); 99.456

100.456001.toPrecision (5);//100.46

Number.prototype.toPrecision.call (10.456001,5);//10.456

Rule of operation of single vertical bar

Look at the above example, the general know that a single vertical bar can be used to take the whole operation, that is, only the positive part, the decimal part by taking off, but "|", and how to operate it, why can "|" 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 examples:

Console.log (3|4); 7
Console.log (4|4);//4 Console.log (8|3);//11 Console.log (5.3|4.1);//5 console.log
(9|3455);/ 3455

All right, I'm here to announce the answer. In fact, single vertical bar "|" Is the result of the addition of the conversion to the 2 binary. For example, let's take a simple example:

Copy Code code as follows:
3|4

After converting to binary 011|100 add to 111=7

Copy Code code as follows:
4|4

After converting to binary 100 |100 add to get 100=4

Copy Code code as follows:
8|3

After converting to binary 1000 |011 add to get 1011=11

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.