Java Bitsel ve Kaydırma Operatörleri (Örneklerle)

Bu eğitimde, örnekler yardımıyla bitsel operatör ve Java'daki farklı kaydırma operatörü türleri hakkında bilgi edineceğiz.

Java'da bitsel operatörler, ayrı bit seviyesinde tamsayı verileri üzerinde işlemler gerçekleştirir. Burada, tamsayı veri içerir byte, short, int, ve longveri türlerini.

Java'da bit düzeyinde işlemler gerçekleştirmek için 7 operatör vardır.

Şebeke Açıklama
| Bit tabanlı VEYA
& Bitsel AND
^ Bitsel ÖZELVEYA
~ Bitsel Tamamlayıcı
<< Sol shift
>> İmzalı Sağ Vardiya
>>> İmzasız Sağa Kaydırma

1. Java Bitsel YA DA Operatörü

Bitsel OR |operatörü, işlenenlerden en az biri 1 ise 1 döndürür. Aksi takdirde, 0 döndürür.

Aşağıdaki doğruluk tablosu, bitsel OR operatörünün çalışmasını gösterir. A ve b, yalnızca ikili değerleri, yani 1 veya 0 alabilen iki işlenen olsun.

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

Yukarıdaki tablo, bitsel OR operatörü için "Doğruluk Tablosu" olarak bilinir.

12 ve 25 numaralı iki tam sayının bitsel OR işlemine bakalım.

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

Örnek 1: Bitsel VEYA

 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 Bitsel VE İşleci

Bitsel AND &operatörü, ancak ve ancak her iki işlenen de 1 ise 1 döndürür. Aksi takdirde, 0 döndürür.

Aşağıdaki tablo, bitsel AND operatörünün çalışmasını gösterir. A ve b, yalnızca ikili değerleri, yani 1 ve 0 alabilen iki işlenen olsun.

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

12 ve 25 numaralı iki tam sayının bitsel AND işlemine bir göz atalım.

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

Örnek 2: Bitsel 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 Operatörü

Bitsel XOR ^operatörü, yalnızca ve yalnızca işlenenlerden biri 1 ise 1 döndürür. Ancak, her iki işlenen de 0 ise veya her ikisi de 1 ise, sonuç 0 olur.

Aşağıdaki doğruluk tablosu, bitsel XOR operatörünün çalışmasını gösterir. A ve b, yalnızca ikili değerleri, yani 1 veya 0 alabilen iki işlenen olsun.

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

12 ve 25 numaralı iki tamsayının bit tabanlı XOR işlemine bakalım.

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

Örnek 4: Bitsel ÖZELVEYA

 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 Bitsel Tamamlayıcı Operatörü

Bitsel tümleme operatörü, tekli bir operatördür (yalnızca bir işlenenle çalışır). İle gösterilir ~.

Bu ikili rakamları değiştirir 1 ile 0 ve 0 ile 1 .

Java Bitwise Tamamlayıcı Operatörü

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

Burada, biz 2'ye tümleyenini görebilirsiniz 36 (yani -36 ) 'dir 11011100 . Bu değer, 35'in bitsel tamamlayıcısına eşdeğerdir .

Bu yüzden, bit cinsinden tamamlayıcı olduğunu söyleyebiliriz 35 olan (35 + 1) = -36 - .

Örnek 3: Bitsel Tamamlayıcı

 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 Operatörleri

Java'da üç tür vardiya operatörü vardır:

  • İmzalı Sol Shift (<<)
  • İmzalı Sağ Vardiya (>>)
  • İmzasız Sağa Kaydırma (>>>)

5. Java Sol Kaydırma Operatörü

Sol kaydırma operatörü, tüm bitleri belirli bir sayıda belirtilen bit kadar sola kaydırır. İle gösterilir <<.

Java 1 bit Sol Kaydırma Operatörü

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)

Burada, 8'in sağa kaymasını gerçekleştiriyoruz (yani işaret pozitiftir). Bu nedenle, işaret biti yoktur. Yani en soldaki bitler 0 ile doldurulur (pozitif işareti temsil eder).

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

Burada, en soldaki bitleri doldurmak için işaretli bit 1'i kullandık.

Örnek 6: İmzalı Sağ Vardiya Operatörü

 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 İmzasız Sağ Kaydırma İşleci

Java ayrıca imzasız bir sağa kayma sağlar. İle gösterilir >>>.

Burada, en soldaki boş pozisyon işaret biti yerine 0 ile doldurulur . Örneğin,

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

Örnek 7: İmzasız Sağa Kaydırma

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

Gördüğümüz gibi, işaretli ve işaretsiz sağa kaydırma operatörü negatif bitler için farklı sonuçlar verir. Daha fazla bilgi edinmek için >> ve >>> arasındaki farkı ziyaret edin.

Ilginç makaleler...