Java Bitwise και Shift τελεστές (με παραδείγματα)

Σε αυτό το σεμινάριο, θα μάθουμε για τον τελεστή bitwise και τους διαφορετικούς τύπους τελεστών shift στην Java με τη βοήθεια παραδειγμάτων.

Στην Java, οι τελεστές bitwise εκτελούν λειτουργίες σε ακέραια δεδομένα σε κάθε επίπεδο bit. Εδώ, ο ακέραιος δεδομένων περιλαμβάνει byte, short, int, και longτύπους δεδομένων.

Υπάρχουν 7 τελεστές που εκτελούν λειτουργίες σε επίπεδο bit στην Java.

Χειριστής Περιγραφή
| Bitwise Ή
& Bitwise ΚΑΙ
^ Bitwise XOR
~ Συμπλήρωμα Bitwise
<< Αριστερή μετατόπιση
>> Υπογραφή δεξιάς αλλαγής
>>> Μη υπογεγραμμένη δεξιά μετατόπιση

1. Java Bitwise Ή χειριστής

Ο |χειριστής bitwise OR επιστρέφει 1 εάν τουλάχιστον ένας από τους τελεστές είναι 1. Διαφορετικά, επιστρέφει 0.

Ο παρακάτω πίνακας αλήθειας δείχνει τη λειτουργία του χειριστή bitwise OR. Αφήστε τα a και b να είναι δύο τελεστές που μπορούν να πάρουν μόνο δυαδικές τιμές, δηλαδή 1 ή 0.

ένα σι α | σι
0 0 0
0 1 1
1 0 1
1 1 1

Ο παραπάνω πίνακας είναι γνωστός ως "Πίνακας αλήθειας" για το χειριστή bitwise OR.

Ας δούμε τη λειτουργία bitwise OR δύο ακέραιων αριθμών 12 και 25.

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

Παράδειγμα 1: Bitwise Ή

 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

Ο τελεφερίκ AND &επιστρέφει 1 εάν και μόνο αν και οι δύο τελεστές είναι 1. Διαφορετικά, επιστρέφει 0.

Ο παρακάτω πίνακας δείχνει τη λειτουργία του τελεστή AND. Αφήστε τα a και b να είναι δύο τελεστές που μπορούν να πάρουν μόνο δυαδικές τιμές, δηλαδή 1 και 0.

ένα σι α & β
0 0 0
0 1 0
1 0 0
1 1 1

Ας ρίξουμε μια ματιά στη λειτουργία bitwise AND δύο ακέραιων αριθμών 12 και 25.

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

Παράδειγμα 2: Bitwise 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

Ο ^τελεστής XOR επιστρέφει 1 εάν και μόνο αν ένας από τους τελεστές είναι 1. Ωστόσο, εάν και οι δύο τελεστές είναι 0 ή και οι δύο είναι 1, τότε το αποτέλεσμα είναι 0.

Ο παρακάτω πίνακας αλήθειας δείχνει τη λειτουργία του τελεστή XOR. Αφήστε τα a και b να είναι δύο τελεστές που μπορούν να πάρουν μόνο δυαδικές τιμές, δηλαδή 1 ή 0.

ένα σι α & β
0 0 0
0 1 1
1 0 1
1 1 0

Ας δούμε τη δυαδική λειτουργία XOR των δύο ακέραιων αριθμών 12 και 25.

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

Παράδειγμα 4: Bitwise 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

Ο τελεστής συμπληρώματος bitwise είναι ένας unary τελεστής (λειτουργεί με έναν μόνο τελεστή). Δηλώνεται με ~.

Αλλάζει δυαδικά ψηφία 1 σε 0 και 0 σε 1 .

Java Bitwise Complement Operator

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

Εδώ, μπορούμε να δούμε το συμπλήρωμα 2 του 36 (δηλαδή -36 ) είναι 11011100 . Αυτή η τιμή είναι ισοδύναμη με το συμπλήρωμα bitwise του 35 .

Ως εκ τούτου, μπορούμε να πούμε ότι το δεξιόστροφο συμπλήρωμα του 35 είναι - (35 + 1) = -36 .

Παράδειγμα 3: Συμπλήρωμα Bitwise

 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

Υπάρχουν τρεις τύποι τελεστών shift στην Java:

  • Υπογεγραμμένη αριστερή μετατόπιση (<<)
  • Υπογραφή δεξιάς αλλαγής (>>)
  • Μη υπογεγραμμένη δεξιά μετατόπιση (>>>)

5. Χειριστής Java Left Shift

Ο χειριστής της αριστερής αλλαγής μετατοπίζει όλα τα δυφία προς τα αριστερά από έναν ορισμένο αριθμό καθορισμένων δυφίων. Δηλώνεται με <<.

Java 1 bit Left Shift Operator

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)

Εδώ, κάνουμε τη σωστή μετατόπιση του 8 (δηλ. Το σύμβολο είναι θετικό). Ως εκ τούτου, δεν υπάρχει κανένα σημάδι. Έτσι, τα αριστερότερα bit γεμίζουν με 0 (αντιπροσωπεύει θετικό σημάδι).

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

Εδώ, χρησιμοποιήσαμε το υπογεγραμμένο bit 1 για να γεμίσουμε τα αριστερότερα bit.

Παράδειγμα 6: Υπογεγραμμένος χειριστής Δεξιάς Μετατόπισης

 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 Unsigned Right Shift Operator

Η Java παρέχει επίσης μια μη υπογεγραμμένη δεξιά αλλαγή. Δηλώνεται με >>>.

Εδώ, η κενή αριστερή θέση γεμίζει με 0 αντί για το bit. Για παράδειγμα,

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

Παράδειγμα 7: Μη υπογεγραμμένη δεξιά μετατόπιση

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

Όπως μπορούμε να δούμε ο υπογεγραμμένος και μη υπογεγραμμένος χειριστής δεξιάς αλλαγής επιστρέφει διαφορετικά αποτελέσματα για αρνητικά ψηφία. Για να μάθετε περισσότερα επισκεφτείτε τη Διαφορά μεταξύ >> και >>>.

ενδιαφέροντα άρθρα...