Java bitni in premični operaterji (s primeri)

V tej vadnici bomo s pomočjo primerov spoznali bitni operator in različne vrste operaterjev premikov v Javi.

V Javi bitni operaterji izvajajo operacije s celoštevilčnimi podatki na posamezni bitni ravni. Tukaj, podatki celo vključuje byte, short, int, in longvrste podatkov.

Za izvajanje operacij na bitni ravni v Javi deluje 7 operaterjev.

Operater Opis
| Bitovno ALI
& Bitovno AND
^ Bitno XOR
~ Bitno dopolnilo
<< Levi shift
>> Podpisan desni premik
>>> Nepodpisan desni premik

1. Java bitni ALI operater

Bitni |operater OR vrne 1, če je vsaj eden od operandov 1. V nasprotnem primeru vrne 0.

Naslednja tabela resnic prikazuje delovanje bitnega operaterja OR. Naj bosta a in b dva operanda, ki lahko sprejmeta samo binarne vrednosti, tj. 1 ali 0.

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

Zgornja tabela je znana kot "tabela resnice" za bitni operater ALI.

Oglejmo si bitno ALI operacijo dveh celih števil 12 in 25.

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

Primer 1: Bitno ALI

 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 bitni in operater

Bitni &operator AND vrne 1, če in samo, če sta oba operanda enaka 1. V nasprotnem primeru vrne 0.

Naslednja tabela prikazuje delovanje bitnega operatorja AND. Naj bosta a in b dva operanda, ki lahko sprejmeta samo binarne vrednosti, tj. 1 in 0.

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

Oglejmo si bitno AND delovanje dveh celih števil 12 in 25.

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

Primer 2: Bitno AND

  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 Operator

Bitni ^operater XOR vrne 1, če in le, če je eden od operandov 1. Če pa sta oba operanda 0 ali če sta oba 1, potem je rezultat 0.

Naslednja tabela resnic prikazuje delovanje bitnega operaterja XOR. Naj bosta a in b dva operanda, ki lahko sprejmeta samo binarne vrednosti, tj. 1 ali 0.

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

Oglejmo si bitno XOR operacijo dveh celih števil 12 in 25.

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

Primer 4: Bitni 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. Operater Java-bitnega komplementa

Operator bitnega komplementa je unarni operator (deluje samo z enim operandom). Označuje se z ~.

Spremeni binarne številke 1 na 0 in 0 na 1 .

Java bitni operater dopolnitve

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

Tu lahko vidimo, da je dopolnilo 36 z 2 (tj. -36 ) 11011100 . Ta vrednost je enakovredna bitnemu dopolnitvi 35 .

Zato lahko rečemo, da je bitni dopolnilo 35 je - (35 + 1) = -36 .

Primer 3: Bitno dopolnilo

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

Operaterji Java Shift

V Javi obstajajo tri vrste operaterjev premikov:

  • Left Shift s podpisom (<<)
  • Podpisan desni premik (>>)
  • Nepodpisani desni premik (>>>)

5. Java Left Shift Operator

Levi operater premika premakne vse bite proti levi za določeno število določenih bitov. Označuje se z <<.

Java 1-bitni operater levega premika

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)

Tu izvajamo pravi premik 8 (tj. Znak je pozitiven). Zato ni nobenega znaka. Torej so levi bitji napolnjeni z 0 (predstavlja pozitiven znak).

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

Tu smo uporabili podpisani bit 1 za zapolnitev skrajnih levih bitov.

Primer 6: Podpisan operater desne premike

 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. Nepodpisani operater desnega premika Java

Java omogoča tudi nepodpisan desni premik. Označuje se z >>>.

Tu je prazno mesto levo zapolnjeno z 0 namesto z znakovnim bitom. Na primer

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

Primer 7: Nepodpisani desni premik

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

Kot lahko vidimo, podpisani in nepodpisani operater desnega premika vrne različne rezultate za negativne bite. Če želite izvedeti več, obiščite Razlika med >> in >>>.

Zanimive Članki...