Current location - Training Enrollment Network - Mathematics courses - Bit operators in java and their usage.
Bit operators in java and their usage.
Bit logic operators are AND, OR, XOR and NOT, which mean & |, ~ and ~ respectively.

The following example illustrates the bit logic operator:

//Demonstrates bitwise logical operators.

Class bit logic (

Public static void main (strinargs []) {

String binary [] = {

"0000", "000 1", "00 10", "00 1 1", "0 100", "0 10 1", "0 1 10", "0 1 1 1",

" 1000", " 100 1", " 10 10", " 10 1 1", " 1 100", " 1 10 1", " 1 1 10", " 1 1 1 1"

};

int a = 3; //binary 0+2+1 or 00 1 1

int b = 6; //Binary 4+2+0 or 0 1 10

int c = a | b;

int d = a & ampb;

int e = a ^ b;

int f =(~ a & amp; b)|(a & amp; ~ b);

int g = ~ a & amp0x0f

system . out . println(" a = "+binary[a]);

system . out . println(" b = "+binary[b]);

system . out . println(" a | b = "+binary[c]);

system . out . println(" a & amp; b = "+binary[d]);

system . out . println(" a^b = "+binary[e]);

system . out . println(" ~ a & amp; b | a & amp~ b = "+binary[f]);

system . out . println(" ~ a = "+binary[g]);

}

}

In this example, the combination of the corresponding bits of variables A and B represents all four combinations of binary numbers: 0-0, 0- 1, 1-0 and 1- 1. The |' operator and the & operator operate on the corresponding bits of variables A and B, respectively, and get the values of variables C and D. The assignment of variables E and F illustrates the role of operators. The string array binary represents binary values from 0 to 15. In this example, the arrangement order of the array elements shows the binary code of the corresponding value of the variable. The reason why the array is constructed in this way is that the binary code corresponding to the value n of the variable can be correctly stored in the element binary[n] corresponding to the array. For example, if the value of variable A is 3, its binary code is stored in the array element binary[3] accordingly. The purpose of bitwise AND operation of the value of ~a and the number 0x0f (corresponding to the binary number 000011) is to reduce the value of ~a and ensure that the result of the variable g is less than 16. Therefore, the running result of this program can be represented by the corresponding elements of array binary. The output of this program is as follows:

a = 00 1 1

b = 0 1 10

a|b = 0 1 1 1

a & ampb = 00 10

a^b = 0 10 1

~ a & ampb | a & amp~b = 0 10 1

~a = 1 100

Left shift operator

Left shift operator

Value < lt < number

Here, num specifies the number of digits to move the value. Which is the left shift operator.

You must be careful when shifting values of type byte and short. Because you know that Java will automatically extend these types to int when evaluating expressions, and the value of expressions is also int. The result of shifting the values of byte and short type is int type. If the left shift is less than 3 1 bit, the original corresponding value of each bit will not be discarded. However, if the negative value of byte or short type is shifted, its symbol will be expanded after being expanded to int type. In this way, the high order of the integer value result will be filled by 1 So, in order to get the right result, you have to give up the high position of getting the result. The simplest method is to convert the result into bytes. The following process illustrates this point:

//Shift left by one byte value.

class ByteShift {

Public static void main (strinargs []) {

Byte a = 64, b;

int I;

i = a & lt& lt2;

B = (bytes) (a < < lt2);

System. out.println ("initial value of a:"+a);

System.out.println("i and B: "+I+""+B);

}

}

The output generated by this program is as follows:

Original value of a: 64

I and b: 256 0

Because the dependent variable A is in the assignment expression, it is extended to the int type, and 64(0 100 0000) is shifted to the left twice to generate the value 256( 10000 0000) assigned to the variable I ... But after the left shift, only 1 in the variable B is moved out.

Because every shift to the left can double the original operand, programmers often use this method to multiply by 2 quickly. But you should be careful. If you move 1 into the higher order (3 1 or 63 bits), the value will become negative. The following process illustrates this point:

//Move left as a quick way to multiply by 2.

MultByTwo class {

Public static void main (strinargs []) {

int I;

int num = 0xFFFFFFE

for(I = 0; I<4; i++) {

num = num & lt& lt 1;

system . out . println(num);

}

}

}

The output of this program is as follows:

536870908

107374 18 16

2 147483632

-32

The initial value is carefully selected so that it will produce -32 after shifting to the left by 4 bits. It can be seen that when 1 is moved into 3 1, the number is interpreted as a negative number.

Right shift operator

Right shift operator >> shifts all bits of the specified value to the right a specified number of times. Its general format is as follows:

Value & gt& gt number

Here, num specifies the number of digits to move the value. That is, the right shift operator >:> shifts all bits of the specified value right by num bits.

The following program fragment shifts the value 32 to the right twice and assigns the result 8 to the variable a:

int a = 32

a = a & gt& gt2; // a now contains 8.

When some bits in the value are "shifted out", the values of these bits will be discarded. For example, the following program fragment moves 35 to the right twice, its two lower bits are deleted and discarded, and the result 8 is also assigned to the variable a:

int a = 35

a = a & gt& gt2; // a still contains 8

Using binary to represent the process, you can see the running process of the program more clearly:

00 1000 1 1 35

& gt& gt2

0000 1000 8

Moving the value to the right every time is equivalent to dividing the value by 2 and discarding the remainder. You can use this function to quickly divide an integer by 2. Of course, you must make sure that you don't move any original numbers.

When moving to the right, the deleted highest digit (the leftmost digit) is supplemented by the original highest digit. For example, if the value to be deleted is negative, it will be shifted every right, and1will be added to the left; If the value to be deleted is positive, it is shifted to the right every time, and 0 is added to the left. This is called sign extension, which is used to keep the sign of negative numbers in the right shift operation. For example, --8 >>;; 1 is–4, which is expressed in binary as follows:

1 1 1 1 1000 –8

& gt& gt 1

1 1 1 1 1 100 –4

An interesting problem to note is that since the sign bit extension (reserved sign bit) is always complementary to 1 in high order, the result of shifting-1 to the right is always–1. Sometimes, when moving to the right, you don't want to keep the symbol. For example, the following example converts a byte value to a hexadecimal representation. Note that the value shifted to the right is bitwise AND-operated with 0x0f, so that any sign bit extension can be discarded, and the obtained value can be used as a subscript to define the array, and the hexadecimal characters represented by the corresponding array elements can be obtained.

//Mask symbol extension.

Hexadecimal byte class (

Static public void main(String args[]) {

char hex[] = {

'0', ' 1', '2', '3', '4', '5', '6', '7',

' 8 ',' 9 ',' a ',' b ',' c ',' d ',' e ',' f ' '

};

Byte b = (byte) 0xf1;

system . out . println(" b = 0x "+hex[(b & gt; & gt4)& amp; 0x0f]+hexadecimal. 0x0f]);

}

}

The output of this program is as follows:

b = 0xf 1

Unsigned right shift

As you can see above, every time you move to the right. An operator always automatically supplements its highest bit with the contents of its previous highest bit. This preserves the sign of the original value. But sometimes this is not what we want. For example, if the operand of the shift operation is not a numeric value, you don't want to expand the sign bit (keep the sign bit). This is common when dealing with pixel values or graphics. In this case, no matter what the initial value of the operand is, you want to always add 0 to the high bit (leftmost) after the shift. This is what people call unsigned shift. At this time, you can use Java's unsigned right shift operator >; & gt& gt It always adds 0 to the left. The following program segment illustrates the unsigned right shift operator >; & gt& gt。 In this example, the variable A is assigned to-1, which means that all 32 bits are 1 in binary. The value is then unsigned right shifted by 24 bits. Of course, it ignores the sign bit extension and always adds 0 to its left. The value 255 thus obtained is assigned to the variable a.

int a =- 1;

a = a & gt& gt& gt24;

This operation is further explained in binary form as follows:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 165438

& gt& gt& gt24 Unsigned right shift by 24 bits.

00000000000000000000000000000111111/kloc-0. Because you must remember that too small a value in an expression is always automatically extended to an int type. This means that the sign bit expansion and shift always occur in 32 bits, not 8 bits or 16 bits. In this way, it is impossible to move the byte value starting with 0 in the seventh bit, because in the actual move operation, the extended 32-bit value is operated. The following example illustrates this point:

//Unsigned shift by one byte value.

class ByteUShift {

Static public void main(String args[]) {

char hex[] = {

'0', ' 1', '2', '3', '4', '5', '6', '7',

8 ',' 9 ',' a ',' b ',' c ',' d ',' e ',' f '

};

Byte b = (byte) 0xf1;

Byte c = (byte) (b>& gt4);

Byte d = (byte) (b>& gt& gt4);

Byte e = (byte) ((b&; 0xff)>& gt4);

System.out.println(" b = 0x "

+hexadecimal [(b > & gt4)& amp; 0x0f]+hexadecimal. 0x0f]);

system . out . println(" b & gt; & gt4 = 0x "

+hexadecimal [(c > & gt4)& amp; 0x0f]+hexadecimal. 0x0f]);

system . out . println(" b & gt; & gt& gt4 = 0x "

+hexadecimal [(d > & gt4)& amp; 0x0f]+hexadecimal. 0x0f]);

system . out . println("(b & amp; 0xff)>& gt4 = 0x "

+hexadecimal [(e > & gt4)& amp; 0x0f]+hexadecimal. 0x0f]);

}

}

The output of the program shows the unsigned right shift operator >; When & gt& gt processes byte values, it actually does not directly operate on byte values, but extends to int type before processing. In this example, the variable b is given an arbitrary negative byte value. Variable B is shifted to the right by 4 bits and then converted into byte type, and the obtained value is assigned to variable C. Because the sign bit is extended, the value is 0xff. Variable B is converted into byte type after 4-bit unsigned right shift, and the value obtained is assigned to variable D. You may expect 0x0f, but it is actually 0xff, because variable B has been extended to int type before shift, and there is already a sign extension bit. The last expression changes the value of variable B to 8 bits by bitwise AND operation, then shifts it to the right by 4 bits, and then assigns the obtained value to variable E, and this time the expected result is 0x0f. Since the state of the sign bit is clear after bitwise AND operation on the variable D (whose value is 0xff), please note that there is no unsigned right shift operation on the variable D. ..

B = 0xf 1

b & gt& gt4 = 0xff

b & gt& gt& gt4 = 0xff

(b & amp0xff)>& gt4 = 0x0f

Bit operator assignment

All binary bit operators have an abbreviated form, which combines assignment and bit operation. For example, the following two statements shift the variable A by 4 bits to the right and assign it to A:

a = a & gt& gt4;

a & gt& gt= 4;

Similarly, the following two statements assign the operation result of expression A or B to A:

a = a | b;

a | = b;

The following program defines several int variables, and then assigns the calculated values to the corresponding variables in the form of bit assignment shorthand:

OpBitEquals class {

Public static void main (strinargs []) {

int a = 1;

int b = 2;

int c = 3;

a | = 4;

b & gt& gt= 1;

c & lt& lt= 1;

^= c;

system . out . println(" a = "+a);

system . out . println(" b = "+b ");

system . out . println(" c = "+c ");

}

}

The output of this program is as follows:

a = 3

b = 1

c = 6