Java Foundation series -03-Some bit operators how would you understand?

Source: Internet
Author: User

Topic One: XOR or

Let's take a look at some code:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/6F/wKiom1XP6JbDX80rAABR6TGTePY135.jpg "title=" Sogou _ 2015-08-16_09-34-05.png "alt=" Wkiom1xp6jbdx80raabr6tgtepy135.jpg "/>

This code is used to implement A/b exchange.


XOR bitwise operator, 0^1 is 1, other cases are 0, this is an addition does not carry the function!


What's the use of it?


In fact, through the above Exchange example, we have already understood that A^b^a will get B, that is, the ability to achieve data restoration!



Topic Two:% and &


We can number the buckets, like number No. 0, number 1th,... n Barrels.


give us a data, we want to complete this data and the bucket ordinal mapping conversion relationship, so put and find it.






but notice that for example, B is 10, the corresponding binary is 1010, that is, no matter what a is, A&b gets the binary number of bits, The number third must be 0 "assuming right-to-left." Then A&b will not get a number, such as 3, meaning that the 3rd bucket will always get no data! So if you choose &, then the binary of B must be 1 consecutive, not 0 bits.



Topic Three: |

We know that the permissions of Linux files can be divided into rwx, that is, with three bits can be expressed, the following requirements of the design program to achieve a given number of completion of the right to judge.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/6C/wKioL1XP-siQKTd1AAFPYMskMVA268.jpg "title=" Sogou _ 2015-08-16_10-43-59.png "alt=" Wkiol1xp-siqktd1aafpymskmva268.jpg "/>


When it comes to a lot of judgment, we can use & | These bits operate to achieve the goal!



Topic Four:>>> and <<


>>> is unsigned to the right, which is the left fill 0.

>> << is a signed move, fill the sign bit when the vacancy.


>>> >> << These are all operating binary sequence moves, but do we get them in real development?


We know that on the network, sending is a byte stream, such as sending int a = 32;


An int occupies 4 bytes, such as high to Low: B1 B2 B3 B4


For Java, it will be sent in order from highest to lowest, so obviously we must be able to decompose an int into b1,b2, b3,b4; At the same time, the receiver must know how to assemble the B1 B2 B3 B4 into the original data!


"Different languages may send the same byte order, for example, C + + may send from low to High"


Look at a program:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/71/6F/wKioL1XQUuDDNToHAAHQmQ84foQ137.jpg "title=" Sogou _ 2015-08-16_16-59-14.png "alt=" Wkiol1xquuddntohaahqmq84foq137.jpg "/>


The result of the operation is:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/6F/wKioL1XQUwKw686QAABz614RB3U260.jpg "title=" Sogou _ 2015-08-16_16-59-48.png "alt=" Wkiol1xquwkw686qaabz614rb3u260.jpg "/>




This article is from the "Hard Struggle" blog, please make sure to keep this source http://zhangfengzhe.blog.51cto.com/8855103/1685021

Java Foundation series -03-Some bit operators how would you understand?

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.