Java. Logical operations. Operations of relation. Bitwise logical operations




Logical operations. Operations of relation. Bitwise logical operations


Contents


Search other websites:

1. What logical operations are used in Java programs?

In Java, in addition to logical operations, operations of relations and bitwise logical operations, there are also the following groups of operations:

  • arithmetic operations;
  • operations for special cases.

Logical operations are performed only on operands of type boolean (unlike the bit-wise logical operations).

Description of the following logical operations.

Operation  Description
-----------------------------------
   &       Logical operation "AND"
   |       Logical operation "OR"
   ^       Logical operation "XOR"
   ||      Shortened logical operation "OR"
   &&      Shortened logical operation "AND"
   !       The unary logical operation "NOT"
   &=      Logical operation "AND" with assignment
   |=      Logical operation "OR" with assignment
   ^=       The logical operation "XOR" with assignment
   ==      Equally
   !=      Not equal
   ?:      Ternary logical operation "if ... then ... else"

2. What result is obtained by using logical operations  !, &, |, ^

The results of the bitwise logical operations !, &, |, ^:

----------------------------------------
  X      Y     X|Y    X&Y    X^Y    !X
----------------------------------------
false  false  false  false  false  true
true   false  true   false  true   false
false  true   true   false  true   true
true   true   true   true   false  false
----------------------------------------

3. Examples of the use of of logical operations ~, &, |, ^
// logical operations
boolean b1, b2, b3;

b1 = true;
b2 = false;

b3 = b1 & b2; // b3 = false
b3 = !b1;     // b3 = false
b3 = b1 | b2; // b3 = true
b3 = b1 ^ b2; // b3 = true

4. What are the advantages of shortened logical operations && и ||

Short logical operations && and || allow to reduce the computation time of the long logical expressions.

When you use the && operation, if the value of the first operand is false, there is no need to compute the second operand, because the result will still be false.

Similarly, when using the || operation, if the value of the first operand is true, the result of the entire logical expression will be true. It means, there is no need to calculate the second operand.

Since, in operations is evaluated only one (first) operand then is increased the efficiency of the code.

Shortened logical operations can be effectively used in the code. For example, if the necessity of calculating of second operand depends on the value of the first operand. In this case, the second operand is not calculated.

5. What are the operations of relations are used in Java?

Operations of relations also are called comparison operations.

Operations of relations allow you to compare two operands together.

Result of the operation of relations is a logical value (boolean) true or false.

The most common operations of relations  are used in control operators of the conditional jump ‘if‘ and  loop operators.

Java supports the following relational operations:

Operation    Description
-------------------------------
   ==        Equally
   !=        Not equal
   >         More
   <         Less
   >=        More or equal
   <=        Less or equal

6. Examples of the use of relational operations

When comparing two values, we get a result of type boolean, which can take two values true or false.

Example 1.

// demonstration of relations operations
int x, y;
boolean b;

x = 8;
y = -5;
b = x>y; // b = true, operation '>' more
b = x<(y+3); // b = false, operation '<' less
b = x == 3-y; // b = true, operation '==' equal
b = x != 0;  // b = true, operation '!=' is not equal
b = x >= 8;  // b = true, operation '>=' more or equal
b = x+y <= 2; // b = false, operation '<=' less or equal

Example 2. Fragment of decision of the quadratic equation, which shows the use of relational operators in the conditional branch statement ‘if‘.

// fragment of decision of the quadratic equation
int a, b, c;
double d, x1, x2;

// input of values a, b, c
// ...

d = b*b - 4*a*c;

if (d>=0)
{
    if (a!=0)
    {
        x1 = (-b - Math.sqrt(d))/(2*a);
        x2 = (-b + Math.sqrt(d))/(2*a);
        System.out.println("x1 = " + x1);
        System.out.println("x2 = " + x2);
    }
    else
    {
        x1 = x2 = (double)-c / (double)b;
        System.out.println("x1 = x2 = " + x1);
    }
}
else
{
    System.out.println("The equation has no solution ");
}

7. What are the features of using of bitwise logical operations in Java?

Bitwise operations can only be performed on integer operands. These operations act on the individual binary operands.

Java supports the following bitwise logical operations:

Operation      Description
-----------------------------------------------------
   ~          Bitwise unary operation "NOT"
   &          Bitwise unary operation "AND"
   |          Bitwise unary operation "OR"
   ^          Bitwise logic operation "XOR"
   >>         Right shift
   >>>        Right shift with zero-fill
   <<         Left shift
   &=         Bitwise logical "AND" with assignment
   |=         Bitwise logical "OR" with assignment
   ^=           Bitwise logical "XOR" with assignment
   >>=        Right shift with assignment
   >>>=       Right shift with zero-fill and with assignment
   <<=        Left shift with assignment
-----------------------------------------------------

8. What is the result is obtained when is used the bitwise logic operations ~, &, |, ^

The results of use of bitwise logical operations ~, &, |, ^

--------------------------------------
  X    Y    X|Y    X&Y    X^Y    ~X
--------------------------------------
  0    0     0      0      0      1
  1    0     1      0      1      0
  0    1     1      0      1      1
  1    1     1      1      0      0
--------------------------------------


9. Using of bitwise logical operation ~ (NOT). Example

Bitwise logic operation ~ is the unary. With its use of all the bits are inverted.

Example.

int a, b;

a = 65;
b = ~a; // b = -66

a = -66;
b = ~a; // b = 65

10. Using bitwise logical operation & (AND). Example

Logical “AND” yet known as the logical multiplication.

Table of results for the two bit values has the form:

0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1

In accordance with this table occurs bitwise logical “AND” of the operands integers.

For example. For the two numbers 25 and 18 bit-wise “AND” gives the following result

00011001    = 25
00010010    = 18
----------------
00010000    = 16

Example for the bit-wise “AND” in the program for Java:

// Bitwise logical "AND"
byte a, b, c;

a = 25;
b = 18;
c = (byte)(a & b);  // c = 16

a = -20;
b = 10;
c = (byte)(a & b);  // c = 8

11. Using the bitwise logical “OR“. Example

The operation of bitwise logical “OR” is a binary (requires two operands).

Bitwise logical “OR” is denoted |. Logical “OR” is also called a logical addition. When doing this, the value is generated, which is the result of a bitwise logical “OR” corresponding bits of each operand.

Operation ‘ | ‘ for two bits gives the result:

0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1

For example. For numbers 25 and 18 bit-wise “OR” gives the following result:

00011001    = 25
00010010    = 18
----------------
00011011    = 27

Example of using the bit-wise “OR“:

// bit-wise logical "OR"
byte a, b, c;

a = 25;
b = 18;
c = (byte)(a | b);  // c = 27

a = -20;
b = 10;
c = (byte)(a | b);  // c = -18

12. Using bitwise logical “XOR”

Bitwise “XOR” is a binary operation (requires two operands).

In Java, the bitwise “XOR” denoted by the symbol ^.

Operation ^ for two digits gives a result according to the table:

0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 0

For operands 25 and 18 the operation “XOR” gives the following result

00011001    = 25
00010010    = 18
----------------
00001011    = 11

Example of bit-wise operation “XOR“:

// bit-wise operation "XOR"
byte a, b, c;

a = 25;
b = 18;
c = (byte)(a ^ b);  // c = 11

a = -20;
b = 10;
c = (byte)(a ^ b); // c = -26

13. How are represented negative integers?

In the Java programming language, all integer data types can be positive or negative.

Negative numbers are represented in the additional code.

The algorithm of forming a negative number from a positive is following:

  • all bits are inverted (0 is changed to 1 and vice versa);
  • to the obtained value is added 1.

Example 1. Presentation of -8 from positive number 8. The numbers are of byte type (8 bits).

Positive number 8   => 00001000
Inverting           => 11110111
+ 1                 => 11111000 => -8

Example 2. Representation of number 8 of a negative number -8.

A negative number   -8 => 11111000
Inverting              => 00000111
+ 1                    => 00001000 => 8

If do the validation: 8 + (-8) = 0.

          00001000 =>  8
      +   11111000 => -8
-----------------------------
Sum   =>  00000000 =>  0





14. Features of use of the shift operation to the left (<<)

The left shift operation shifts all bits to the left by a specified number of positions. The general form of of the shift operation to the left:

value << amount;

where

  • amount – it is the number of positions that need to shift of bits to the value.

When shift to the left, the most significant bit is shifted in the acceptable range of values and is lost. From the right side is added zero. The shift to the left by n positions means multiplying a number by 2 in the degree of n.

If are shifted the values of type byte and type short, at first they automatically are given into the int type. Therefore, shifted to the left bits are not lost within the boundaries of bigness of type int (int type is larger than the byte and short types.).

Furthermore, when casting to int type, the negative value of byte and short types is preserved (most significant bit is saved).

One should be careful in shear values of byte and short types.

Example.

Shift of the number 27 on the left to 1 bit is as follows:

00011011 = 27
<<         1
------------
00110110 = 54 = 27*2¹

Shift of the number 11 on 3 bits to the left:

00001011 = 11
<<         3
------------
01011000 = 88 = 11*2³

15. Features of use of the shift operation to the right (>>)

The operation of right shift shifts all bits to the right by a specified number of positions. General view of the shift operation to the right:

value >> amount;

where

amount – it is the number of positions that need to shift of bits to the value.

When is shift to the right by one position, the most significant bit is shifted to the right and lost.

Most significant character is complemented by zero (if the number is positive).

If the number is negative, the most significant bit complemented by 1. It is called an extension of the sign.

The shift to the right by n positions is division on the number 2 in the degree of n..

Example.

The shift to the right of number 27 on 2 bits:

00011011 = 27
>>         1
------------
00000110 =  6 = 27/2²

The shift to the right of number 100 on 1 bit:

01100100 = 100
>>         1
------------
00110010 =  50 = 100/2¹

Example of a right shift of a negative number. The sign of number is stored (most significant bit).

11111000 = -8
>>         1
------------
11111100 = -4

16. Example of operations of shift to the left and shift to the right
// shift to the left
byte a, b;

a = 27;
b = (byte)(a << 1); // b = 54

a = 18;
b = (byte)(a<<2);   // b = 72

// shift to the right
a = 15;
b = (byte)(a >> 2); // b = 3

a = 88;
b = (byte)(a>>2);   // b = 22

17. What are the features of shift to the right operation with zero-fill (>>>)

In the preceding paragraphs was determined, when right shift (>>) is executed, the sign of the number is stored. That is, if the number is negative, the most significant bit complemented by 1.

Sometimes you need, when is shift to the right, the sign of the number is not expanded. That is, you need to perform an unsigned shift to the right. To do this Java provides an operation right shift with zero-fill, which is denoted by >>>. This operation is called an unsigned right shift.

Example.

// shift to the right with preserving the sign
int a, b;

a = -5;
b = a >> 2; // b=-2 - the sign is preserved

// right shift with zero-fill
b = a >>> 2; // b=1073741822 - sign gets lost

18. How in Java are used the composite operations of shift with assignment? Example

In Java are the following shift operation with assignment:

Operation   Description
---------------------------------------------------
&=          Bitwise logical "AND" with assignment
|=          Bitwise logical "OR" with assignment
^=           Bitwise logical "XOR" with assignment
>>=         Shift to the right with assignment
>>>=        Right shift with zero-fill and assignment
<<=         Left shift with assignment

These operations are similar to the operations:

& – bitwise logical "AND"
|  – bitwise logical "OR"
^ – bitwise logical "XOR"
>> – bitwise right shift
<< – bitwise left shift
>>> – bitwise right shift with zero-fill

Example.

// bitwise composite operations with assignment
int a, b, c;
a = 15;
b = 22;
b &= a; // b = b & a, b = 6

b = 22;
b |= a; // b = b | a, b = 31

b = 22;
b ^= a; // b = b ^ a, b = 25

b = -22;
b >>= 1; // b = b >> 1; b = -11

b = -22;
b <<= 1; // b = b << 1; b = -44

b = -22;
b >>>= 1; // b = 2147483637


Related topics