Discussion on bitwise operators and displacement operations, and operator displacement operations

Source: Internet
Author: User

Discussion on bitwise operators and displacement operations, and operator displacement operations

I have never understood how to calculate this, because I have never used it before, but now I want to understand it, so it is a good idea to understand it all at once, you don't have to worry about it in the future.

The bitwise operator refers to the calculation of a number as a binary value, and converts the calculated binary value to a decimal value, which is the final result.

 

For example:

   7&9

This is bitwise AND. We will not discuss it or not. In short, it will convert 7 and 9 to binary. Next we will convert 7 and 9 to binary, here I use python.

It doesn't matter if you don't understand this code, because the focus here is not on python, but on bitwise operators. here we can see that the binary value of 7 is 111, and the binary value of 9 is 1001. Obviously, we can see that the binary length of 7 and 9 is different. If the length is different, the front of the short one will automatically fill in 0. The result is as follows:

 

7:0111

9:1001

The so-called bitwise operator is used to compare the binary values of two numbers. So how can we compare them, if it is a bitwise AND, then compare whether two numbers are both 1. If it is 1, otherwise it is 0. See the following.

 

For example, for the first and first, they are 0. As long as one of them is 0, it is zero. Of course, this is for location.

 

The overall comparison is as follows:

1 1 1

0 0 1

Result: 0 0 0 1

Then the binary result is 0001, and then the binary 0001 is converted to 1 in decimal format. The result of 7 & 9 is 1.

We can use the following tools to help us switch.

 

Don't ask me about other things. I only say the bitwise operator, because I don't know anything about others. As for conversion, I am Baidu too, however, my goal has been achieved, because I didn't come to learn hexadecimal conversion. I have a good saying in one sentence. I don't forget my mind, so I always have. This is also a way of learning. I used to learn and struggle with this, so I have never learned well, because some of them are not what you should consider now. You need to know what you should do.

 

So bitwise OR, bitwise is not, bitwise is inverse, it is also a truth, but it is just a different pattern for comparison, and then use the bitwise OR to demonstrate it.

By bit or

5|6

Convert binary:

5:101

6: 110

As long as 1 is 1

Result: 111. This is binary.

 

Let's see if there are any rules for this bitwise operator.

// Convert to binary

Console. log (1). toString (2); // 1
Console. log (2). toString (2); // 10
Console. log (3). toString (2); // 11
Console. log (4). toString (2); // 100
Console. log (5). toString (2); // 101
Console. log (6). toString (2); // 110

 

Console. log (1 & 0); // 0

Console. log (1 & 1); // 1

Console. log (1 & 2); // 0
Console. log (1 & 3); // 1
Console. log (1 & 4); // 0
Console. log (1 & 5); // 1
Console. log (1 & 6); // 0

 

Here we can see that 1 & all singular values are 1, while 1 & all double numbers are 0. (Including negative numbers)

 

Console. log (2 & 0); // 0
Console. log (2 & 1); // 0
Console. log (2 & 2); // 2
Console. log (2 & 3); // 2
Console. log (2 & 4); // 0
Console. log (2 & 5); // 0
Console. log (2 & 6); // 2
Console. log (2 & 7); // 2
Console. log (2 & 8); // 0
Console. log (2 & 9); // 0
Console. log (2 & 10); // 2
Console. log (2 & 11); // 2
Console. log (2 & 12); // 0
Console. log (2 & 13); // 0
Console. log (2 & 14); // 2
Console. log (2 & 15); // 2

 

Here we can see that the rule of 2 is 0 0 2 2 the management that can be calculated by the mouth is really not seen.

Let's look at another group.

console.log(3&0); //0
console.log(3&1); //1
console.log(3&2); //2
console.log(3&3); //3

console.log(3&4); //0
console.log(3&5); //1
console.log(3&6); //2
console.log(3&7); //3

console.log(3&8); //0
console.log(3&9); //1
console.log(3&10); //2
console.log(3&11); //3

console.log(3&12); //0
console.log(3&13); //1
console.log(3&14); //2
console.log(3&15); //3

Or

Console. log (3 | 0); // 3
Console. log (3 | 1); // 3
Console. log (3 | 2); // 3
Console. log (3 | 3); // 3

Console. log (3 | 4); // 7
Console. log (3 | 5); // 7
Console. log (3 | 6); // 7
Console. log (3 | 7); // 7

Console. log (3 | 8); // 11
Console. log (3 | 9); // 11
Console. log (3 | 10); // 11
Console. log (3 | 11); // 11

Console. log (3 | 12); // 15
Console. log (3 | 13); // 15
Console. log (3 | 14); // 15
Console. log (3 | 15); // 15

Rule interval 4 plus 4.

 

Other rules are not enough.

 

OK. This is a pleasant process. If the goal is met, the bitwise operation can be completed.

 

Bitwise operations have taken a long time to understand T_T. Let's write down the principle of bitwise operations.

First shift right> click operation.

 

Example: 16> 2 = 4

It first converts 16 to binary, and then moves the binary two places to the right.

16 binary: 0001 0000

Moving two digits to the right becomes 0000 0100. Why do we move two digits because we wrote 2 as above?> the following figure shows how many digits you want to move. In fact, we still don't understand it here. Why did the first 0001 change to 0000? Yes:

16> 2 indicates that two zeros or two zeros are added before the binary value of 16, and whether to add zero or 1 is determined by> whether the number on the left is positive or negative, if it is a positive number, it will fill 0, and if it is a negative number, it will fill 1.

 

0001 0000

Fill 0 because 16 is a positive number.

0000 0100 00 has exceeded two zeros to remove the excess, so it is:

0000 0100

 

Another example is 5> 3 = 0.

The binary value of 5 is: 0000 0101

Result: 0000 0000

 

What about left shift? The opposite is true.

 

For example, 5 <3 = 40

The binary value of 5 is: 0000 0101

Result: 0010 1000

What do you mean? It is to add 0 or 1 to 0000 and then delete the excess.

000 0010 1000 Delete the first three zeros.

 

This is the case. Can you understand what you can do on your own.

 

See this article on the Internet is still good: http://www.cnblogs.com/yyangblog/archive/2011/01/14/1935656.html

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.