Tässä opetusohjelmassa opitaan esimerkkejä bittikohtaisesta operaattorista ja erityyppisistä Java-operaattoreista.
Java-käyttöjärjestelmässä bittioperaattorit suorittavat operaatioita kokonaislukutiedoilla yksittäisillä bittitasoilla. Tässä kokonaisluku data sisältää byte
, short
, int
, ja long
tietotyyppejä.
Bittitason operaatioita Java-käyttöjärjestelmässä on 7 operaattoria.
Operaattori | Kuvaus |
---|---|
| | Bittiä TAI |
& | Bittikohtaisesti JA |
^ | Bitteittäin XOR |
~ | Bittikohtainen täydennys |
<< | Vasen vaihtonäppäin |
>> | Allekirjoitettu oikea vaihto |
>>> | Allekirjoittamaton oikea vaihto |
1. Java Bitwise OR -käyttäjä
Bittikohtainen OR- |
operaattori palauttaa arvon 1, jos ainakin yksi operandeista on 1. Muussa tapauksessa se palauttaa arvon 0.
Seuraava totuustaulukko osoittaa bitti-OR-operaattorin toiminnan. Olkoon a ja b kaksi operandia, jotka voivat ottaa vain binääriarvot eli 1 tai 0.
a | b | a | b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Yllä olevaa taulukkoa kutsutaan "totuustaulukoksi" bittiä TAI-operaattorille.
Tarkastellaan kahden kokonaisluvun 12 ja 25 bittiä TAI-operaatiota.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise OR Operation of 12 and 25 00001100 | 00011001 ____________ 00011101 = 29 (In Decimal)
Esimerkki 1: bittiä kohti OR
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
Bittikohtainen AND- &
operaattori palauttaa arvon 1 ja vain, jos molemmat operandit ovat 1. Muussa tapauksessa se palauttaa arvon 0.
Seuraava taulukko havainnollistaa bitti- ja operaattorin toimintaa. Olkoon a ja b kaksi operandia, jotka voivat ottaa vain binääriarvot eli 1 ja 0.
a | b | a & b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Katsotaanpa kahden kokonaisluvun 12 ja 25 bittikohtainen toiminta.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) // Bitwise AND Operation of 12 and 25 00001100 & 00011001 ____________ 00001000 = 8 (In Decimal)
Esimerkki 2: Bittiviiva 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 -käyttäjä
Bittikohtainen XOR- ^
operaattori palauttaa arvon 1 ja vain, jos toinen operandeista on 1. Kuitenkin, jos molemmat operandit ovat 0 tai jos molemmat ovat 1, tulos on 0.
Seuraava totuustaulukko osoittaa bitti XOR -operaattorin toiminnan. Olkoon a ja b kaksi operandia, jotka voivat ottaa vain binääriarvot eli 1 tai 0.
a | b | a & b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Tarkastellaan kahden kokonaisluvun 12 ja 25 bittikohtaista XOR-operaatiota.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) // Bitwise XOR Operation of 12 and 25 00001100 00011001 ____________ 00010101 = 21 (In Decimal)
Esimerkki 4: Bittikohtainen 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 Bitwise Complement Operator
Bittikohtainen komplementtioperaattori on unaarinen operaattori (toimii vain yhden operandin kanssa). Se on merkitty ~
.
Se muuttaa binaariset numerot 1: stä 0: een ja 0: sta 1: een .

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
Tässä voimme nähdä, että 36: n (eli -36 ) 2: n komplementti on 11011100 . Tämä arvo vastaa 35: n bittiä täydentävää arvoa .
Siksi voimme sanoa, että 35: n bittikomplementti on - (35 + 1) = -36 .
Esimerkki 3: Bittikohtainen täydennys
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-vaihto-operaattorit
Java-ohjelmissa on kolmenlaisia vuoro-operaattoreita:
- Allekirjoitettu vasen vaihto (<<)
- Allekirjoitettu oikea vaihto (>>)
- Allekirjoittamaton oikea vaihto (>>>)
5. Java Vasemmanvaihdon operaattori
Vasemman vaiheen operaattori siirtää kaikki bitit vasemmalle tietyllä määrällä määritettyjä bittejä. Se on merkitty <<
.

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)
Tässä teemme oikean siirtymän 8 (ts. Merkki on positiivinen). Siksi ei ole merkkiä bittiä. Joten vasemmanpuoleisimmat bitit täytetään 0: lla (edustaa positiivista merkkiä).
// 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)
Tässä olemme käyttäneet allekirjoitettua bittiä 1 vasemmanpuoleisten bittien täyttämiseen.
Esimerkki 6: Allekirjoitettu oikean vaiheen operaattori
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-allekirjoittamaton oikeanvaihdon operaattori
Java tarjoaa myös allekirjoittamattoman oikean siirtymän. Se on merkitty >>>
.
Tyhjä vasemmalla oleva paikka täytetään tässä merkkibitin sijaan 0: lla. Esimerkiksi,
// unsigned right shift of 8 8 = 1000 8>>> 2 = 0010 // unsigned right shift of -8 -8 = 1000 (see calculation above) -8>>> 2 = 0010
Esimerkki 7: Allekirjoittamaton oikea siirtymä
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 ) )
Kuten voimme nähdä, allekirjoitettu ja allekirjoittamaton oikean siirtymän operaattori palauttaa negatiivisten bittien tulokset eri tavalla. Lisätietoja on kohdassa >> ja >>>.