Selles õpetuses õpime näidete abil Java-tüüpi bitipõhist operaatorit ja erinevat tüüpi nihkeoperaatoreid.
Javas teostavad bitipõhised operaatorid täisarvu andmetega toiminguid üksikute bititasandil. Siin täisarv andmed sisaldavad byte
, short
, int
, ja long
mis tüüpi andmeid.
Java-s bititaseme toimingute tegemiseks on 7 operaatorit.
Operaator | Kirjeldus |
---|---|
| | Piki VÕI |
& | Pikkade kaupa JA |
^ | Piki XOR |
~ | Bitipõhine täiendus |
<< | Vasak Shift |
>> | Allkirjastatud parempoolne nihe |
>>> | Allkirjastamata parem nihe |
1. Java Bitwise VÕI operaator
Biti kaupa OR |
operaator tagastab 1, kui vähemalt üks operandidest on 1. Muul juhul tagastab 0.
Järgmine tõetabel näitab bitipõhise OR-operaatori tööd. Olgu a ja b kaks operandi, mis võivad võtta ainult binaarseid väärtusi, st 1 või 0.
a | b | a | b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Ülaltoodud tabelit nimetatakse bitipõhise VÕI operaatori tõetabeliks.
Vaatame kahe täisarvu 12 ja 25 bittide kaupa VÕI toimingut.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise OR Operation of 12 and 25 00001100 | 00011001 ____________ 00011101 = 29 (In Decimal)
Näide 1: bitipõhine VÕI
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 Operaator
Bittpidi AND &
operaator tagastab 1 siis ja ainult siis, kui mõlemad operandid on 1. Muul juhul tagastab ta 0.
Järgmine tabel näitab bitipõhise JA-operaatori tööd. Olgu a ja b kaks operandi, mis võivad võtta ainult binaarseid väärtusi, st 1 ja 0.
a | b | a & b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Heidame pilgu kahe täisarvu 12 ja 25 bitipõhisele toimingule.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) // Bitwise AND Operation of 12 and 25 00001100 & 00011001 ____________ 00001000 = 8 (In Decimal)
Näide 2: bitipõhiselt JA
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 operaator
Bittide kaupa XOR ^
tagastab operaatori 1 juhul ja ainult siis, kui üks operandidest on 1. Kui mõlemad operandid on 0 või kui mõlemad on 1, on tulemus 0.
Järgmine tõttabel näitab bitipõhise XOR-operaatori tööd. Olgu a ja b kaks operandi, mis võivad võtta ainult binaarseid väärtusi, st 1 või 0.
a | b | a & b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Vaatame kahe täisarvu 12 ja 25 bitipõhist XOR-toimingut.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) // Bitwise XOR Operation of 12 and 25 00001100 00011001 ____________ 00010101 = 21 (In Decimal)
Näide 4: bitipõhine XOR
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 bittide täiendusoperaator
Biti kaupa täiendav operaator on unaaroperaator (töötab ainult ühe operandiga). Seda tähistatakse ~
.
See muudab kahendarvud 1 kuni 0 ja 0 kuni 1 .

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
Siin näeme, et 2 komplemendiks 36 (st -36 ) on 11011100 . See väärtus on samaväärne biti kaupa täiendiga 35 .
Seega võime öelda, et 35 bittide täiend on - (35 + 1) = -36 .
Näide 3: täiendus bitipõhiselt
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 vahetuse operaatorid
Java-s on kolme tüüpi vahetuseoperaatoreid:
- Allkirjastatud vasakpoolne nihe (<<)
- Parempoolne nihe allkirjastatud (>>)
- Parempoolne nihe allkirjastamata (>>>)
5. Java vasakpoolse vahetuse operaator
Vasaku vahetuse operaator nihutab kõiki bitte kindla arvu määratud bitide võrra vasakule. Seda tähistatakse <<
.

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)
Siin teostame parempoolse nihke 8 (st märk on positiivne). Seega pole märki natuke. Nii et kõige vasakpoolsemad bitid täidetakse 0-ga (tähistab positiivset märki).
// 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)
Siin oleme vasakpoolsete bitide täitmiseks kasutanud allkirjastatud bitti 1 .
Näide 6: Parempoolse vahetuse operaator
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 allkirjastamata parempoolse vahetuse operaator
Java pakub ka allkirjastamata parempoolset nihet. Seda tähistatakse >>>
.
Siin täidetakse vasakpoolne vaba positsioon märgibiti asemel 0-ga . Näiteks,
// unsigned right shift of 8 8 = 1000 8>>> 2 = 0010 // unsigned right shift of -8 -8 = 1000 (see calculation above) -8>>> 2 = 0010
Näide 7: allkirjastamata parem nihe
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 ) )
Nagu näeme, annab allkirjastatud ja allkirjastamata parempoolse nihke operaator negatiivsete bitide jaoks erinevad tulemused. Lisateabe saamiseks külastage jaotist >> ja >>> erinevus.