Bitové operátory Java a operátory posunu (s příklady)

V tomto tutoriálu se pomocí příkladů dozvíme o bitovém operátoru a různých typech operátorů posunu v Javě.

V Javě provádějí bitové operátory operace s celočíselnými daty na jednotlivé bitové úrovni. Zde se data celé číslo zahrnuje byte, short, inta longtypy dat.

Existuje 7 operátorů, kteří provádějí operace na bitové úrovni v Javě.

Operátor Popis
| Bitové NEBO
& Bitové AND
^ Bitový XOR
~ Bitový doplněk
<< Levý Shift
>> Podepsaný pravý posun
>>> Nepodepsaný pravý posun

1. Java bitový operátor OR

Bitový |operátor OR vrátí 1, pokud je alespoň jeden z operandů 1. V opačném případě vrátí 0.

Následující tabulka pravdivosti ukazuje fungování bitového operátoru OR. Nechť a a b jsou dva operandy, které mohou nabývat pouze binární hodnoty, tj. 1 nebo 0.

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

Výše uvedená tabulka je známá jako „tabulka pravdy“ pro bitový operátor OR.

Podívejme se na bitovou operaci OR dvou celých čísel 12 a 25.

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

Příklad 1: Bitový součet NEBO

 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 bitový operátor AND

Bitový &operátor AND vrací 1 právě tehdy, jsou-li oba operandy 1. V opačném případě vrátí 0.

Následující tabulka ukazuje fungování bitového operátoru AND. Nechť a a b jsou dva operandy, které mohou nabývat pouze binární hodnoty, tj. 1 a 0.

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

Pojďme se podívat na bitovou operaci AND dvou celých čísel 12 a 25.

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

Příklad 2: Bitové 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. Operátor Java Bitwise XOR

Bitový ^operátor XOR vrací 1 právě tehdy, když je jeden z operandů 1. Pokud jsou však oba operandy 0 nebo pokud jsou oba 1, pak je výsledek 0.

Následující tabulka pravdy demonstruje fungování bitového operátoru XOR. Nechť a a b jsou dva operandy, které mohou nabývat pouze binární hodnoty, tj. 1 nebo 0.

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

Podívejme se na bitovou operaci XOR dvou celých čísel 12 a 25.

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

Příklad 4: Bitový 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. Operátor Java Bitwise Complement

Operátor bitového doplňku je unární operátor (pracuje pouze s jedním operandem). Označuje to ~.

Mění binární číslice 10 a 01 .

Operátor bitového doplňku Java

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

Zde vidíme doplněk 2 36 (tj. -36 ) je 11011100 . Tato hodnota odpovídá bitovému doplňku 35 .

Můžeme tedy říci, že bitový doplněk 35 je - (35 + 1) = -36 .

Příklad 3: Bitový doplněk

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

Operátoři posunu Java

V Javě existují tři typy operátorů směn:

  • Podepsaný levý posun (<<)
  • Podepsaný pravý posun (>>)
  • Nepodepsaný pravý posun (>>>)

5. Operátor levého posunu Java

Operátor posunu doleva posune všechny bity směrem doleva o určitý počet zadaných bitů. Označuje to <<.

Operátor levého posunu 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)

Zde provádíme správný posun 8 (tj. Znaménko je kladné). Z tohoto důvodu není žádný znak. Bity zcela vlevo jsou tedy vyplněny 0 (představuje kladné znaménko).

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

Zde jsme použili bit 1 se znaménkem k vyplnění bitů nejvíce vlevo.

Příklad 6: Podepsaný operátor pravého posunu

 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 také poskytuje nepodepsaný pravý posun. Označuje to >>>.

Zde je prázdná pozice zcela vlevo vyplněna 0 místo znaménkového bitu. Například,

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

Příklad 7: Nepodepsaný pravý posun

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

Jak vidíme, operátor posunu se znaménkem a bez znaménka vrací různé výsledky pro záporné bity. Další informace najdete v části Rozdíl mezi >> a >>>.

Zajímavé články...