Bitwise operations in Android

Source: Internet
Author: User

Original article, if reproduced, please indicate the source: http://blog.csdn.net/yihui823/article/details/6754038

In fact, it is the same as Java, and Java is also common.

Our understanding of numbers starts from decimal. Since childhood, the education started from 1 + 9 = 10. However, for today's computers, their world is a binary world. The computer uses a high level to indicate 1, and a low level to indicate 0.

If we want to represent the decimal number 10, for the computer, we must convert it to the binary number 1010. Of course, the computer not only uses "0" and "1" to represent data, but also uses "0" and "1" to represent letters, operations, operations, and reasoning, represents everything. For all operations on the computer, the original operations are "0" and "1.

The original operations on these computers are also different from what we know. The basic operation we learned, such as 1 + 1 = 2, is an addition and cannot be split. However, the original operations on the computer only involve operations such as "and", "or", "unusual", "Non", and "displacement. Addition can be achieved only by combining these original operations.

The description is general. This is not a sentence or two. In the current advanced language, the operators of addition and bitwise operation can be used at the same time, so we do not need to go too far into the bitwise Operation to implement addition, you only need to know that there is a bitwise operation.

The following are basic bitwise operations:

0 & 0 = 0; 0 & 1 = 0; 1 & 0 = 0; 1 & 1 = 1

0 | 0 = 0; 0 | 1 = 1; 1 | 0 = 1; 1 | 1 = 1

0 ^ 0 = 0; 1 ^ 0 = 1; 0 ^ 1 = 1; 1 ^ 1 = 0

In addition, the concept of bytes needs to be described. Byte is a unit of measurement used by Computer Information Technology to measure storage capacity and transmission capacity. one byte equals to eight bits. The Byte in Java is the byte.

For an unsigned integer, the maximum number of energy-saving characters is 255, that is, "11111111 ". But for a signed integer, because the first digit on the Left needs to be used to represent positive and negative, the maximum number that can be expressed is 127. That is, "0111111 ". The smallest number that can be expressed can also be calculated.

In Java, an int is expressed in four bytes. Because Java does not have the unsigned integer type, the first digit must be a symbol bit.

If we forcibly convert a byte to the int type, the lowest 8 bits of the int type are the same as those of the byte type. The previous three bytes are filled with the symbol bit. That is to say, if it is a negative byte, after it is converted to int, the first three bytes are all 1.

We can use the following code:

Byte b1 = 10; system. out. println ("b1 =" + byte. tostring (B1); system. out. println ("binary after B1 is converted to int:" + integer. tobinarystring (B1); byte b2 =-10; system. out. println ("b2 =" + byte. tostring (B2); system. out. println ("binary After B2 is converted to int:" + integer. tobinarystring (B2 ));

 

Result:

B1 = 10

Binary value after B1 is converted to int: 1010

B2 =-10

Binary Value After B2 is converted to int: 11111111111111111111111111110110

B1 is a positive number. After it is converted to the int type, it is filled with 0, so it is still 00001010, but 0000 is removed when it is converted to the string type.

B2 is a negative number. After it is converted to the int type, the first value is 1, so it is 11111111111111111111111111110110.

Careful friends must have discovered something wrong. Yes, the binary value of-10. The last few digits are 11110110, which is not what we think of as 10001010. Here is a concept called Code complement.

Why is there such a concept. It also needs to be indicated by the symbol bit. If only the sign bit is used to indicate positive and negative, and the subsequent data is the same, there will be a problem: + 0 and-0. Positive numbers can be expressed from 0 ~ 127. Negative Numbers can represent-0 ~ -127, then a total of 255 numbers can be expressed, because 0 and-0 are a number. To solve this problem, we have the concept of Code complement. That is to say, if it is a negative number, in addition to the symbol bit expressed with 1, the remaining digits are reversed, and then-1.

Then, the binary calculation steps of-10 should be:

1. the binary value of 10 is 0001010.

2, minus one, that is, 0001001

3. In turn, it is 1110110.

4. Add the symbol bit, which is 11110110.

In this case, there is no concept of-0. 10000000 indicates the number, which should be:

1. Remove the symbol bit. The data bit is 0000000

2. Because the symbol bit is 1, you need to compress the code.

3. In turn, it is 1111111.

4. Add 1 to 10000000.

Therefore, the decimal number represented by 10000000 is-128.

In this way, a byte can represent-128 ~ 127, which is exactly 256 digits.

What if we only need to display the binary number of bytes? Then we will first convert the byte into an int, and then remove all the first three bytes, leaving only the data of the last byte. The Code is as follows:

Byte b1 = 10; system. out. println ("b1 =" + byte. tostring (B1); system. out. println ("B1 binary:" + integer. tobinarystring (0xff & B1); byte b2 =-10; system. out. println ("b2 =" + byte. tostring (B2); system. out. println ("B2 binary:" + integer. tobinarystring (0xff & B2 ));

This is mainly because we cannot find the Byte ClassTobinarystringFunction, so we have to use integer.

Here we also need to explain the displacement operation. If one byte is moved to the left, the rightmost side is supplemented with 0, and the leftmost data is lost. The right shift is different. The leftmost complement is the symbol bit, that is, the highest bit. If it is a negative number, it is still a negative number after the right shift.

Read this Code:

Byte b1 = 10; system. out. println ("b1 =" + byte before displacement. tostring (B1); system. out. println ("binary of B1 before displacement:" + integer. tobinarystring (0xff & B1); b1 = (byte) (b1> 2); system. out. println ("b1 =" + byte after displacement. tostring (B1); system. out. println ("binary of B1 after displacement:" + integer. tobinarystring (0xff & B1); byte b2 =-10; system. out. println ("b2 =" + byte before displacement. tostring (B2); system. out. println ("binary of B2 before displacement:" + integer. tobinarystring (0xff & B2); b2 = (byte) (B2> 2); system. out. println ("b2 =" + byte after displacement. tostring (B2); system. out. println ("binary of B2 after displacement:" + integer. tobinarystring (0xff & B2 ));

The result is:

B1 = 10 before displacement

Binary value of B1 before displacement: 1010

B1 = 2 after displacement

Binary value of B1 after displacement: 10

B2 =-10 before displacement

Binary value of B2 before displacement: 11110110

B2 =-3 after displacement

Binary 11111101 of B2 after displacement

 

Generally, under what circumstances will the operation be performed in place? My summary:

1. When dealing with devices

Basically, both the interface and the device interface are passed byte arrays. If the byte array needs to be converted to a string, bitwise operations are not required. You only need to decode the byte array. However, if it is converted to a value such as int/long, it must be converted using bitwise operations.

2. Quick Computation

When it needs to be × 2, the first bit is directly shifted to the left, and the operation is fast, saving the CPU.

3. Memory saving

It is easier to directly use bitwise operations to determine a byte than to convert it into a string.

What else can I think of at once. I will show several pieces of code later to demonstrate the benefits of bitwise operations.

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.