__Bitwise AND "&" operator on Integers in Java__

In this tutorial, we will show how to use the bitwise AND operator "&" on integers, and the side effects that this would have such as

effects on signed and unsigned numbers.

In Java, there are two uses for the "&" operator. The first is to perform logical AND on booleans. The second is to perform a bitwise

AND operation on integers. The operator is called

at a time. As with logical AND, the behavior of bitwise and is as follows:

0 & 0 = 0

0 & 1 = 0

1 & 0 = 0

1 & 1 = 1

Therefore, if we do an and operation between two numbers, the following would happen, let us say we will AND the numbers

3 and 2, with the corresponding binary representations 11 and 10. Performing the AND will work as follows:

AND the most significant bit of the first number with the most significant bit of the second number (1 AND 1).

AND the 2nd most significant bit of the first number with the 2nd most significant bit of the second number (1 AND 0).

Hence:

AND 11

10

= 10

The following code is an example on how to perform bitwise AND on integer numbers in Java:

effects on signed and unsigned numbers.

In Java, there are two uses for the "&" operator. The first is to perform logical AND on booleans. The second is to perform a bitwise

AND operation on integers. The operator is called

*bitwise*since the operation is performed on corresponding bits on two integer numbersat a time. As with logical AND, the behavior of bitwise and is as follows:

0 & 0 = 0

0 & 1 = 0

1 & 0 = 0

1 & 1 = 1

Therefore, if we do an and operation between two numbers, the following would happen, let us say we will AND the numbers

3 and 2, with the corresponding binary representations 11 and 10. Performing the AND will work as follows:

AND the most significant bit of the first number with the most significant bit of the second number (1 AND 1).

AND the 2nd most significant bit of the first number with the 2nd most significant bit of the second number (1 AND 0).

Hence:

AND 11

10

= 10

The following code is an example on how to perform bitwise AND on integer numbers in Java:

public class BitwiseANDExample { |

public static void main(String[]args){ |

byte x = 7; //equivalent to binary of 00000111 |

byte y = -7; //equivalent to binary of 10000001 |

byte xANDy = (byte)(x & y); |

System.out.println("X AND Y = "+xANDy); //result of AND = 00000001 |

int a = 13; //0000...0001101 |

int b = 11; //0000...0001011 |

int aANDb = a & b; |

System.out.println("A AND B = "+aANDb);// result of AND = 1001 |

} |

} |

Notice that in Java, signed / negative numbers are represented using the 2's compliment representation. Hence, the number -7 is represented

as 10000001. Therefore ANDing -7 and 7 produces in positive 1, since the sign bit in the resulting number is 0. The following is the output of running

the above program:

as 10000001. Therefore ANDing -7 and 7 produces in positive 1, since the sign bit in the resulting number is 0. The following is the output of running

the above program:

X AND Y = 1 A AND B = 9

Notice how the AND operator is performed bit by bit on corresponding bits within the ANDed numbers. (The binary representations of the numbers

are available in the comments in the code above).

are available in the comments in the code above).