The truth table for AND is:

false AND true = false

true AND false = false

true AND true = true

This article explains the use of these three operators, beginning with the logical-And operator, &.

## Logical & Operator

A boolean value is either true or false. This operator should be used, when both operands are boolean values. The following program illustrates this:

In Java, a program is a programmer implemented class. The name of the program file is the class name. The class should have the main() method. For this program, the name of the class is, TheClass. In the main() method here, there are four lines. Each line corresponds to a line in the AND truth table. Each line prints its result. The output is:

false

false

true

confirming that this is AND logic.

## The conditional-And Operator, &&

The operator, && is an AND operator, and it is used in if-conditions and loop-conditions. Its left operand is a general purpose expression, and its right operand is also a general purpose expression. The following program illustrates its same use, outside the if or loop condition:

In the main() method here, there are four lines. Each line corresponds to a line in the AND truth table. Note the left and right operands for each && operator. Each of these operands is an expression. Each of these expressions results in true or false. So, a true expression could have been substituted with the bare-word, true, and a false expression could have been substituted with the bare-word, false. The output is:

false

false

true

confirming that this is AND logic.

The above program is rewritten, where each line of interest, is an if-compound-statement:

public static void main(String[] args) {

if (1==0 && 1==0) System.out.println(true); else System.out.println(false);

if (1==0 && 1==1) System.out.println(true); else System.out.println(false);

if (1==1 && 1==0) System.out.println(true); else System.out.println(false);

if (1==1 && 1==1) System.out.println(true); else System.out.println(false);

}

}

In the main() method here, there are four lines. Each line corresponds to a line in the AND truth table. Note the left and right operands for each && operator. Each of these operands is an expression. Each of these expressions results in true or false. So, a true expression could have been substituted with the bare-word, true, and a false expression could have been substituted with the bare-word, false. The output is:

false

false

true

confirming that this is AND logic.

## The Bitwise-AND Operator, &

The AND truth table with bits is:

0 & 1 = 0

1 & 0 = 0

1 & 1 = 1

& is the AND operator for bits, as well as it is the AND operator for boolean values.

Now 1111111100000000 in hexadecimal is 0xff00, and in decimal, it is 65280.

Also, 1111000011110000 in hexadecimal is 0xf0f0, and in decimal, it is 61680.

Also, 1111000000000000 in hexadecimal is 0xf000, and in decimal, it is 61440.

Decimal means base 10.

ANDing binary numbers bit-by-bit is called bitwise ANDing, and the operator for this is &. So

The same thing applies to their corresponding hexadecimal numbers. That is:

The same thing applies to their corresponding decimal numbers. That is:

The bitwise operator is normally used with hexadecimal numbers or decimal numbers.

## Bitwise AND (&) with Hexadecimal Numbers

The following program bitwises 0xff00 & 0xff00 to have 0xf000:

num1 and num2 are declared and initialized with hexadecimal numbers. The third statement does the bitwise AND, using &, for these hexadecimal numbers. The last statement prints out the result. The output is 61440, which is the decimal equivalent of the expected, 0xf000.

## Bitwise AND (&) with Decimal Numbers

The following program bitwises 65280 & 61680 to have 61440 (all decimal numbers):

num1 and num2 are declared and initialized with decimal numbers. The third statement does the bitwise AND, using &, for these decimal numbers. The last statement prints out the result. The output is 61440, which is the decimal equivalent of binary 1111000000000000.

## Conclusion

In Java, && is called the conditional-And operator. In Java, & is the logical-And operator and also the bitwise operator. These three operators are binary operators, in the sense that each has a left operand and a right operand. && is used when the left and right operands are expressions, where each results in true or false. Either of these expressions can actually be substituted with true or false & is used when handling boolean values: true or false. & must be used when bitwise AND is required.