„Java Bitwise“ ir „Shift“ operatoriai (su pavyzdžiais)

Šioje pamokoje su pavyzdžių pagalba sužinosime apie „Java“ operacinę sistemą bitais ir įvairius „Java“ poslinkio operatorius.

„Java“ operacijose bitų pavidalu operacijos su sveikaisiais skaičiais atliekamos individualiai bitų lygiu. Čia sveikasis duomenys apima byte, short, intir longtipų duomenis.

„Java“ bitų lygio operacijoms atlikti yra 7 operatoriai.

operatorius apibūdinimas
| ARBA bitais
& Bitais IR
^ Bitais XOR
~ Bitų papildymas
<< Kairysis „Shift“
>> Pasirašė dešiniąją pamainą
>>> Nepasirašytas dešinysis poslinkis

1. „Java Bitwise“ ARBA Operatorius

|Operatorius bitais ARBA grąžina 1, jei bent vienas iš operandų yra 1. Priešingu atveju jis grąžina 0.

Ši tiesų lentelė parodo bitų ARBA operatoriaus veikimą. Tegul a ir b yra du operandai, galintys gauti tik dvejetaines reikšmes, ty 1 arba 0.

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

Aukščiau pateikta lentelė yra žinoma kaip „bitų OR“ operatoriaus „tiesos lentelė“.

Pažvelkime į dviejų sveikų skaičių 12 ir 25 bitų ARBA operaciją.

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

1 pavyzdys

 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 AND Operator“

&Operatorius bitais IR grąžina 1, jei abu operandai yra 1. Priešingu atveju jis grąžina 0.

Ši lentelė parodo bitų IR operatoriaus veikimą. Tegul a ir b yra du operandai, galintys gauti tik dvejetaines reikšmes, ty 1 ir 0.

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

Pažvelkime į dviejų sveikųjų skaičių 12 ir 25 bitų IR operaciją.

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

2 pavyzdys

  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“ operatorius

^Operatorius bitais XOR grąžina 1 tik tada, kai vienas iš operandų yra 1. Tačiau, jei abu operandai yra 0 arba jei abu yra 1, rezultatas yra 0.

Ši tiesų lentelė parodo bitų XOR operatoriaus darbą. Tegul a ir b yra du operandai, galintys gauti tik dvejetaines reikšmes, ty 1 arba 0.

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

Pažvelkime į dviejų sveikų skaičių 12 ir 25 bitų XOR operaciją.

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

4 pavyzdys: XOR bitais

 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 Complement Operator“

Bitų komplemento operatorius yra vienarūšis operatorius (veikia tik su vienu operandu). Tai žymima ~.

Ji pakeičia dvejetainius skaitmenis nuo 1 iki 0 ir nuo 0 iki 1 .

„Java Bitwise“ papildymo operatorius

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

Čia galime pamatyti, kad 2 papildymas 36 (ty -36 ) yra 11011100 . Ši reikšmė yra lygi 35 papildymui bitais .

Taigi galime sakyti, kad 35 bitų papildymas yra - (35 + 1) = -36 .

3 pavyzdys: papildymas bitais

 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 Shift“ operatoriai

„Java“ yra trijų tipų pamainų operatoriai:

  • Pasirašytas kairysis poslinkis (<<)
  • Pasirašyta dešinė pamaina (>>)
  • Nepasirašytas dešinysis poslinkis (>>>)

5. „Java“ kairiojo poslinkio operatorius

Kairiojo poslinkio operatorius perkelia visus bitus į kairę tam tikru nurodytų bitų skaičiumi. Tai žymima <<.

„Java“ 1 bitų kairiojo poslinkio operatorius

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)

Čia mes atliekame dešinįjį 8 poslinkį (ty ženklas yra teigiamas). Vadinasi, nėra nė ženklo. Kairiausi bitai užpildomi 0 (reiškia teigiamą ženklą).

 // 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)

Čia mes naudojome pasirašytą 1 bitą, kad užpildytume kairiausius bitus.

6 pavyzdys: Pasirašytas dešinės pavaros operatorius

 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“ nepasirašytas dešiniojo poslinkio operatorius

„Java“ taip pat suteikia nepasirašytą dešinę. Tai žymima >>>.

Čia laisva kairė pozicija vietoj ženklo bitų užpildoma 0 . Pavyzdžiui,

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

7 pavyzdys: nepasirašytas dešinysis poslinkis

 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 ) )

Kaip matome, pasirašytas ir nepasirašytas dešiniojo poslinkio operatorius pateikia skirtingus neigiamų bitų rezultatus. Norėdami sužinoti daugiau, apsilankykite skirtumuose tarp >> ir >>>.

Įdomios straipsniai...