Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic knowledge of java bit operation

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the basic knowledge of java bit operation". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the basic knowledge of java bit operation".

The origin of everything: binary

Bit: binary bit, or "bit" for short. A symbol in a binary numbering system that represents integers less than 2, usually represented by 1 or 0, and is one of the two states with equal probability. The number of bits in a binary bit can represent the word length of a machine word, and the amount of information contained in a binary bit is called a bit.

For example, chestnut: int occupies 4 bytes (byte) 1byte = 8bit, so one int type accounts for 32bitint I = 88; here 88 is decimal, converted to binary: 1011000, using a complete 32-bit representation: 00000000 00000000 00000000 01011000

The 00000000 00000000 0000000001011000 above is the decimal 88 converted to binary source code, and its related definitions include inverse code and complement code.

About the original code, inverse code and complement code

In a computer, signed numbers can be expressed in three ways: original code, inverse code, and complement code. The original code is the binary set point representation, that is, the highest bit is the symbol bit, "0" plus or minus "1", and the remaining bits represent the size of the value. Inverse code: the inverse code of a positive number is the same as its original code; the inverse code of a negative number is a bit-by-bit inversion of the positive number, and the symbol bit is maintained at 1. Complement: the complement of a positive number is the same as its original code; the complement of a negative number is to add 1 to the last bit of its inverse code.

Similarly, we use "88" to illustrate the original code, inverse code, and complement.

Source code of "88": 00000000 00000000 00000000 01011000 "88" reverse code: 0000000000000000000000000001011000 "88" complement: 00000000000000000000000000000 01011000

For the negative number "- 88", the original code, inverse code and complement are as follows:

The original code of "- 88": 10000000 00000000 00000000 01011000 "- 88" inverse code: 11111111111111111111111111111110100111 "- 88" complement: 1111111111111111111111111110101000

Why use complements?

To put it simply, it is inconvenient for the computer to calculate subtraction, so the inverse code is invented, and it is found that the inverse code also has defects (there are two zeros: "+ 0" and "- 0"), and then the complement code is invented to solve this problem.

In computer systems, numerical values are always represented and stored by complements. The reason is that symbol bits and numerical ranges can be processed uniformly by using complements, and addition and subtraction can also be handled uniformly. In addition, the complement code and the original code are converted to each other, and the operation process is the same, and no additional hardware circuit is needed.

The meaning and function of the complement are discussed in great detail in the link above. I won't play tricks here, just understand it.

After we have a preliminary understanding of the original code, inverse code and complement code, we will look at the bit operation much more clearly.

On bit operation

With regard to bit operation, here we use three philosophical extreme questions to try to explain clearly what bit operation is: what is bit operation? The function of bit operation? What are the advantages of bit computing?

What is a bit operation?

All the numbers in the program are stored in binary form in the computer memory. Bit operation is to directly operate on the binary bits of integers in memory. For example, the and operation is originally a logical operator, but and operations can also be performed between integers and integers. For example, if the binary of 6 is 110 and 11 and the binary of 11 is 1011, then the result of 6 and 11 is 2, which is the result of the logical operation of the corresponding binary bits (0 represents True, and all spaces are treated as zeros).

The following table lists the basic operations of the bit operator (A = 8, B = 9)

If both bitwise and & relative bits are 1, the result is 1, otherwise it is 0 A&B=8, that is, 1000 bitwise or | if both bitwise bits are 0, the result is 0, otherwise it is 1 A | Back9, that is, 1001 bitwise XOR ^ if the corresponding bitwise values are the same, then the result is 0, otherwise it is 1 A ^ B = 1, that is, 0001 bitwise inverse ~ bitwise reverse operator reverses every bit of the Operand, that is, 0 becomes 1, 1 becomes 0 ~ 0 ~ A = 7. That is, the 0111 left shift bitwise right shift operator. The left Operand moves bitwise to the right by the specified number of digits A > > 2 = 2, that is, 0010

The function and advantage of bit operation

I try to describe the functions and advantages of clear bit operation away from the practical application scenario, and then find that it is very difficult to separate from the practical application, which is as difficult as buying a lottery ticket. So here we combine the MeasureSpec class in the Android source code to describe the function and advantage of bit operation. Friends who are familiar with Android View system should be familiar with MeasureSpec. If you are not familiar with it, please Google yourself, or you may look a little foggy below. Let's take a look at its code:

Public static class MeasureSpec {private static final int MODE_SHIFT = 30th private static final int MODE_MASK = 0x3

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report