Operatori Java Bitwise e Shift (con esempi)

In questo tutorial, impareremo l'operatore bit per bit e diversi tipi di operatori di spostamento in Java con l'aiuto di esempi.

In Java, gli operatori bit per bit eseguono operazioni su dati interi a livello di singolo bit. Qui, i dati integer comprende byte, short, int, e longtipi di dati.

Esistono 7 operatori per eseguire operazioni a livello di bit in Java.

Operatore Descrizione
| OR bit per bit
& Bitwise AND
^ Bitwise XOR
~ Bitwise Complement
<< Tasto maiuscolo di sinistra
>> Firmato Right Shift
>>> Maiusc destro senza segno

1. Operatore OR bit per bit Java

L' |operatore OR bit per bit restituisce 1 se almeno uno degli operandi è 1. In caso contrario, restituisce 0.

La seguente tabella di verità mostra il funzionamento dell'operatore OR bit per bit. Siano aeb due operandi che possono assumere solo valori binari, cioè 1 o 0.

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

La tabella sopra è nota come "tabella della verità" per l'operatore OR bit per bit.

Diamo un'occhiata all'operazione OR bit per bit di due interi 12 e 25.

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

Esempio 1: OR bit per bit

 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. Operatore AND bit per bit Java

L' &operatore AND bit per bit restituisce 1 se e solo se entrambi gli operandi sono 1. In caso contrario, restituisce 0.

La tabella seguente mostra il funzionamento dell'operatore AND bit per bit. Siano aeb due operandi che possono assumere solo valori binari, ad esempio 1 e 0.

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

Diamo un'occhiata all'operazione AND bit per bit di due interi 12 e 25.

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

Esempio 2: AND bit per bit

  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. Operatore Java Bitwise XOR

L' ^operatore XOR bit per bit restituisce 1 se e solo se uno degli operandi è 1. Tuttavia, se entrambi gli operandi sono 0 o se entrambi sono 1, il risultato è 0.

La seguente tabella di verità mostra il funzionamento dell'operatore XOR bit per bit. Siano aeb due operandi che possono assumere solo valori binari, cioè 1 o 0.

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

Diamo un'occhiata all'operazione XOR bit per bit di due interi 12 e 25.

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

Esempio 4: XOR bit per bit

 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. Operatore Java Bitwise Complement

L'operatore di complemento bit per bit è un operatore unario (funziona con un solo operando). È indicato da ~.

Cambia le cifre binarie da 1 a 0 e da 0 a 1 .

Operatore Java Bitwise Complement

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

Qui, possiamo vedere che il complemento a 2 di 36 (cioè -36 ) è 11011100 . Questo valore è equivalente al complemento bit per bit di 35 .

Quindi, possiamo dire che il complemento bit per bit di 35 è - (35 + 1) = -36 .

Esempio 3: Bitwise Complement

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

Operatori Java Shift

Esistono tre tipi di operatori di spostamento in Java:

  • Signed Left Shift (<<)
  • Shift destro firmato (>>)
  • Maiusc destro senza segno (>>>)

5. Operatore Java Left Shift

L'operatore di spostamento a sinistra sposta tutti i bit verso sinistra di un certo numero di bit specificato. È indicato da <<.

Operatore di spostamento a sinistra di Java 1 bit

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)

Qui, stiamo eseguendo lo spostamento a destra di 8 (cioè il segno è positivo). Quindi, non ci sono bit di segno. Quindi i bit più a sinistra vengono riempiti con 0 (rappresenta il segno positivo).

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

Qui abbiamo usato il bit 1 con segno per riempire i bit più a sinistra.

Esempio 6: Operatore di spostamento a destra firmato

 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. Operatore di spostamento a destra senza segno Java

Java fornisce anche uno spostamento a destra non firmato. È indicato da >>>.

Qui, la posizione più a sinistra vuota viene riempita con 0 invece del bit di segno. Per esempio,

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

Esempio 7: spostamento a destra senza segno

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

Come possiamo vedere, l'operatore di spostamento a destra con segno e senza segno restituisce risultati diversi per bit negativi. Per saperne di più visita la Differenza tra >> e >>>.

Articoli interessanti...