Moving bit operations in JavaScript

Source: Internet
Author: User

Because the technical knowledge is too obvious, an interview in the process of the interviewer asked me a question such as "1>>2=?"

Before the truth has not met, come back Baidu also not too many things, back to see elevation know this is called signed right shift operation , the concrete steps are:

(1) Convert 1 to binary number 00000001 first;

(2) Then move right two bits, the vacancy according to the preceding sign bit 00000000;

(3) The result of 1>>2 is 0;

And if -1>>2=?

(1) The same first conversion binary 11111111;

(2) Step two is the same, according to the symbol bit to the right shift 11111111;

(3) because it is a negative number, the binary negative numbers are converted to decimal numbers with a result of 10000001, that is, the -1>>2 result or-1;

So in terms of the unsigned bit right shift , that is 1>>>2=?, for positive numbers, the unsigned bit right and the signed bit right shift are the same, but for negative numbers, it is different, the following is only for negative numbers to say the unsigned bit right shift:

(1) or first convert decimal into binary number, that is, -1=11111111;

(2) Then the right shift, and the sign bit is different from the right shift is the unsigned bit of the right to move the vacancy is not filled with the sign bit, but with 0, and will be in a positive way to move regardless of the existence of the symbol bit , 00111111;

(3) Therefore, the result of the unsigned bit right shift of a negative number is very large.

Right shift said finished, left and right shift is the same, displacement is not necessary to consider the sign bit, in the back of the vacancy automatically fill 0 can be;

Because of the decimal and binary conversion issues mentioned, by the way:

Decimal Turn binary:

Positive, direct conversion, e.g. 2 = 00000010

Negative number, the first to find the absolute value of the binary and then in the inverse code plus a can, such as: -2=|-2| anti-+1=11111101+1=11111110;

Binary goto decimal:

Positive, like direct conversion, such as 00001000=8;

Negative number, first minus one in seeking its inverse code, the sign bit remains unchanged, such as 11111001 = (-1111000-1) anti-=-0001000, and then normal conversion-8;

Moving bit operations in JavaScript

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.