Java binary operation (power node)

displacement

Most operations in bit operations are shift left and shift right. In Java, this corresponds to the two operators < < and > >. Examples are as follows:

Note: shift right is a signed operator. Like many languages, Java uses the highest bit to represent the positive and negative of a number, and the highest bit of a negative number is always 1. A binary number beginning with 1 will start with 1 after shifting, and a binary tree beginning with 0 will start with 0 after shifting. So be careful: Java can perform bit operations in integers.

You can use a third operator called the unsigned shift right operator: > > > to implement a shift filled with "0", which ignores the sign bit and always fills with "0".

One of the biggest uses is to quickly find the power of 2. Shift 1 bit to the left is 2, shift 2 bits is 4, shift 3 bits is 8... Similarly, shift 1 bit to the right is equivalent, so divide the number by 2.

Another use is to create masks. Bit mask can be used to mask or modify some finger positions in a binary number, which will be explained in detail in the next section. Suppose we want to create one

00001000 mask, the code is very simple:

You can use bitwise operators to create more complex masks. The next section will also explain bitwise operators.

Bit operator

Here are four common bit operators in Java:

For example, you can use the "or" operation to "set" the finger position on a binary number to 1 without affecting other bits.

If you want to selectively set a bit to 0, you can sum a number with all 1 but a bit to 0.

About bit order

Suppose the highest bit is on the left:

Note that the value of bit 0 is 2 ^ 0, the first bit is 2 ^ 1,..., and the value of bit 7 is 2 ^ 7.

Using parseInt

A convenient way to manipulate binary numbers in your code is to use integer Parseint() method. Integer. ParseInt ("101", 2) means to convert binary number 101 to decimal number (5). This means that you can even use binary numbers in the for loop with this method:

Bit read write

Suggestion: implement a class to convert binary bits (bits) into streams and read and write. Try not to use Java input and output streams, because Java streams can only operate by bytes. You will feel "give me the next N bits" and "move the pointer forward by M bits" This function is very practical. For example, you can read enough data to determine the length of the longest Huffman code. When you get the actual length of the Huffman code you just read, you can move the pointer forward to the corresponding length. Such a class can divide the ugly side of bit operation into a familiar code block.

Similarly, if you pursue speed, you will be surprised to find that table lookup is so powerful. If you have a Huffman code that starts with 0, and other codes are all 3 in length and start with 1, this means that you need a table that can hold 8 (2 ^ 3) items. Your table may be like this:

By searching twice, you can locate the character you are looking for, and you can also know how many positions the next character is in front of. This is more cost-effective than some over and over loops to find all characters, and saves more memory.

The above is the Java binary operation (power node Java college sorting) introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>