Java I/o: bit operation bitwise operation

Source: Internet
Author: User
Tags bitwise

Writer:bysocket (mud and brick pulp carpenter)

Weibo: Bysocket

Watercress: Bysocket

Facebook:bysocket

Twitter:bysocket

Masons like Java, and the article always pulls Java. I/O basis is the binary, which is the Bit.

Bit vs. binary

What is a bit (bit)? Bit is the unit of CPU processing or the smallest data storage. It is similar to a small, small switch that is opened and closed, and is expressed as 1 or 0. So this is the "cell" in which the computer processes any data, so keep in mind.

The binary system is just a kind of norm and convention in the computational circle, which is exactly a numerical code. Saying "every two in one," This is actually an algorithm.

Second, bitwise operations

After two points, the Mason takes you to the concept of bit arithmetic. The number is stored in memory in binary. Bit operations, that is, binary operations, are actually logarithmic in the memory of the binary direct operation of the process. Here's a question, man.

Q: All are the same, the bit operation does not necessarily result in different.

A: That's a good question. Bit operations are not a good thing in a common scenario. And under the specific, the accesses than either calculation is convenient, the speed is fast, the support surface is wide . For example, I would like to cite a few examples, you can use the bitwise operation to determine the number of odd numbers, and some specific algorithms such as compression, encryption algorithm relies heavily on bit algorithm. The two have small storage bytes , such as the permissions of the file (read, write, etc.) are represented by bits.

Above, the bit operation is illustrated with a small dialogue form and an example. The following starts the actual combat.

Third, the operator of the actual combat

One, bitwise operations and & {Two bits full 1, the result is 1}

function :

① zeroed. If the specific field needs to clear 0, then the other segments with you 0 and the operation can be. 0xFF & ... You can make the rest of the 8-bit on the right 0 clear.

The ② takes a number in the position of the finger. 1010 1110 & 0xF can get 1010 1110 lower 4 bits.

two-bit arithmetic or | {As long as there is a 1, the result is 1}

function: used to fill 1, where 1 is required to fill where.

Three, XOR, or operation ^{Two response bits are different, 1}

function : Special positioning Flip 1010 1110 ^ 0xF = 1010 0001

Iv. shift operation left and right shift

< < left : fill 0 on the right-hand side, and the left bit will be squeezed out of the word, and its value is equal to 2.

The right shift is not repeated.

Here again add a solution to the above Masons mentioned how to judge an odd number of even numbers.

?
1234 // Is it odd?(value & 0x1) > 0// Is it divisible by two (even)?(value & 0x1) == 0

Iv. Summary

Bit bit is small but cornerstone, hollows. Water droplets-a bit, it is worth our good understanding. Here the complex compression algorithms are these, which are later written to the algorithm again.

Points:

Concept

Operator operations

Writer:bysocket (mud and brick pulp carpenter)

Weibo: Bysocket

Watercress: Bysocket

Facebook:bysocket

Twitter:bysocket

Java I/o: bit operation bitwise operation

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.