JS operator single vertical bar "|" The usage and function of JS data processing

Source: Internet
Author: User

Many friends are familiar with the double vertical Bar "| |", because this is often used. But do you know the single vertical bar?

Read JavaScript practical tips, JS small knowledge article, saw a single vertical bar "|" Operation, which is unfamiliar to it.

Learn and master it.

JS operator single vertical bar "|" The role

In JS operation, number | When you are 0:

A. Integer operation, the equivalent of removing the decimal point, parseint.

B. When positive, it is equivalent to rounding down, Math.floor ().

C. When negative, it is equivalent to rounding up, Math.ceil ().

// Rounding up // Rounding down // Rounding Rounded rounding Console.log (//  0//  1//  3//  -5  //  -7

The methods used to deal with numbers are: parseint (), parsefloat (), toFixed (), toprecision ()

ToFixed (), leaving several decimals, rounded, and the result is a string.

// "100.46" // "100.456" // "100.46"

Toprecision (), preserving several numbers, rounding, and the result is a string.

// "99.456" // "100.46" // "10.456"
Single Vertical Bar "|" The arithmetic rules

"Number|0" can achieve the purpose of rounding, if the single vertical bar is not 0, the result will be how much?

// 7 // 4 //  One // 5 // 3455

It seems that there is no rule to follow. Not really, single vertical bar "|" Is the result of the addition of the converted to 2 binary.

// 7 // after conversion to binary 011|110 add to  get 111=7console.log (//  4//  convert to binary after 100|100  add to get 100=4 Console.log (//   /// convert to binary after 1000|011 add to  get 1011=11 Console.log (///  5//  convert to binary after 101|100  add to get 101=5

  

  

JS operator single vertical bar "|" The usage and function of JS data processing

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.