Java Binary Operation (Power Node Java College Arrangement)

  • 2020-06-19 10:09:43
  • OfStack

shift

Most operations in bitwise operations shift left and right. In Java, this corresponds to < < and > > These two operators are shown as follows:


/* 00000001 << 1 = 00000010 */
1 << 1 == 2 
/* 00000001 << 3 = 00001000 */
1 << 3 == 8
/* 11111111 11111111 11111111 11110000 >> 4 = 11111111 11111111 11111111 11111111 */
0xFFFFFFF0 >> 4 == 0xFFFFFFFF 
/* 00001111 11111111 11111111 11111111 >> 4 = 00000000 11111111 11111111 11111111 */
0x0FFFFFFF >> 4 == 0x00FFFFFF

Note: A shift to the right is a signed operator. Like many languages, Java USES the highest bit to indicate positive and negative values, and the highest bit for negative values is always 1. A hexadecimal number starting with 1 will also start with 1 when shifted, and a hexadecimal tree starting with 0 will also start with 0 when shifted. So be careful: Java is bitable in integers.

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


/* 10000000 00000000 00000000 00000000 >>> 1 = 01000000 00000000 00000000 00000000 */
0x80000000 >>> 1 == 0x40000000
/* 10000000 00000000 00000000 00000000 >> 1 = 11000000 00000000 00000000 00000000 */
0x80000000 >> 1 == 0xC0000000

One of the biggest USES is to raise two quickly. 1 shifts to the left 1 is 2, 2 is 4, 3 is 8... Similarly, moving 1 bit to the right is the same thing as dividing by 2.

Another use is to create masks. Bitmasks can be used to mask or modify certain specified bits in a base 2 number, as explained in the next section. Let's say we want to create one

00001000 mask, code 10 minutes simple:


int bitmask = 1 << 3;

You can use bitoperators to create more complex masks, as shown in the next section.

Bitwise operator

Here are four common bit operators in Java:

The & # 61548; The digit is reversed  & � and by location The & # 61548; The digit is different or The & # 61548; | wicks by bit or The & # 61548; The simple application is as follows (to keep things simple, just show base 2)

1010 & 0101 == 0000
1100 & 0110 == 0100
1010 | 0101 == 1111
1100 | 0110 == 1110
~1111 == 0000
~0011 == 1100
1010 ^ 0101 == 1111
1100 ^ 0110 == 1010

For example, you can "set" the specified bit on a base 2 number to 1 through the "or" operation without affecting the other bits.


10000001 | 00100000 = 10100001 /*  The first 5 Bit is set to 1 */
10000001 | 1 << 5 = 10100001 /*  The same effect  */
00000000 | 1 << 2 | 1 << 5 = 00100100

If you want to selectively set someone to zero, you can have the number equal to one with all ones but one with zero.


01010101 & ~(1<<2) == 01010101 & 11111011 == 01010001

About bit order

Assume that the highest bit is on the left:


10010110
^   ^
|   |-------  The first  0  position 
|
|--------------  The first  7  position 

Notice that the 0th bit is 2^0 and the 1st bit is 2^1... The seventh digit is 2 to the seventh.

Using ParseInt

A convenient way to manipulate base 2 Numbers in your code is to use the Integer.parseInt () method. Integer.parseInt(" 101 ",2) represents converting a base 2 number 101 to a base 10 number (5). This means that with this method you can even use base 2 Numbers in the for loop:


/*  from 5 to 15 The cycle of  */
for (int b = Integer.parseInt("0101",2); b <= Integer.parseInt("1111",2); b++) {
  /*  What to do  */
}

A read/write

Recommendation: Implement your own class that converts 2 bits to streams and reads and writes. Try not to use Java's INput-output streams because Java's streams operate only in bytes. You'll find the "give me the next M bit" and "move the pointer forward M bit" functions very useful. For example, you can read enough data to determine the length of the longest Huffman code, and when you get the actual length of the Huffman code you just read, you can move the pointer forward accordingly. One such class can divide the ugly face of bit-arithmetic into a familiar block of code.

Similarly, if you're looking for speed, you'll be surprised how powerful table lookup is. If you have a Huffman code that starts with 0, and all the other codes are 3 and start with 1, that means you need a table that can hold 8(2^3) items. Your table might look like this:


char code[8];
int codelen[8];
code[0] = 'a'; codelen[0] = 1;
code[1] = 'a'; codelen[1] = 1;
code[2] = 'a'; codelen[2] = 1;
code[3] = 'a'; codelen[3] = 1;
code[4] = 'b'; codelen[4] = 3;
code[5] = 'c'; codelen[5] = 3;
code[6] = 'd'; codelen[6] = 3;
code[7] = 'e'; codelen[7] = 3;

After two searches, you can locate the character you are looking for, and you can also find out how much of the next character precedes it. This is much cheaper and more memory efficient than some 1 - round loops to find all the characters.


Related articles: