• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

& | with integers

 
deva raj
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class test {
public static void main(String args[]) {
int x,y;
x = 3 & 5;
y = 3 | 5;
System.out.println(x + " " + y);
}
}

Can anyone explain how this works?


Note: - New to java.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to Java in General (Beginner).

Check out the JLS, §15.22.1 Integer Bitwise Operators &, ^, and |.
 
Jeffrey Bennett
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x = 3 & 5;

In binary notation, this translates as:
x = (011 & 101);

A bitwise AND operation yields:
x = (001)
x = 1;

----------------
y = 3 | 5;
y = (011) | (101)
y = (111); // <-- Bitwise OR operation
y = 7;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic