Java Bitwise un Shift operatori (ar piemēriem)

Šajā apmācībā mēs ar piemēriem uzzināsim par operētājsistēmas bitiem un dažādiem Java maiņu operatoru veidiem.

Java operētājsistēmas bitos veic darbības ar veselu skaitļu datiem individuālā bitu līmenī. Lūk, datu skaitlis ietver byte, short, int, un longdatu veidus.

Bitu līmeņa operāciju veikšanai Java ir 7 operatori.

Operators Apraksts
| Bitwise OR
& Bitu virzienā UN
^ Bitor XOR
~ Bitwise papildinājums
<< Kreisā maiņa
>> Parakstīta labā maiņa
>>> Neparakstīta labā maiņa

1. Java Bitwise OR Operators

|Operators bitu izteiksmē OR atgriež 1, ja vismaz viens no operandiem ir 1. Pretējā gadījumā tas atgriež 0.

Šajā patiesības tabulā parādīts operētājsistēmas OR bitu darbība. Ļaujiet a un b būt diviem operandiem, kas var iegūt tikai bināras vērtības, ti, 1 vai 0.

a b a | b
0 0 0
0 1 1
1 0 1
1 1 1

Iepriekš minētā tabula ir pazīstama kā "Patiesības tabula" operatoram bit bit OR.

Apskatīsim divu veselu skaitļu 12 un 25 bitu darbību OR.

 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise OR Operation of 12 and 25 00001100 | 00011001 ____________ 00011101 = 29 (In Decimal)

1. piemērs

 class Main ( public static void main(String() args) ( int number1 = 12, number2 = 25, result; // bitwise OR between 12 and 25 result = number1 | number2; System.out.println(result); // prints 29 ) )

2. Java Bitwise UN operators

&Operators bitu bāzē AND atgriež 1 tikai tad, ja abi operandi ir 1. Pretējā gadījumā tas atgriež 0.

Nākamajā tabulā parādīts, kā darbojas operētājsistēma bitā UN. Ļaujiet a un b būt diviem operandiem, kas var iegūt tikai bināras vērtības, ti, 1 un 0.

a b a & b
0 0 0
0 1 0
1 0 0
1 1 1

Apskatīsim divu veselu skaitļu 12 un 25 bitu darbību.

 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) // Bitwise AND Operation of 12 and 25 00001100 & 00011001 ____________ 00001000 = 8 (In Decimal)

2. piemērs

  class Main ( public static void main(String() args) ( int number1 = 12, number2 = 25, result; // bitwise AND between 12 and 25 result = number1 & number2; System.out.println(result); // prints 8 ) )

3. Java Bitwise XOR operators

BITĀ XOR ^operators atgriež 1 tikai tad, ja viens no operandiem ir 1. Tomēr, ja abi operandi ir 0 vai ja abi ir 1, tad rezultāts ir 0.

Šajā patiesības tabulā parādīts XOR operatora bitu darbība. Ļaujiet a un b būt diviem operandiem, kas var iegūt tikai bināras vērtības, ti, 1 vai 0.

a b a & b
0 0 0
0 1 1
1 0 1
1 1 0

Apskatīsim divu veselu skaitļu 12 un 25 bitu darbību XOR.

 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) // Bitwise XOR Operation of 12 and 25 00001100 00011001 ____________ 00010101 = 21 (In Decimal)

4. piemērs: XOR pa bitiem

 class Main ( public static void main(String() args) ( int number1 = 12, number2 = 25, result; // bitwise XOR between 12 and 25 result = number1 number2; System.out.println(result); // prints 21 ) )

4. Java Bitwise papildināšanas operators

Bitu papildinājuma operators ir vienots operators (darbojas tikai ar vienu operandu). To apzīmē ar ~.

Tas maina bināros ciparus no 1 līdz 0 un 0 pret 1 .

Java Bitwise papildināšanas operators

It is important to note that the bitwise complement of any integer N is equal to - (N + 1). For example,

Consider an integer 35. As per the rule, the bitwise complement of 35 should be -(35 + 1) = -36. Now let's see if we get the correct answer or not.

 35 = 00100011 (In Binary) // using bitwise complement operator ~ 00100011 __________ 11011100

In the above example, we get that the bitwise complement of 00100011 (35) is 11011100. Here, if we convert the result into decimal we get 220.

However, it is important to note that we cannot directly convert the result into decimal and get the desired output. This is because the binary result 11011100 is also equivalent to -36.

To understand this we first need to calculate the binary output of -36.

2's Complement

In binary arithmetic, we can calculate the binary negative of an integer using 2's complement.

1's complement changes 0 to 1 and 1 to 0. And, if we add 1 to the result of the 1's complement, we get the 2's complement of the original number. For example,

 // compute the 2's complement of 36 36 = 00100100 (In Binary) 1's complement = 11011011 2's complement: 11011011 + 1 _________ 11011100

Šeit mēs varam redzēt, ka 2 papildinājums ar 36 (ti, -36 ) ir 11011100 . Šī vērtība ir ekvivalenta papildinājumam bitā 35 .

Tādējādi mēs varam teikt, ka 35 bitu papildinājums ir - (35 + 1) = -36 .

3. piemērs: papildinājums pa bitiem

 class Main ( public static void main(String() args) ( int number = 35, result; // bitwise complement of 35 result = ~number; System.out.println(result); // prints -36 ) )

Java maiņas operatori

Java ir trīs maiņu operatoru veidi:

  • Parakstīts kreisās maiņas taustiņš (<<)
  • Parakstīta labā maiņa (>>)
  • Neparakstīta labā maiņa (>>>)

5. Java kreisās maiņas operators

Kreisās maiņas operators pārvieto visus bitus uz kreiso pusi par noteiktu skaitu norādīto bitu. To apzīmē ar <<.

Java 1 bitu kreisās maiņas operators

As we can see from the image above, we have a 4-digit number. When we perform a 1 bit left shift operation on it, each individual bit is shifted to the left by 1 bit.

As a result, the left-most bit (most-significant) is discarded and the right-most position(least-significant) remains vacant. This vacancy is filled with 0s.

Example 5: Left Shift Operators

 class Main ( public static void main(String() args) ( int number = 2; // 2 bit left shift operation int result = number << 2; System.out.println(result); // prints 8 ) )

5. Java Signed Right Shift Operator

The signed right shift operator shifts all bits towards the right by a certain number of specified bits. It is denoted by >>.

When we shift any number to the right, the least significant bits (rightmost) are discarded and the most significant position (leftmost) is filled with the sign bit. For example,

 // right shift of 8 8 = 1000 (In Binary) // perform 2 bit right shift 8>> 2: 1000>> 2 = 0010 (equivalent to 2)

Šeit mēs veicam pareizo nobīdi 8 (ti, zīme ir pozitīva). Līdz ar to nav nekādu pazīmju. Tātad kreisākie biti ir aizpildīti ar 0 (apzīmē pozitīvu zīmi).

 // right shift of -8 8 = 1000 (In Binary) 1's complement = 0111 2's complement: 0111 + 1 _______ 1000 Signed bit = 1 // perform 2 bit right shift 8>> 2: 1000>> 2 = 1110 (equivalent to -2)

Šeit mēs izmantojām parakstīto bitu 1, lai aizpildītu kreisākos bitus.

6. piemērs: Parakstīts labās maiņas operators

 class Main ( public static void main(String() args) ( int number1 = 8; int number2 = -8; // 2 bit signed right shift System.out.println(number1>> 2); // prints 2 System.out.println(number2>> 2); // prints -2 ) )

7. Java neparakstītais labās maiņas operators

Java nodrošina arī neparakstītu labo maiņu. To apzīmē ar >>>.

Šeit vakanto kreiso vietu aizpilda ar 0, nevis zīmes bitu. Piemēram,

 // unsigned right shift of 8 8 = 1000 8>>> 2 = 0010 // unsigned right shift of -8 -8 = 1000 (see calculation above) -8>>> 2 = 0010

7. piemērs: neparakstīta labā maiņa

 class Main ( public static void main(String() args) ( int number1 = 8; int number2 = -8; // 2 bit signed right shift System.out.println(number1>>> 2); // prints 2 System.out.println(number2>>> 2); // prints 1073741822 ) )

Kā redzam, parakstītās un neparakstītās labās maiņas operators negatīvajiem bitiem atgriež atšķirīgus rezultātus. Lai uzzinātu vairāk, apmeklējiet sadaļu >> un >>> atšķirība.

Interesanti raksti...